-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeb.cs
85 lines (69 loc) · 3.33 KB
/
Web.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace rt4k_pi;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using rt4k_pi.Slices;
using System.Reflection;
public partial class Program
{
public static void RunWeb()
{
var builder = WebApplication.CreateSlimBuilder();
builder.Configuration.Sources.Clear(); // Disable appsettings
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?> { { "AllowedHosts", "*" } });
builder.WebHost.UseUrls("http://*:80");
builder.Logging.ClearProviders();
builder.Logging.AddSimpleConsole(options =>
{
options.IncludeScopes = false;
options.SingleLine = true;
options.ColorBehavior = Microsoft.Extensions.Logging.Console.LoggerColorBehavior.Disabled;
});
var app = builder.Build();
var embeddedProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly(), "rt4k_pi");
var contentProvider = new FileExtensionContentTypeProvider();
contentProvider.Mappings.Add(".avif", "image/avif"); // dotnet doesn't know about AVIF yet
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = embeddedProvider,
ContentTypeProvider = contentProvider,
ServeUnknownFileTypes = true
});
var appState = new AppState()
{
Logger = logger,
Serial = Serial,
Ser2net = Ser2net,
StatusDaemon = StatusDaemon,
FuseDaemon = FuseDaemon,
Settings = Settings,
Installer = Installer,
RT4K = RT4K
};
var assembly = Assembly.GetExecutingAssembly();
// Retrieve and print all embedded resource names
Console.WriteLine("Embedded resources:");
foreach (var resourceName in assembly.GetManifestResourceNames())
{
Console.WriteLine(resourceName);
}
// Static file overrides
app.MapGet("/favicon.ico", () => Results.File(embeddedProvider.GetFileInfo("Static/favicon.ico").CreateReadStream(), "image/x-icon"));
// Pages
app.MapGet("/", () => Results.Extensions.RazorSlice<Slices.Status, Slices.AppState>(appState));
app.MapGet("/Remote", () => Results.Extensions.RazorSlice<Slices.Remote, Slices.AppState>(appState));
app.MapGet("/Calculator", () => Results.Extensions.RazorSlice<Slices.Calculator, Slices.AppState>(appState));
app.MapGet("/Settings", () => Results.Extensions.RazorSlice<Slices.Settings, Slices.AppState>(appState));
app.MapGet("/DebugLog", () => Results.Extensions.RazorSlice<Slices.DebugLog, Slices.AppState>(appState));
// APIs
app.MapGet("/GetUpdateStatus", () => Installer.GetStatus());
app.MapGet("/CheckUpdates", () => Installer.CheckUpdate());
// Commands
app.MapGet("/SendSerial", ([FromQuery] string cmd) => Serial?.WriteLine(cmd));
app.MapPost("/RemoteCommand/{cmd}", ([FromRoute] string cmd) => RT4K?.SendRemoteString(cmd) );
app.MapPost("/UpdateSetting/{name}/{value}", ([FromRoute] string name, [FromRoute] string value) => Settings.UpdateSetting(name, value) );
app.MapPost("/InstallUpdate", () => Installer.DoUpdate());
Console.WriteLine("rt4k_pi startup complete.");
app.Run();
}
}