1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00
bobbylisp/examples/ht.hlm

46 lines
1.1 KiB
Plaintext
Raw Normal View History

2022-12-20 08:21:21 -06:00
// This is just proof of concept on what the language
// might look like in the future
2022-12-13 10:02:41 -06:00
import http from "http"
2022-12-20 08:21:21 -06:00
// Define a custom type to represent a user
2022-12-13 10:02:41 -06:00
type User =
id: number,
name: string,
end
2022-12-20 08:21:21 -06:00
// Define a function to handle incoming HTTP requests
2022-12-13 10:02:41 -06:00
fun handle_request
req: http.IncomingMessage,
res: http.ServerResponse,
= do
let user_id = req.url.split("/")[1]
let name =
match user_id
| 12345 -> Some("John Smith")
| 727 -> Some("Foo Bar")
else None
match name
| Some name -> do
let user = User(user_id, name)
res.statusCode = 200
res.setHeader("Content-Type", "application/json")
res.write(JSON.stringify(user))
end
| None -> do
res.statusCode = 404
res.write("User not found")
end
res.end()
end
fun main = do
let
port = 8080,
server = http.createServer(handle_request),
in
server.listen(port, fun -> println("HTTP server listening on port 8080"))
end