-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroutes.knot
More file actions
37 lines (31 loc) · 1.35 KB
/
Copy pathroutes.knot
File metadata and controls
37 lines (31 loc) · 1.35 KB
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
with {
data Priority = Low {} | Medium {} | High {} | Critical {}
type Todo = {title: Text, owner: Text, priority: Priority}
*todos : [Todo]
api Api where
GetTodos get /todos/{owner: Text} @{xForwardedFor: Maybe Text} -> [Todo]
CreateTodo post /todos ={title: Text, owner: Text, priority: Text} -> Todo
GetCount get /admin/count -> Int 1
addTodo (\title owner priority -> do
todos <- full *todos
*todos = base.union todos [{title title owner owner priority (Priority.Low {})}])
getTodos (\owner -> do
todos <- full *todos
with {result (do t <- todos; where t.owner == owner; yield t)} yield result)
srv (serve Api where
GetTodos = \{owner owner xForwardedFor xForwardedFor} -> do
case xForwardedFor of
Maybe.Just {value ip} -> base.logInfo ("GetTodos from " ++ ip)
Maybe.Nothing {} -> base.logInfo "GetTodos (no proxy header)"
todos <- getTodos owner
case base.count todos == 0 of
Bool.True {} -> yield (Result.Err {error {status 404 message "no todos found for owner"}})
Bool.False {} -> yield (Result.Ok {value todos})
CreateTodo = \{title title owner owner priority priority} -> do
addTodo title owner priority
yield (Result.Ok {value {title title owner owner priority (Priority.Low {})}})
GetCount = \{} -> do
todos <- full *todos
yield (Result.Ok {value (base.count todos)}))
}
(base.listen 8080 srv)