-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
51 lines (42 loc) · 1.39 KB
/
Program.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
using System;
using System.IO;
using System.Threading.Tasks;
using BasicHttpServerCore;
using Newtonsoft.Json;
namespace EPGPWeb
{
class Program
{
const string CONFIG_FILE = "config.json";
private class ServerConfig {
public string Prefix { get; set; } = "http://localhost:8080";
}
private static Logger.LogFunc Log = Logger.BuildClassLogger( "Program" );
static async Task Main(string[] args)
{
Logger.Init( ( line ) => Console.WriteLine( line ) );
ServerConfig config = new ServerConfig();
if( !File.Exists( CONFIG_FILE ) ) {
using( StreamWriter sw = new StreamWriter( new FileStream( CONFIG_FILE, FileMode.CreateNew, FileAccess.Write ) ) ) {
await sw.WriteLineAsync( JsonConvert.SerializeObject( config ) );
}
Log( "Main", $"Created a new config file: { CONFIG_FILE }" );
}
else {
using( StreamReader sr = new StreamReader( new FileStream( CONFIG_FILE, FileMode.Open, FileAccess.Read ) ) ) {
string json = await sr.ReadToEndAsync();
config = JsonConvert.DeserializeObject<ServerConfig>( json );
}
}
string baseDir = Environment.CurrentDirectory;
#if DEBUG
baseDir += "\\..\\..\\..\\";
#endif
baseDir = Path.Combine( baseDir, "wwwroot" );
BasicHttpServer server = new BasicHttpServer( config.Prefix );
server.HandleFiles( baseDir );
server.RegisterAPIHandler( "api", new API() );
await server.Start();
}
}
}