-
Notifications
You must be signed in to change notification settings - Fork 8
/
Main.hx
122 lines (112 loc) · 3.06 KB
/
Main.hx
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
import hxargs.Args;
import sys.FileSystem;
import wtri.net.Socket;
var server(default, null):wtri.Server;
var startTime = Sys.time();
private function main() {
var host = "localhost";
var port = 8080;
var root:String = null;
var quiet = false;
var uv = false;
var maxConnections = 20;
var usage:String = null;
var argHandler = Args.generate([@doc("Address to bind")
["--host"] => (name:String) -> host = name, @doc("Port to bind")
["--port"] => (number:Int) -> {
if (number < 1 || number > 65535)
exit('Port number out of range');
port = number;
}, @doc("Filesystem root")
["--root"] => (path:String) -> {
if (!FileSystem.exists(path) || !FileSystem.isDirectory(path))
exit('Root path not found');
root = path;
},
#if hl
@doc("Use libuv") ["--uv"] => (connections:Int) -> {
maxConnections = connections;
uv = true;
},
#end
@doc("Disable logging to stdout")
["--quiet"] => () -> quiet = true, @doc("Print this help")
["--help"] => () -> exit(usage),
_ => arg -> exit(1, 'Unknown argument [$arg]\n')
]);
usage = 'Usage: wtri [options]\n\n' + argHandler.getDoc();
argHandler.parse(Sys.args());
if (root == null)
root = Sys.getCwd();
wtri.Response.defaultHeaders.set('server', 'wtri');
/*
var wsHandler = new wtri.handler.WebSocketHandler();
wsHandler.onconnect = client -> {
trace("Websocket client connected",wsHandler.clients.length, client.socket.peer().host );
client.onmessage = m -> {
trace("Websocket client message: "+m);
if( m != null ) {
var str = m.toString();
switch str {
case 'quit':
client.close();
case _:
wsHandler.broadcast( m );
}
}
}
client.ondisconnect = () -> {
trace("Websocket client disconnected",wsHandler.clients.length);
}
client.write("Welcome!");
}
*/
var handlers:Array<wtri.Handler> = [
// wsHandler,
new wtri.handler.FileSystemHandler(root, true),
new wtri.handler.ContentEncoding(["deflate" => b -> return haxe.zip.Compress.run(b,
9)]) // new wtri.handler.ContentEncoding(["deflate" => b -> return format.tools.Deflate.run(b)])
];
log('Starting server http://$host:$port');
server = new wtri.Server((req, res) -> {
// res.end( 'Hello!' );
/*
if( req.path == "/favicon.ico" ) {
res.redirect('/favicon.svg');
}
*/
for (h in handlers)
h.handle(req, res);
if (!res.finished)
res.end();
if (!quiet) {
if (Std.isOfType(req.socket, TCPSocket)) {
var tcp:wtri.net.Socket.TCPSocket = cast req.socket;
var peer = tcp.socket.peer();
log('${peer.host} - ${req.method} - ${res.code} - ${req.path}');
} else {
log('${req.method} - ${res.code} - ${req.path}');
}
}
});
try {
server.listen(port, host, uv, maxConnections);
} catch (e) {
Sys.stderr().writeString(e.message);
Sys.exit(1);
}
}
function log(str:String) {
Sys.stdout().writeString(Date.now().toString() + ' - $str\n');
}
function exit(code = 0, ?msg:String) {
if (msg != null) {
switch code {
case 0:
Sys.stdout().writeString('$msg\n');
case _:
Sys.stderr().writeString('$msg\n');
}
}
Sys.exit(code);
}