-
Notifications
You must be signed in to change notification settings - Fork 50
/
Program.cs
115 lines (98 loc) · 3.38 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
using DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Web;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;
using System;
using System.Linq;
namespace DevelopmentInProgress.TradeServer.Console
{
class Program
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types")]
static void Main(string[] args)
{
try
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File("Logs\\DevelopmentInProgress.TradeServer.Console-.log", rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();
Log.Information($"Running as {Environment.UserName}");
if (args == null
|| args.Length.Equals(0))
{
Log.Warning($"No args. Use defaults...");
args = new[]
{
$"s=TradeServer",
"u=http://localhost:5500"
};
}
else if (InvalidArgs(args))
{
Log.Error($"Invalid args");
foreach (var arg in args)
{
Log.Error($"{arg}");
}
Log.Error($"You must provide the following args:");
Log.Error($"--s=YourServerName");
Log.Error($"--u=http://localhost:5500");
return;
}
Log.Information($"args");
foreach (var arg in args)
{
Log.Information($"{arg}");
}
var url = args.First(a => a.StartsWith("u=", StringComparison.Ordinal)).Split("=")[1];
Log.Information("Launching DevelopmentInProgress.TradeServer.Console");
var webHost = WebHost.CreateDefaultBuilder()
.UseUrls(url)
.UseStrategyRunnerStartup(args)
.UseSerilog()
.Build();
var task = webHost.RunAsync();
task.GetAwaiter().GetResult();
}
catch (Exception ex)
{
Log.Fatal(ex, "DevelopmentInProgress.TradeServer.Console terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
private static bool InvalidArgs(string[] args)
{
var snMissing = true;
for(int i = 0; i < args.Length; i++)
{
if(args[i].StartsWith("--s=", StringComparison.Ordinal))
{
snMissing = false;
break;
}
}
var urlMissing = false;
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("--u=", StringComparison.Ordinal))
{
urlMissing = false;
break;
}
}
if(snMissing || urlMissing)
{
return true;
}
for (int i = 0; i < args.Length; i++)
{
args[i] = args[i][2..];
}
return false;
}
}
}