Repository navigation
Expand file tree
/
Copy pathhello-web.roc
More file actions
44 lines (36 loc) · 1.61 KB
/
Copy pathhello-web.roc
File metadata and controls
44 lines (36 loc) · 1.61 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
## Uses host-owned operational telemetry and responds with a simple HTML greeting.
app [Context, program] {
http: "https://github.com/roc-lang/http/releases/download/1.0.0/6ZUwqYhCS8PU9Mo6MF7oV82ET2o7KYb57CLKDq4cq4sS.tar.zst",
pf: platform "https://github.com/roc-lang/basic-webserver/releases/download/0.20.0/7dR2ewuEbpPQXTAZL3aH62weBwpNNjbCJtyyiRsufTNZ.tar.zst",
roc: "nightly-2026-11-07-ad3a5d2",
}
import pf.Server
import pf.Stdout
import pf.UnixTime
import http.Response
# `init!` produces this immutable context once, and every request receives it.
Context : {}
program = { init!, respond!, shutdown! }
# `init!` can validate configuration, run migrations, or prepare immutable
# startup data. This example has no startup data, so its context is `{}`.
init! : () => Try({ config : Server.Config, context : Context }, [Exit(I64)])
init! = || {
config = Server.default_config
.with_access_log(
Server.json_lines_access_log({
target: Server.path_without_query,
max_buffered_events: 128,
}),
)
.with_metrics(Server.open_metrics({ at: "/metrics" }))
Ok({ config, context: {} })
}
respond! : Server.Request, Context => Try(Server.Outcome, [ServerErr(Str)])
respond! = |request, _context| {
seconds = UnixTime.now!().seconds_since_epoch()
Stdout.line!("${seconds.to_str()} ${Str.inspect(request.method())} ${Str.inspect(request.target())}")
? |err| ServerErr("Failed to log request: ${Str.inspect(err)}")
Ok(Server.respond(Response.from_status(200).with_body(Str.to_utf8("<b>Hello from server</b><br>"))))
}
shutdown! : Server.ShutdownReason, Context => Try({}, [Exit(I64)])
shutdown! = |_reason, _context| Ok({})