-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
86 lines (69 loc) · 2.62 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
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using hlcup2018.Models;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using hlcup2018.Controllers;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
var folder = args.Length > 0 ? args[0] : null;
var loader = new StorageLoader(Storage.Instance);
loader.Load(folder == null ? "/tmp/data/" : @"C:\Old\MyProjects\core\hlcup2018\" + folder + @"\data");
GetHostBuilder().Build().Run();
}
public static IWebHostBuilder GetHostBuilder(string port = "80") => new WebHostBuilder()
.UseKestrel()
.UseUrls($"http://*:{port}")
.Configure(cfg => cfg.Run(ctx => Task.Run(() => HandleRequest(ctx))));
private static AccountsController controller = new AccountsController();
private static void HandleRequest(HttpContext ctx) {
var body = AccountsController.empty;
var parts = ctx.Request.Path.Value.Split('/', StringSplitOptions.RemoveEmptyEntries);
var l = parts.Length;
if (parts[0] != "accounts")
{
ctx.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
bool get = ctx.Request.Method == "GET";
bool post = ctx.Request.Method == "POST";
var p1 = parts.Last();
switch (p1) {
case "filter" when get:
body = controller.Filter(ctx); // accounts/filter
break;
case "group" when get:
body = controller.Group(ctx); // accounts/group
break;
case "recommend" when get:
body = controller.Recommend(ctx, parts[1]); // accounts/5/recommend
break;
case "suggest" when get:
body = controller.Suggest(ctx, parts[1]); // accounts/5/suggest
break;
case "new" when post:
body = controller.New(ctx); // accounts/new
break;
case "likes" when post:
body = controller.Likes(ctx);// accounts/likes
break;
default:
if (l == 2 && p1[0] <= '9')
body = controller.Update(ctx, parts[1]);
else
ctx.Response.StatusCode = StatusCodes.Status404NotFound;
break;
}
if (body.Length > 0) {
ctx.Response.ContentLength = body.Length;
ctx.Response.Body.Write(body, 0, body.Length);
}
return;
}
}