-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.cs
39 lines (31 loc) · 1.24 KB
/
WebServer.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
using CCTavern.Web;
using EmbedIO;
using EmbedIO.WebApi;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CCTavern {
internal class WebServer {
public IServiceProvider ServiceProvider { get; }
public WebServer(IServiceProvider serviceProvider) {
ServiceProvider = serviceProvider;
}
public async Task StartServer(CancellationToken cancellationToken) {
using var server = new EmbedIO.WebServer(o => o
.WithUrlPrefix("http://*:10001/")
.WithMode(HttpListenerMode.EmbedIO));
// Inject dependencies into controller
server.WithLocalSessionManager();
_ = server.WithWebApi("Serve status page", "/api/status",
m => m.RegisterController( () => ServiceProvider.GetInstance<StatusController>()! )
);
server.WithAction("/", HttpVerbs.Any, async (ctx) => await ctx.SendStandardHtmlAsync(200, (x) => { x.WriteLine("Waiting for API requests"); }));
// Start server
await server.RunAsync(cancellationToken);
}
}
}