-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerApp.cs
50 lines (45 loc) · 1.34 KB
/
ServerApp.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
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Http;
using WebApp.Handlers;
using DotNetEnv;
namespace WebApp
{
public class ServerApp
{
private readonly int port;
public ServerApp(int port)
{
this.port = port;
}
public void Start()
{
var host = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(services => services.AddSingleton(this))
.Configure(app =>
{
app.Map("/api", apiApp => apiApp.Run(new ApiHandler().HandleApiRequest));
app.Run(new RequestHandler().HandleRequest);
})
.UseUrls($"http://localhost:{port}/")
.Build();
host.Run();
}
}
public class Program
{
public static void Main(string[] args)
{
DotNetEnv.Env.Load();
string portString = Environment.GetEnvironmentVariable("port");
int.TryParse(portString, out int port);
ServerApp server = new ServerApp(port);
server.Start();
Console.WriteLine($"Server started on port {port}");
}
}
}