~subsetpark/whist

ref: ce6d559ad77293610211f21b8297b0acdddd859d whist/main.janet -rw-r--r-- 1.0 KiB
ce6d559a — Zach Smith Complete whist 7 months ago
                                                                                
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
## main.janet
(import json)
(import init)
(import whist)

(use joy)

(route :get "/api/v1/whist/config" :config)
(route :get "/api/v1/whist/init" :init)
(route :post "/api/v1/whist/next" :next)

## Config Handler
(defn config [request]
  (def resp (init/config))
  (application/json resp))

## Init Handler
(defn init [request]
  (let [players (get-in request [:query-string :players])
	[p1 p2 p3 p4] (string/split "," players)
	resp (init/init p1 p2 p3 p4)]
  
    (application/json resp)))

## Next Handler
(def- params
  (params :next
    (validates [:action] :required true)
    (permit [:action :state :players])))

(defn next [request]
  (def handler (fiber/new (fn [req]
			    (whist/next (params req))) :e))
  (match (resume handler request)
    {:error error-msg} @{:status 422
			 :body (json/encode {:error error-msg})
			 :headers @{"Content-Type" "application/json"}}
    [state events]
    (application/json {:state state :events events})))


(def app (app {:layout layout :csrf-token false}))

(defn main [& args]
  (server app 9001))