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
|