0 | module HTTP.API.Server.Interface
2 | import public HTTP.API
3 | import public HTTP.I18n
4 | import public HTTP.Prog
5 | import public HTTP.Request
6 | import public HTTP.Response
14 | interface Serve (0 a : Type) where
18 | 0 Constraint : a -> Type
22 | 0 InTypes : a -> List Type
26 | 0 OutTypes : a -> List Type
31 | outs : (v : a) -> TList (OutTypes v)
43 | canHandle : (v : a) -> (con : Constraint v) => Request -> Bool
49 | -> {auto con : Constraint v}
51 | -> Handler (HList $
InTypes v)
56 | -> {auto con : Constraint v}
57 | -> HList (OutTypes v)
69 | 0 Constraints : All Serve ts => (endpoint : HList ts) -> List Type
70 | Constraints @{[]} [] = []
71 | Constraints @{_::_} (v::vs) = Constraint v :: Constraints vs
76 | 0 AllInTypes : All Serve ts => (endpoint : HList ts) -> List Type
77 | AllInTypes @{[]} [] = []
78 | AllInTypes @{_::_} (v::vs) = InTypes v ++ AllInTypes vs
83 | 0 AllOutTypes : All Serve ts => (endpoint : HList ts) -> List Type
84 | AllOutTypes @{[]} [] = []
85 | AllOutTypes @{_::_} (v::vs) = OutTypes v ++ AllOutTypes vs
89 | allOuts : All Serve ts => (endpoint : HList ts) -> TList (AllOutTypes endpoint)
90 | allOuts @{[]} [] = []
91 | allOuts @{_::_} (v::vs) = outs v ++ allOuts vs
95 | 0 EndpointHandler : All Serve ts => (endpoint : HList ts) -> Type
96 | EndpointHandler vs =
97 | Fun (AllInTypes vs) (Handler (ResultType $
AllOutTypes vs))
107 | {auto all : All Serve ts}
108 | -> (endpoint : HList ts)
109 | -> {auto con : HList (Constraints endpoint)}
111 | canServe @{[]} [] @{[]} req = True
112 | canServe @{_::_} (v::vs) @{_::_} req = canHandle v req && canServe vs req
117 | {auto all : All Serve ts}
118 | -> (endpoint : HList ts)
119 | -> {auto con : HList (Constraints endpoint)}
121 | -> Handler (HList $
AllInTypes endpoint)
122 | getIns @{[]} [] @{[]} req = pure []
123 | getIns @{_::_} (v::vs) @{_::_} req = Prelude.do
124 | rs <- fromRequest v req
125 | rem <- getIns vs req
131 | {auto all : All Serve ts}
132 | -> (endpoint : HList ts)
133 | -> {auto con : HList (Constraints endpoint)}
134 | -> HList (AllOutTypes endpoint)
137 | -> Handler Response
138 | putOuts @{[]} [] [] @{[]} req resp = pure resp
139 | putOuts @{_::_} (x::xs) vs @{_::_} req resp = Prelude.do
140 | let (ts,rem) := splitHList (outs x) vs
141 | r2 <- adjResponse x ts req resp
142 | putOuts xs rem req r2
161 | {auto all : All Serve ts}
162 | -> (endpoint : HList ts)
163 | -> {auto con : HList (Constraints endpoint)}
164 | -> EndpointHandler endpoint
166 | -> Handler Response
167 | serveEndpoint endpoint f req = Prelude.do
168 | ins <- getIns endpoint req
169 | outs <- applyHList ins f
170 | putOuts endpoint (wrapResult (allOuts endpoint) outs) req empty