-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.pony
More file actions
85 lines (73 loc) · 2.34 KB
/
Copy pathmain.pony
File metadata and controls
85 lines (73 loc) · 2.34 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
use stallion = "../../stallion"
use uri = "uri"
use lori = "lori"
actor Main
new create(env: Env) =>
let auth = lori.TCPListenAuth(env.root)
Listener(auth, "0.0.0.0", "8080", env.out)
actor Listener is lori.TCPListenerActor
"""
TCP listener that creates `HelloServer` actors for each connection.
"""
var _tcp_listener: lori.TCPListener = lori.TCPListener.none()
let _out: OutStream
let _config: stallion.ServerConfig
let _server_auth: lori.TCPServerAuth
new create(
auth: lori.TCPListenAuth,
host: String,
port: String,
out: OutStream)
=>
_out = out
_server_auth = lori.TCPServerAuth(auth)
_config = stallion.ServerConfig(host, port)
_tcp_listener = lori.TCPListener(auth, host, port, this)
fun ref _listener(): lori.TCPListener => _tcp_listener
fun ref _on_accept(fd: U32): lori.TCPConnectionActor =>
HelloServer(_server_auth, fd, _config)
fun ref _on_listening() =>
try
(let host, let port) = _tcp_listener.local_address().name()?
_out.print("Server listening on " + host + ":" + port)
else
_out.print("Server listening")
end
fun ref _on_listen_failure() =>
_out.print("Failed to start server")
fun ref _on_closed() =>
_out.print("Server closed")
actor HelloServer is stallion.HTTPServerActor
"""
Responds with a personalized greeting using an optional query parameter.
"""
var _http: stallion.HTTPServer = stallion.HTTPServer.none()
new create(
auth: lori.TCPServerAuth,
fd: U32,
config: stallion.ServerConfig)
=>
_http = stallion.HTTPServer(auth, fd, this, config)
fun ref _http_connection(): stallion.HTTPServer => _http
fun ref on_request_complete(
request': stallion.Request val,
responder: stallion.Responder)
=>
"""
Extracts a `name` query parameter and responds with a greeting.
"""
var name: String val = "World"
match request'.uri.query_params()
| let params: uri.FormURLEncoded val =>
match params.get("name")
| let n: String => name = n
end
end
let resp_body: String val = "Hello, " + name + "!"
let response = stallion.ResponseBuilder(stallion.StatusOK)
.add_header("Content-Type", "text/plain")
.add_header("Content-Length", resp_body.size().string())
.finish_headers()
.add_chunk(resp_body)
.build()
responder.respond(response)