-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelloDBServer.elm
57 lines (48 loc) · 1.67 KB
/
HelloDBServer.elm
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
module HelloDBServer exposing (main)
import Database.Postgres exposing (WhereCondition(..))
import Error
import File
import Json.Decode
import Person
import Response
import Result.Extra
import Server exposing (Config, Flags, Method(..), Request, Response)
import Status exposing (Status(..))
main : Server.Program
main =
Server.program
{ init = init
, handler = handler
}
init : Flags -> Config
init _ =
Server.baseConfig
|> Database.Postgres.connect
{ hostname = "localhost"
, port_ = 5432
, database = "postgres"
, user = "postgres"
, password = "postgres"
}
handler : Request -> Response
handler request =
case ( Server.getMethod request, Server.getPath request ) of
( Get, [] ) ->
File.load "./examples/hello-db-client.html"
|> Server.onSuccess
(Json.Decode.decodeValue Json.Decode.string
>> Result.map
(\file ->
Response.ok
|> Response.setBody file
|> Server.respond request
)
>> Result.mapError
(Json.Decode.errorToString >> Response.error >> Server.respond request)
>> Result.Extra.merge
)
|> Server.onError (Error.toString >> Response.error >> Server.respond request)
( _, "persons" :: restOfPath ) ->
Person.handler request restOfPath
_ ->
Server.respond request Response.notFound