0 | ||| Define the core of the server.
 1 | module Pact.Server.Core
 2 |
 3 | import Pact.API
 4 | import Pact.WAI.HTTPErr
 5 |
 6 | import Data.Vect
 7 | import FS.Socket
 8 | import JSON.FromJSON
 9 | import Control.Monad.Either
10 | import Control.Monad.Reader
11 |
12 | ||| Handler is a type that represents a handler function
13 | public export
14 | Handler : Type -> Type
15 | Handler = EitherT HTTPErr IO
16 |
17 | -- 在 Core.idr 中添加
18 | public export
19 | interface Hoistable (m : Type -> Type) where
20 |   hoist : m a -> Handler a
21 |
22 | -- 基本实现
23 | public export
24 | implementation Hoistable IO where
25 |   hoist = liftIO
26 |
27 | public export
28 | implementation Hoistable Handler where
29 |   hoist = id
30 |
31 | ||| Route record type
32 | ||| Associates an API definition with its handler function
33 | public export
34 | record RouteItem (m : Type -> Type) where
35 |   constructor (:=>)
36 |   ||| API definition, describes path and endpoint
37 |   routeApi: API
38 |   ||| Handler function, type is determined by the API definition
39 |   routeHandler : GetHandlerType m routeApi
40 |   { auto mimeRenderProof : MimeRender (VerbAccept routeApi.verb) (VerbResponse routeApi.verb) }
41 |   { auto reqBodyProof : FromJSON (ApiReqBody routeApi)}
42 |
43 |
44 | public export
45 | GetEndpointTypeFromRouteItem : (m : Type -> Type) -> RouteItem m -> Type
46 | GetEndpointTypeFromRouteItem m (api :=> handler) = GetEPFromAPI m api
47 |
48 | ||| Server data type
49 | ||| Contains a set of route definitions for handling HTTP requests
50 | public export
51 | data Router: (m : Type -> Type) -> Type where
52 |   ||| Creates a server instance containing a list of routes
53 |   MkRouter : Hoistable m => (routes : List (RouteItem m)) -> Router m
54 |
55 |
56 | ||| ServerConfig is a record type that contains server configuration
57 | ||| 
58 | ||| Contains server bind address and max connection count
59 | ||| @ return ServerConfig
60 | public export
61 | record ServerConfig where
62 |   constructor MkServerConfig
63 |   ||| Server bind IP address and port
64 |   bind : IP4Addr
65 |   ||| Max allowed connections
66 |   maxConns : Nat
67 |
68 |
69 | ||| defaultConfig is a function that returns the default server configuration
70 | ||| 
71 | ||| Binds to 127.0.0.1:2222, max connections 128
72 | ||| @ return ServerConfig
73 | public export
74 | defaultConfig : ServerConfig
75 | defaultConfig = MkServerConfig  {
76 |     bind = IP4 [127,0,0,1] 2222,
77 |     maxConns = 128
78 |   }
79 |