|
| 1 | +module Web.Template.Servant.API where |
| 2 | + |
| 3 | +import Control.Lens ((?~)) |
| 4 | +import Data.Function ((&)) |
| 5 | +import Data.OpenApi (applyTags, description) |
| 6 | +import Data.Proxy (Proxy (..)) |
| 7 | +import Data.String (fromString) |
| 8 | +import Data.Text (pack) |
| 9 | +import GHC.TypeLits (AppendSymbol, KnownSymbol, Symbol, symbolVal) |
| 10 | + |
| 11 | +import Control.Monad.IO.Class (MonadIO (..)) |
| 12 | +import Control.Monad.Trans.Resource (runResourceT) |
| 13 | +import Network.Wai (Request, Response, ResponseReceived) |
| 14 | +import Servant (HasServer (..), Raw, (:>)) |
| 15 | +import Servant.OpenApi (HasOpenApi (..)) |
| 16 | +import Servant.Server.Internal (RouteResult (..), Router' (..), runDelayed, runHandler) |
| 17 | + |
| 18 | +-- | Prepend version to every sub-route. |
| 19 | +-- |
| 20 | +-- > type API = Version "3" :> ("route1" :<|> "route2") |
| 21 | +type Version (v :: Symbol) = AppendSymbol "v" v |
| 22 | + |
| 23 | +-- | Mark sub-api with a Swagger tag with description. |
| 24 | +-- |
| 25 | +-- > type API |
| 26 | +-- > = (Tag "foo" "Some Foo routes" :> ("foo1" :<|> "foo2")) |
| 27 | +-- > :<|> (Tag "bar" "Some Bar routes" :> ("bar1" :<|> "bar2")) |
| 28 | +data Tag (tag :: Symbol) (descr :: Symbol) |
| 29 | + |
| 30 | +instance HasServer api context => HasServer (Tag tag descr :> api) context where |
| 31 | + type ServerT (Tag tag descr :> api) m = ServerT api m |
| 32 | + |
| 33 | + route _ = route @api Proxy |
| 34 | + hoistServerWithContext _ = hoistServerWithContext @api Proxy |
| 35 | + |
| 36 | +instance (KnownSymbol tag, KnownSymbol descr, HasOpenApi api) => HasOpenApi (Tag tag descr :> api) where |
| 37 | + toOpenApi _ = toOpenApi @api Proxy |
| 38 | + & applyTags [fromString (symbolVal @tag Proxy) & description ?~ pack (symbolVal @descr Proxy)] |
| 39 | + |
| 40 | +-- | As 'Raw', but with access to the custom monad @m@. |
| 41 | +-- |
| 42 | +-- See <https://github.com/haskell-servant/servant/pull/1349>. |
| 43 | +data RawM |
| 44 | + |
| 45 | +instance HasServer RawM ctx where |
| 46 | + type ServerT RawM m = Request -> (Response -> IO ResponseReceived) -> m ResponseReceived |
| 47 | + |
| 48 | + hoistServerWithContext _ _ nt m = \request respond -> nt $ m request respond |
| 49 | + |
| 50 | + route _ _ dApp = RawRouter $ \env request respond -> runResourceT $ do |
| 51 | + r <- runDelayed dApp env request |
| 52 | + liftIO $ case r of |
| 53 | + Fail a -> respond $ Fail a |
| 54 | + FailFatal e -> respond $ FailFatal e |
| 55 | + Route appH -> do |
| 56 | + r' <- runHandler $ appH request (respond . Route) |
| 57 | + -- appH may return result with 'Right' _only_ by calling smth like @liftIO . respond@, |
| 58 | + -- so in case of 'Left' we may suppose that 'respond' was never called. |
| 59 | + case r' of |
| 60 | + Left e -> respond $ FailFatal e |
| 61 | + Right x -> return x |
| 62 | + |
| 63 | +instance HasOpenApi RawM where |
| 64 | + toOpenApi _ = toOpenApi @Raw Proxy |
0 commit comments