-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.ts
121 lines (109 loc) · 7.67 KB
/
server.ts
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
import http from 'http';
import https from 'https';
import * as fs from "fs";
import {std} from "./index"
import route from './routes/route';
export function log(...args: any[]) {
std.write(`CONSOLE: ${args.join(" ")}\n`);
}
type IRoute = typeof import("./routes/route");
export default class Dispatch{
httpServer?: http.Server;
httpsServer?: https.Server;
constructor(public httpPort: number,public httpsPort: number){
}
public start(){
if(this.httpServer || this.httpsServer){
log("already started");
return;
}
this.httpServer = http.createServer(this.handleRequest);
this.httpsServer = https.createServer({
key: fs.readFileSync('./src/cert/ys.key'),
cert: fs.readFileSync('./src/cert/ys.crt')
}, this.handleRequest);
this.httpServer.listen(80, '127.0.0.1');
this.httpsServer.listen(443, '127.0.0.1');
log(`started on port ${this.httpPort} and ${this.httpsPort}
▄█
▄▓▓▌
▓▓▓▓▌
▄▓▓▓▓█▄▒▓▒▒▒░░▒░░░░▀▀▓▒▄
▓▓▓▓▓▓▌▒▒░░░░░░░░░░░░░░░▒▒▒▒▄
▄▓▓▓▓▓▀▀░▒▒░░░░░░░░░░░░░░░░░░▒▒▒▄
▄ ▄ █▓▀░░░▒▒▓▒░░░░░░░░░░░░░░░░░░░░░░▒▒▒▄
▐█▓▓▓▓▒▄▄▒▒▒▒▒░░▒▒▒▒▒▒▒░░░▒▒▒▒▒▒▒░░░░░░░▒▒▓
█▒▓▓▒▒██▓▒░░░▒▒▒░░░░░░░░░░░░░░▒▒▒▒░░░░░░▒▒▒
▒▒▒░▒▒▒▓▓▄▄▄▄▄░░░░░░░░░░░░░░░░░░░▒▒░░░░░░▒▒▒▄
▓▒▒▒▒▓▓▓▓▓▓▓█▀░░░░░░░░░░░░░░░░░░░░░░▒░░░░░░▒▒▒▒▄
▓▒▒▓▒▓█████▀░░░░░░░░░░░░░░░░░░░░░▒▒░░▒░░░░░░▒▒▒▒▒▒▄
▐▒▒▒▓▀░░░▒▒▒▒░░░░░░░░░░░░░░░░░▒░▒░▒▒░░▒░░░░░▒▒▒▒▓▒▒▒▒▒▒▓▀
▓▒▓▓▓▒▒▒▒▒▒▒▒▒░░░░░░░▒░░░░░░░░▒▒▒░░▒░▒▒▒▒░░▒▒▒▒▒▓ ▀▀▐▀▀
▄▐▓▓▒█▒▒▒▒▒▒▒▒▒▒▒▒░░░░▒▒░░░░▒░░░▒▓▀▀░░▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▓▒▒▒▒▒▒▒▒▒▓█▓▒▒░░▒▒▒▒░░░▒░▒▒▓▒▄▄▄▄▄▌▒▒▒▒▒▒▒▒▒▒▒▓▌
▒▒▒▓▓▒▒▒▒▒▒▓▒▒▓▌▓▓▒▒▓▓▒░▒▒░▒▒▒▒▐█▀▒▒▒▒▓▓▒▒▓▒▒▒▒▒▒▒▓▓▒▒░▐
▒▒▓▓▒▒▒▒▒▒▓▓▒▒▌░▒░▒▒▓░░░░░░░░░░▓▒▒░▒▓▓▌▒▒▓▒▒▒▒▒▒▓▒▒▒▒▒
▒▒▌▓▓▒▒▒▒▒▓▒▒▒▒▒░░░▒░░░░░░░░░░░▐▒░░░░▓▒▒▒▓▒▒▒▒▒▒▒▓▒▒▌| ElysiaPS v1.0.1
▐▒▓ ▓▓▒▒▒▒▒▓▒▒▒░░░░░░░░░░░░░░░░░░░░░░▒▒▒▓▓▒▒▒▒▒▓▓▀▒▒▌
▒▓▌ ▐▓▓▓▓▒▒▒▓▒▒░░░░░░░░░░░░░░░░░░░░░▒▒░▓▒▒▒▒▒▓▒▓ ▐▒▒▌ | Via Matsuko
▀▓▓ ▐▓▒▒▒▒▓▓▓▓▓▒░░░░░░░░░░░░░░░░░░░▓▓▓▒▒▓▓▓▒▓▓ ▐▒▒
▓▓▄ ▀▓▓▒▒▒▒▒▒▒▒▓▒▒▓▄▄▒░░░▒▒▄▄▄▓▓▓▒▓▒▒▒▒▒▒▓▓▓ ▄▓▒▀
▀ ▀▓▓▀▓▒▒▒▒▒▒▒▓▓████▓▒▓▓▀▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓ ▄▓▓▀
▄▒▒▒▓▓▒▓▒▒▓▓▒▓▒▓▓▒▒▓▒▓▒▒▒▒▒▓▀ ▀▀
▄▄▓██▓███▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▌
▓▓▀██████▒▓▒▒░░░░░░▒▒▌▒▒▒▒▒▀
▄▄███████▌▌▒▓▒▒▓▒▒░░░▒▓▀▄▓▓▀▀▓▄
▄▓▓▓▓█████▓▌▌▒░▐▓▒▒░░░░▒▒▒▒▓▒▒░░░▒▓▄
▐▀▓▓████▀▒▒▓█▒░░▒▓▒▒▒░░░▐▌▒▒▓▓▒▒▒▒▒▓███
▄▒▒▀██▀▒▒▒▓▓▓▒██▒▓▄░▒░░▒░█▄▄▄▄█▓▓▀████▀
▀▒░▒░▒▀▀▀█▓▓▒███▒███████████▓▓▓▓▄ ▀▀▀
▒▒ ▄▀▓▓▓▀▀██████████▀▀▀▒▒░▐▓▄
▄▄▒▓▓▒░░░░░░▀███▀▄▄▓▓██▒▒▒▓▓
█████▓▒░░░░░░▒██▓█▀▀░░░░▒▒░▓▓▓
████████▄▄▄▄▄▒▓████▒▒▄██▄▄▓▒▓▓▓▌
▀▀▀ ▐██████▀▀▀▀█▓██████▀█▀▓▓▓
██████ ▐██████ ▓▓
▐█████ ██████
█████ █████▌
██▓█ █████
▀▀▓▀ ▓▓██
`);
}
public stop(){
if(!this.httpServer ||!this.httpsServer){
log("already stopped");
return;
}
this.httpServer.close();
this.httpsServer.close();
this.httpServer = undefined;
this.httpsServer = undefined;
}
async handleRequest(req: http.IncomingMessage, res: http.ServerResponse){
const url: URL = new URL(req.url!, `http://${req.headers.host}`);
console.log(url.pathname.split('?')[0]);
const data = {
path: url.pathname.split('?')[0],
}
if(url.pathname.includes(".ico")){
res.writeHead(200, {
'Content-Type': 'image/x-icon'
})
res.write(fs.readFileSync(`./src/routes/elysia.ico`));
res.end();
return;
}
try{
let route = await import(`./routes${data.path}`) as IRoute;
route.default.get(req, res);
}catch(e){
try{
let rsp = await import(`./routes${data.path}`) as object;
res.end(JSON.stringify(rsp))
}catch(e){
log(e)
res.end("404");
}
}
}
}