mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
46 lines
1.1 KiB
Plaintext
46 lines
1.1 KiB
Plaintext
// This is just proof of concept on what the language
|
|
// might look like in the future
|
|
|
|
import http from "http"
|
|
|
|
// Define a custom type to represent a user
|
|
type User =
|
|
id: number,
|
|
name: string,
|
|
end
|
|
|
|
// Define a function to handle incoming HTTP requests
|
|
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
|