0 | module HTTP.API.Server.Interface
2 | import public HTTP.API
3 | import public HTTP.Prog
4 | import public HTTP.Request
5 | import public HTTP.Response
13 | interface Serve (0 a : Type) where
17 | 0 Constraint : a -> Type
21 | 0 InTypes : a -> List Type
25 | 0 OutTypes : a -> List Type
30 | outs : (v : a) -> TList (OutTypes v)
42 | canHandle : (v : a) -> (con : Constraint v) => Request -> Bool
48 | -> {auto con : Constraint v}
50 | -> Handler (HList $
InTypes v)
55 | -> {auto con : Constraint v}
56 | -> HList (OutTypes v)
68 | 0 Constraints : All Serve ts => (endpoint : HList ts) -> List Type
69 | Constraints @{[]} [] = []
70 | Constraints @{_::_} (v::vs) = Constraint v :: Constraints vs
75 | 0 AllInTypes : All Serve ts => (endpoint : HList ts) -> List Type
76 | AllInTypes @{[]} [] = []
77 | AllInTypes @{_::_} (v::vs) = InTypes v ++ AllInTypes vs
82 | 0 AllOutTypes : All Serve ts => (endpoint : HList ts) -> List Type
83 | AllOutTypes @{[]} [] = []
84 | AllOutTypes @{_::_} (v::vs) = OutTypes v ++ AllOutTypes vs
88 | allOuts : All Serve ts => (endpoint : HList ts) -> TList (AllOutTypes endpoint)
89 | allOuts @{[]} [] = []
90 | allOuts @{_::_} (v::vs) = outs v ++ allOuts vs
94 | 0 EndpointHandler : All Serve ts => (endpoint : HList ts) -> Type
95 | EndpointHandler vs =
96 | Fun (AllInTypes vs) (Handler (ResultType $
AllOutTypes vs))
106 | {auto all : All Serve ts}
107 | -> (endpoint : HList ts)
108 | -> {auto con : HList (Constraints endpoint)}
110 | canServe @{[]} [] @{[]} req = True
111 | canServe @{_::_} (v::vs) @{_::_} req = canHandle v req && canServe vs req
116 | {auto all : All Serve ts}
117 | -> (endpoint : HList ts)
118 | -> {auto con : HList (Constraints endpoint)}
120 | -> Handler (HList $
AllInTypes endpoint)
121 | getIns @{[]} [] @{[]} req = pure []
122 | getIns @{_::_} (v::vs) @{_::_} req = Prelude.do
123 | rs <- fromRequest v req
124 | rem <- getIns vs req
130 | {auto all : All Serve ts}
131 | -> (endpoint : HList ts)
132 | -> {auto con : HList (Constraints endpoint)}
133 | -> HList (AllOutTypes endpoint)
136 | -> Handler Response
137 | putOuts @{[]} [] [] @{[]} req resp = pure resp
138 | putOuts @{_::_} (x::xs) vs @{_::_} req resp = Prelude.do
139 | let (ts,rem) := splitHList (outs x) vs
140 | r2 <- adjResponse x ts req resp
141 | putOuts xs rem req r2
160 | {auto all : All Serve ts}
161 | -> (endpoint : HList ts)
162 | -> {auto con : HList (Constraints endpoint)}
163 | -> EndpointHandler endpoint
165 | -> Handler Response
166 | serveEndpoint endpoint f req = Prelude.do
167 | ins <- getIns endpoint req
168 | outs <- applyHList ins f
169 | putOuts endpoint (wrapResult (allOuts endpoint) outs) req empty