-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpserver.js
142 lines (134 loc) · 4.75 KB
/
httpserver.js
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
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
var httpserver = {};
httpserver.cors = "*";
httpserver.handler = {};
httpserver.path = (path, f) => (httpserver.handler[path] = f);
httpserver.staticdir = "";
httpserver.spa = false;
httpserver.statichandler = null;
httpserver.readfile = null;
httpserver.default = async (r) => {
return new Response(`404`, {
status: 404,
});
};
httpserver.notfound = async (r) => {
return new Response(`404`, {
status: 404,
});
};
async function handler(request, connInfo) {
var h = httpserver.handler[new URL(request.url).pathname];
try {
var res;
if (request.method == "OPTIONS"){
res = new Response()
}
if (request.method != "OPTIONS"){
if (!h) {
if(httpserver.statichandler){
res = await httpserver.statichandler(request, connInfo);
}
if(!httpserver.statichandler){
res = await httpserver.notfound(request, connInfo);
}
}
if (h) {
res = await h(request, connInfo);
}
}
if (request.headers.get("Origin")) {
res.headers.set("Access-Control-Allow-Origin", httpserver.cors);
res.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, HEAD, PATCH");
if (request.headers.get("Access-Control-Request-Headers")) {
res.headers.set("Access-Control-Allow-Headers", request.headers.get("Access-Control-Request-Headers"));
}
res.headers.set("Access-Control-Max-Age", 24 * 60 * 60);
}
return res;
} catch (e) {
return new Response(`${e}`, {
status: 500,
});
}
}
httpserver.run = async (opt) => {
if(httpserver.staticdir || httpserver.readfile){
httpserver.statichandler = async (r, connInfo) => {
var s = `${new URL(r.url).pathname}`;
if (s.endsWith("/")) {
s += "index.html";
}
var t = "text/plain; charset=utf-8";
if (s.endsWith(".html")) {
t = "text/html; charset=utf-8";
}
if (s.endsWith(".png")) {
t = "image/png";
}
if (s.endsWith(".css")) {
t = "text/css";
}
if (s.endsWith(".js")) {
t = "application/javascript";
}
if (s.endsWith(".json")) {
t = "application/json";
}
if (s.endsWith(".jpg") || s.endsWith(".jpeg") || s.endsWith(".jfif") || s.endsWith(".pjpeg") || s.endsWith(".pjp")) {
t = "image/jpeg";
}
try {
if(httpserver.staticdir){
var data = await Deno.readFile(join(httpserver.staticdir, s));
}
if(httpserver.readfile){
var data = await httpserver.readfile(s);
}
return new Response(new TextDecoder("utf-8").decode(data), {
status: 200,
headers: new Headers({
"Content-Type": t,
}),
});
} catch (e) {
if (e.toString().indexOf("NotFound") == -1) {
return new Response(`${e}`, {
status: 500,
});
}
if(!httpserver.spa || s == "/index.html"){
return new Response(`404`, {
status: 404,
});
}
try {
if(httpserver.staticdir){
var data = await Deno.readFile(join(httpserver.staticdir, "index.html"));
}
if(httpserver.readfile){
var data = await httpserver.readfile("/index.html");
}
return new Response(new TextDecoder("utf-8").decode(data), {
status: 200,
headers: new Headers({
"Content-Type": "text/html; charset=utf-8",
}),
});
} catch (e) {
if (e.toString().indexOf("NotFound") == -1) {
return new Response(`${e}`, {
status: 500,
});
}
return new Response(`404`, {
status: 404,
});
}
}
};
}
await serve(handler, opt);
};
export default httpserver;