-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.lean
62 lines (53 loc) · 1.8 KB
/
Main.lean
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
52
53
54
55
56
57
58
59
60
61
62
import Rinha
import Ash
open Ash.App
open Ash
open Rinha
open Rinha.Entities
open Rinha.Environment
/--
Rinha de backend basic application monad
-/
def app (db: Pgsql.Connection) : Ash.App Unit := do
post "/pessoas" $ λ conn => do
let person : Option Person := conn.json
match person with
| none => conn.unprocessableEntity "Invalid JSON"
| some person =>
let res ← person.create! db
match res with
| none => conn.unprocessableEntity "Already exists."
| some person => do
match person.id with
| none => conn.unprocessableEntity "Invalid id (?)."
| some id => do
let location := s!"/pessoas/{id}"
conn.created "" location
get "/pessoas/:id" $ λ conn => do
match conn.bindings.find? "id" with
| none => conn.badRequest "Bad Request"
| some query =>
match (← findById query db) with
| some person => conn.ok person
| none => conn.notFound ""
get "/pessoas" $ λ conn => do
match conn.query.find? "t" with
| none => conn.badRequest "Bad Request"
| some query => conn.ok (← findLike query db)
get "/contagem-pessoas" $ λ conn => do
let count ← countPeople db
conn.ok s!"{count}"
/--
Rinha de backend entrypoint
-/
def main : IO Unit := do
-- Read the environment from environment variables,
-- but if they are not set, use the default values.
let env <- readEnvironment
-- Connects to the database using the environment variables.
let conn ← Pgsql.connect $ env.postgres.toConnectionString
let app := app conn
IO.println s!"INFO: Database connection set up"
-- Run the application with the environment variables host and port.
app.run env.host env.port do
IO.println s!"INFO: Server running on {env.host}:{env.port}"