-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
188 lines (178 loc) · 7.8 KB
/
Copy pathProgram.cs
File metadata and controls
188 lines (178 loc) · 7.8 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using HistoryContest.Server.Services;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Dynamic;
using System.IO;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
namespace HistoryContest.Server
{
public static class Program
{
public static bool FromMain { get; set; } = false;
public static bool RefreshCache { get; set; } = false;
public static bool RegenerateSeed { get; set; } = false;
public static bool UseWebpack { get; set; } = false;
public static int Port { get; set; } = 5000;
public static string EnvironmentName { get; set; } = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
public static string ContentRootPath { get; set; } = EnvironmentName.ToLowerInvariant() == "development" ?
Directory.GetParent(Directory.GetCurrentDirectory()).FullName : Directory.GetCurrentDirectory();
public static void Main(string[] args)
{
FromMain = true;
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
}
ProcessArgs(args);
return new WebHostBuilder()
.UseKestrel(option => option.Listen(IPAddress.Loopback, Port))
.UseContentRoot(ContentRootPath)
.UseIISIntegration()
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();
}
internal static void ProcessArgs(string[] args)
{
bool runBrowser = false;
dynamic envSetting = new ExpandoObject();
dynamic parseSetting = new ExpandoObject();
envSetting.SwitchEnv = false;
parseSetting.Type = null;
for (int i = 0; i < args.Length; ++i)
{
var arg = args[i];
switch (arg)
{
case "-p":
case "--port":
++i;
Port = int.Parse(args[i]);
break;
case "-rb":
case "--runbrowser":
runBrowser = true;
break;
case "-env":
case "--environment":
++i;
envSetting = new { SwitchEnv = true, EnvName = args[i] };
break;
case "--refresh-cache":
RefreshCache = true;
break;
case "--regenerate-seed":
RegenerateSeed = true;
break;
case "--use-webpack":
UseWebpack = true;
break;
case "--parse-question-sql":
++i;
parseSetting = new { Type = "question", Format = "sql", Path = args[i] };
break;
case "--parse-student-text":
++i;
parseSetting = new { Type = "student", Format = "text", Path = args[i] };
break;
case "--parse-student-excel":
++i;
parseSetting = new { Type = "student", Format = "excel", Path = args[i] };
break;
case "-h":
case "--help":
string[] messages = new string[]
{
"-h|--help 显示帮助。",
"-p|--port <ID> 在指定端口运行服务器。默认为5000",
"-rb|--runbrowser 程序启动后运行默认浏览器打开网站。",
"-env|--environment <env> 设置程序运行环境。默认为Production。",
"--refresh-cache 清除并从数据库中重新加载缓存。Development环境下该开关无效,总是重新加载。",
"--regenerate-seed 重新生成问题种子。Development环境下该开关无效,总是重新生成。",
"--use-webpack 开启Webpack Middleware。一般在前端使用vue时打开此项。",
"--parse-question-sql <path> 解析一个SQL格式问题集到json数据文件。",
"--parse-student-text <path> 解析一个文本格式学生信息集到json数据文件。",
"--parse-student-excel <path> 解析一个Excel格式学生信息集到json数据文件。"
};
foreach (var message in messages)
{
Console.WriteLine(message);
}
Environment.Exit(0);
break;
}
}
#region Environment Setting
if (envSetting.SwitchEnv == true)
{
switch ((envSetting.EnvName as string).ToLowerInvariant())
{
case "development":
EnvironmentName = "Development";
break;
case "staging":
EnvironmentName = "Staging";
break;
case "production":
EnvironmentName = "Production";
break;
default:
throw new ArgumentException("Enviroment name provided invalid. Please choose one in \"Development\", \"Production\", or \"Staging\"");
}
}
switch (EnvironmentName)
{
case "Development":
ContentRootPath = Directory.GetParent(Directory.GetCurrentDirectory()).FullName;
break;
case "Staging":
case "Production":
ContentRootPath = Directory.GetCurrentDirectory();
break;
}
#endregion
if (parseSetting.Type != null)
{
switch (parseSetting.Type as string)
{
case "question":
switch(parseSetting.Format as string)
{
case "sql":
DocParseService.ParseQuestionsFromSQL(parseSetting.Path as string, DocParseService.QuestionSqlFilePattern);
break;
}
break;
case "student":
switch (parseSetting.Format as string)
{
case "text":
DocParseService.ParseStudentsFromText(parseSetting.Path as string);
break;
case "excel":
DocParseService.ParseStudentsFromExcel(parseSetting.Path as string);
break;
}
break;
}
Environment.Exit(0);
}
if (runBrowser)
{
new Timer(o =>
{
string url = @"http://localhost:" + Port;
Console.WriteLine(@"Starting " + url + " with default browser...");
System.Diagnostics.Process.Start("explorer", url);
}, null, (int)TimeSpan.FromSeconds(10).TotalMilliseconds, Timeout.Infinite);
}
}
}
}