-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
123 lines (99 loc) · 4.3 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Welcome To Homework Tracker Server's Source Code
Things to do TODO:
- Push notifications using OneSignal
- More robust error handling
*/
using System;
using System.Collections.Generic;
using System.IO;
using HomeworkTrackerServer.Storage;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace HomeworkTrackerServer;
public static class Program {
public static IStorageMethod Storage { get; set; }
public static readonly Version Ver = new(0, 9, 0);
public static readonly string WwwAuthHeader = "Bearer realm=\"HomeworkAccounts\"";
public static bool Debug { get; private set; }
public static bool StorageInitialized { get; set; }
public static Dictionary<string, string> Config { get; private set; }
public static (string, string)[] CustomHeaders { get; set; }
public static void Main(string[] args) {
Console.WriteLine("Starting Log Initialization");
// Initialize logging (LogLevel gets updated once config is loaded)
Logger.Init(LogLevel.Debug);
// Apply args
int argCounter = 0;
while (argCounter < args.Length) {
switch (args[argCounter]) {
case "--debug":
Debug = true;
Logger.Info("Debug mode enabled");
break;
case "--directory":
try {
Directory.SetCurrentDirectory(args[argCounter+1]);
}
catch (Exception) {
Logger.Error("Invalid directory");
return;
}
Logger.Info("Set active directory to " + Directory.GetCurrentDirectory());
argCounter++; // Skip next arg because it's the directory
break;
case "--config":
ConfigManager.ConfigFileName = args[argCounter+1];
Logger.Info("Set config file to: " + args[argCounter+1]);
argCounter++; // Skip next arg because it's the config file
break;
default:
Logger.Error($"Invalid Argument: {args[argCounter]}");
Logger.Info("Exiting...");
return;
}
argCounter++;
}
// Print info
Logger.Info($"Starting Homework Tracker Server v{Ver}");
Logger.Info($"Active Directory: {Directory.GetCurrentDirectory()}");
// Custom config because ASP.NET's config system is stupid
Logger.Info("Loading config...");
try {
Config = ConfigManager.LoadConfig(); // Attempt to load config and correct any errors
}
catch (InvalidConfigException) {
Logger.Error("Failed to load config, config is invalid!");
throw;
}
Logger.Info("Loaded config");
Logger.LoggingLevel = (LogLevel) int.Parse(Config["LoggingLevel"]);
// Run actual server (Catch all errors)
try {
CreateHostBuilder(args).Build().Run();
}
catch (Exception e) {
Logger.Error("Program exited with an error: " + e.Message);
Logger.Error(e.ToString());
}
Logger.Info("Exiting");
if (StorageInitialized) {
Logger.Info("Closing storage");
try {
Storage.Deinit();
}
catch (Exception e) {
Logger.Error("Storage deinit failed: ");
Logger.Error(e);
}
} else { Logger.Info("Storage not initialized, skipping deinit"); }
Logger.Info("Bye!");
Logger.WaitFlush(); // Flush all logs
}
// Yes I'm aware that http is insecure but you should just use a reverse proxy for https
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>().UseUrls(Config.ContainsKey("bind_address") ? Config["bind_address"] : "http://*:9898");
});
}