2 Module : Gargantext.API.Server
3 Description : REST API declaration
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# LANGUAGE ScopedTypeVariables #-}
13 ---------------------------------------------------------------------
14 module Gargantext.API.Server where
15 ---------------------------------------------------------------------
16 import Control.Lens ((^.))
17 import Control.Monad.Except (withExceptT)
18 import Control.Monad.Reader (runReaderT)
19 import Data.Text (Text)
20 import Data.Version (showVersion)
22 import Servant.Swagger.UI (swaggerSchemaUIServer)
23 import qualified Data.ByteString.Lazy.Char8 as BL8
24 import qualified Paths_gargantext as PG -- cabal magic build module
26 import qualified Gargantext.API.Public as Public
28 import Gargantext.API.Admin.Auth.Types (AuthContext)
29 import Gargantext.API.Admin.Auth (auth)
30 import Gargantext.API.Admin.FrontEnd (frontEndServer)
31 import Gargantext.API.Prelude
32 import Gargantext.API.Routes
33 import Gargantext.API.Swagger (swaggerDoc)
34 import Gargantext.API.ThrowAll (serverPrivateGargAPI)
35 import Gargantext.Prelude
36 import Gargantext.Prelude.Config (gc_url_backend_api)
37 import Gargantext.Database.Prelude (hasConfig)
40 serverGargAPI :: MimeRender JSON err => Text -> GargServerM env err GargAPI
41 serverGargAPI baseUrl -- orchestrator
44 :<|> serverPrivateGargAPI
45 :<|> Public.api baseUrl
49 gargVersion :: GargServer GargVersion
50 gargVersion = pure (cs $ showVersion PG.version)
52 -- | Server declarations
53 server :: forall env. EnvC env => env -> IO (Server API)
55 -- orchestrator <- scrapyOrchestrator env
56 pure $ swaggerSchemaUIServer swaggerDoc
57 :<|> hoistServerWithContext
58 (Proxy :: Proxy GargAPI)
59 (Proxy :: Proxy AuthContext)
61 (serverGargAPI (env ^. hasConfig . gc_url_backend_api))
64 transform :: forall a. GargM env GargError a -> Handler a
65 transform = Handler . withExceptT showAsServantErr . (`runReaderT` env)
68 showAsServantErr :: GargError -> ServerError
69 showAsServantErr (GargServerError err) = err
70 showAsServantErr a = err500 { errBody = BL8.pack $ show a }