forked from microsoft/winget-cli-restsource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (56 loc) · 2.18 KB
/
Program.cs
File metadata and controls
71 lines (56 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.WinGet.RestSource.AppConfig;
using Microsoft.WinGet.RestSource.Server.AppConfig;
using Microsoft.WinGet.RestSource.Server.Middleware;
using Microsoft.WinGet.RestSource.Sqlite;
using Microsoft.WinGet.RestSource.Utils.Common;
using Microsoft.WinGet.RestSource.Utils.Constants;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Set ServerIdentifier from config/environment
var serverIdentifier = builder.Configuration["ServerIdentifier"] ?? "winget-restsource-self-hosted";
Environment.SetEnvironmentVariable(ApiConstants.ServerIdentifierEnvName, serverIdentifier);
// Database
var dbPath = builder.Configuration["Database:Path"] ?? "data/winget.db";
var dbDir = Path.GetDirectoryName(dbPath);
if (!string.IsNullOrEmpty(dbDir) && !Directory.Exists(dbDir))
{
Directory.CreateDirectory(dbDir);
}
builder.Services.AddSingleton<IApiDataStore>(sp =>
new SqliteDataStore(
sp.GetRequiredService<ILogger<SqliteDataStore>>(),
dbPath));
builder.Services.AddSingleton<IWinGetAppConfig>(sp =>
new SimpleAppConfig(builder.Configuration));
// API key auth filter
builder.Services.AddScoped<ApiKeyAuthFilter>();
// Controllers with Newtonsoft JSON
builder.Services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.Formatting = Formatting.None;
});
// Blazor Server
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddFluentUIComponents();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAntiforgery();
app.MapControllers();
app.MapRazorComponents<Microsoft.WinGet.RestSource.Server.Components.App>()
.AddInteractiveServerRenderMode();
// Redirect root to admin dashboard
app.MapGet("/", () => Results.Redirect("/admin"));
app.Run();