-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.cs
More file actions
133 lines (104 loc) · 3.56 KB
/
express.cs
File metadata and controls
133 lines (104 loc) · 3.56 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ExpressSharp
{
public class express
{
HttpListener listener;
Dictionary<Tuple<string, HttpMethod>, Action<Request, Response>> routes;
Dictionary<string, Action<Request, Response>> middlewares;
public string host = "localhost";
public string protocol = "http";
public express()
{
listener = new HttpListener();
routes = new Dictionary<Tuple<string, HttpMethod>, Action<Request, Response>>();
middlewares = new Dictionary<string, Action<Request, Response>>();
}
public void stop()
{
listener.Stop();
}
public void use(string prefix, Action<Request, Response> callback)
{
middlewares[prefix] = callback;
}
public void listen(int port, Action callback)
{
string baseAddress = $"{protocol}://{host}:";
if (!HttpListener.IsSupported)
{
Console.WriteLine("HttpListener is not suppotred.");
return;
}
if (listener.IsListening)
{
return;
}
foreach (var s in routes.Keys)
{
string url = baseAddress + port.ToString() + s.Item1 + "/";
listener.Prefixes.Add(url);
}
listener.Start();
callback();
Task.Factory.StartNew(async () =>
{
while (true)
{
var context = await listener.GetContextAsync();
handleIncomingRequest(context);
};
}, TaskCreationOptions.LongRunning);
}
private void handleIncomingRequest(HttpListenerContext context)
{
if (context.Request.Url is null)
{
throw new Exception("Fail to proccess request, context is null");
}
var request = new Request(context.Request);
var response = new Response(context.Response);
var path = context.Request.Url.AbsolutePath;
var method = context.Request.HttpMethod;
var tuple = Tuple.Create(path, new HttpMethod(method));
if (middlewares.ContainsKey(path))
{
middlewares[path](request, response);
}
if (routes.ContainsKey(tuple))
{
routes[tuple](request, response);
}
else
{
response.send($"Cannot {method} " + path);
}
}
public void get(string prefix, Action<Request, Response> callback)
{
routes[Tuple.Create(prefix, HttpMethod.Get)] = callback;
}
public void post(string prefix, Action<Request, Response> callback)
{
routes[Tuple.Create(prefix, HttpMethod.Post)] = callback;
}
public void put(string prefix, Action<Request, Response> callback)
{
routes[Tuple.Create(prefix, HttpMethod.Put)] = callback;
}
public void delete(string prefix, Action<Request, Response> callback)
{
routes[Tuple.Create(prefix, HttpMethod.Delete)] = callback;
}
public void patch(string prefix, Action<Request, Response> callback)
{
routes[Tuple.Create(prefix, HttpMethod.Patch)] = callback;
}
}
}