|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE FlexibleContexts #-} |
| 3 | +{-# LANGUAGE FlexibleInstances #-} |
| 4 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 5 | +{-# LANGUAGE KindSignatures #-} |
| 6 | +{-# LANGUAGE LambdaCase #-} |
| 7 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 8 | +{-# LANGUAGE OverloadedStrings #-} |
| 9 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 10 | +{-# LANGUAGE TypeOperators #-} |
| 11 | +{-# LANGUAGE TypeFamilies #-} |
| 12 | +{-# LANGUAGE DefaultSignatures #-} |
| 13 | +{-# LANGUAGE StandaloneDeriving #-} |
| 14 | +{-# LANGUAGE NamedFieldPuns #-} |
| 15 | +module Solga.Core |
| 16 | + ( -- * Path components |
| 17 | + type (:>), type (/>) |
| 18 | + , Get |
| 19 | + , Post |
| 20 | + , JSON(..) |
| 21 | + , Raw(..) |
| 22 | + , RawResponse(..) |
| 23 | + , End(..) |
| 24 | + , WithIO(..) |
| 25 | + , Seg(..) |
| 26 | + , OneOfSegs(..) |
| 27 | + , Capture(..) |
| 28 | + , Method(..) |
| 29 | + , HeaderName |
| 30 | + , Header |
| 31 | + , ResponseHeaders |
| 32 | + , ExtraHeaders(..) |
| 33 | + , NoCache(..) |
| 34 | + , ReqBodyJSON(..) |
| 35 | + , MultiPartParam |
| 36 | + , MultiPartFile |
| 37 | + , MultiPartFileInfo(..) |
| 38 | + , MultiPartData |
| 39 | + , ReqBodyMultipart(..) |
| 40 | + , Endpoint |
| 41 | + , (:<|>)(..) |
| 42 | + ) where |
| 43 | + |
| 44 | +import GHC.TypeLits |
| 45 | +import Data.ByteString (ByteString) |
| 46 | +import Data.CaseInsensitive (CI) |
| 47 | + |
| 48 | +--------------------------------------------------- |
| 49 | + |
| 50 | +-- | Compose routers. This is just type application, |
| 51 | +-- ie.: @Foo :> Bar :> Baz == Foo (Bar Baz)@ |
| 52 | +type f :> g = f g |
| 53 | +infixr 2 :> |
| 54 | + |
| 55 | +-- | Serve a given WAI `Wai.Application`. |
| 56 | +newtype Raw a = Raw { rawApp :: a } |
| 57 | + |
| 58 | +-- | Serve a given WAI `Wai.Response`. |
| 59 | +newtype RawResponse a = RawResponse { rawResponse :: a } |
| 60 | + |
| 61 | +-- | Only accept the end of a path. |
| 62 | +newtype End next = End { endNext :: next } |
| 63 | + |
| 64 | +-- | Match a constant directory in the path. |
| 65 | +-- |
| 66 | +-- When specifying APIs, use the `/>` combinator to specify sub-paths: |
| 67 | +-- @"foo" `/>` `JSON` Bar@ |
| 68 | +newtype Seg (seg :: Symbol) next = Seg { segNext :: next } |
| 69 | + deriving (Eq, Ord, Show) |
| 70 | + |
| 71 | +-- | Match a path, segment, e.g @"foo" `/>` `JSON` Bar@ |
| 72 | +type seg /> g = Seg seg :> g |
| 73 | +infixr 2 /> |
| 74 | + |
| 75 | +-- | Try to route with @left@, or try to route with @right@. |
| 76 | +data left :<|> right = (:<|>) { altLeft :: left, altRight :: right } |
| 77 | + deriving (Eq, Ord, Show) |
| 78 | + |
| 79 | +infixr 1 :<|> |
| 80 | + |
| 81 | +-- | Match any of a set of path segments. |
| 82 | +data OneOfSegs (segs :: [ Symbol ]) next = OneOfSegs { oneOfSegsNext :: next } |
| 83 | + |
| 84 | +-- | Capture a path segment and pass it on. |
| 85 | +newtype Capture a next = Capture { captureNext :: a -> next } |
| 86 | + |
| 87 | +-- | Accepts requests with a certain method. |
| 88 | +newtype Method (method :: Symbol) next = Method { methodNext :: next } |
| 89 | + deriving (Eq, Ord, Show) |
| 90 | + |
| 91 | +-- | Return a given JSON object |
| 92 | +newtype JSON a = JSON { jsonResponse :: a } |
| 93 | + deriving (Eq, Ord, Show) |
| 94 | + |
| 95 | +type HeaderName = CI ByteString |
| 96 | +type Header = (HeaderName, ByteString) |
| 97 | +type ResponseHeaders = [Header] |
| 98 | + |
| 99 | +-- | Set extra headers on responses. |
| 100 | +-- Existing headers will be overriden if specified here. |
| 101 | +data ExtraHeaders next = ExtraHeaders |
| 102 | + { extraHeaders :: ResponseHeaders |
| 103 | + , extraHeadersNext :: next |
| 104 | + } |
| 105 | + |
| 106 | +-- | Prevent caching for sub-routers. |
| 107 | +newtype NoCache next = NoCache { noCacheNext :: next } |
| 108 | + |
| 109 | +-- | Parse a JSON request body. |
| 110 | +newtype ReqBodyJSON a next = ReqBodyJSON { reqBodyJSONNext :: a -> next } |
| 111 | + |
| 112 | +-- | Produce a response with `IO`. |
| 113 | +newtype WithIO next = WithIO { withIONext :: IO next } |
| 114 | + |
| 115 | +type MultiPartParam = (ByteString, ByteString) |
| 116 | +type MultiPartFile y = (ByteString, MultiPartFileInfo y) |
| 117 | + |
| 118 | +data MultiPartFileInfo c = MultiPartFileInfo |
| 119 | + { mpfiName :: ByteString |
| 120 | + , mpfiContentType :: ByteString |
| 121 | + , mpfiContent :: FilePath |
| 122 | + } |
| 123 | + |
| 124 | +-- | A parsed "multipart/form-data" request. |
| 125 | +type MultiPartData y = ([MultiPartParam], [MultiPartFile y]) |
| 126 | + |
| 127 | +-- | Accept a "multipart/form-data" request. |
| 128 | +-- Files will be stored in a temporary directory and will be deleted |
| 129 | +-- automatically after the request is processed. |
| 130 | +data ReqBodyMultipart y a next = ReqBodyMultipart |
| 131 | + { reqMultiPartParse :: MultiPartData y -> Either String a |
| 132 | + , reqMultiPartNext :: a -> next |
| 133 | + } |
| 134 | + |
| 135 | +-- | Useful synonym for dynamic endpoints: accept requests with a given method, compute a JSON response in `IO` and don't cache. |
| 136 | +type Endpoint method a = End :> NoCache :> Method method :> WithIO :> a |
| 137 | + |
| 138 | +-- | Handle a "GET" request and produce a "JSON" response, with `IO`. |
| 139 | +type Get a = Endpoint "GET" (JSON a) |
| 140 | +-- | Handle a "POST" request and produce a "JSON" response, with `IO`. |
| 141 | +type Post a = Endpoint "POST" (JSON a) |
| 142 | + |
0 commit comments