forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.nula
More file actions
51 lines (45 loc) · 1.38 KB
/
Copy pathhttp_server.nula
File metadata and controls
51 lines (45 loc) · 1.38 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
// HTTP Server — an actor wrapping a TCP socket with HTTP request/response
// framing. Demonstrates actor state, message passing, and the connection
// lifecycle pattern.
//
// Run with: nulang examples/http_server.nula
actor HttpServer {
state port: Int = 8080
state running: Bool = false
state request_count: Int = 0
behavior start() {
// Bind and begin accepting connections
self.running = true
self.request_count = 0
perform IO.print("HTTP server listening on port " + perform Int.to_string(self.port))
}
behavior handle_request(method: String, path: String) {
// Process an incoming request
self.request_count = self.request_count + 1
let response = if method == "GET" then
"200 OK: " + path
else
"405 Method Not Allowed"
in
perform IO.print(response)
}
behavior stats() {
perform IO.print(
"Requests handled: " + perform Int.to_string(self.request_count)
)
}
behavior stop() {
self.running = false
perform IO.print("Server stopped")
}
}
fn main() {
let server = spawn HttpServer {}
server ! start()
server ! handle_request("GET", "/index.html")
server ! handle_request("GET", "/api/data")
server ! handle_request("POST", "/api/data")
server ! stats()
server ! stop()
0
}