2 Module : Gargantext.API
3 Description : REST API declaration
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Main (RESTful) API of the instance Gargantext.
12 The Garg-API is typed to derive the documentation, the mock and tests.
14 This API is indeed typed in order to be able to derive both the server
17 The Garg-API-Monad enables:
20 - Database connection (long term)
21 - In Memory stack management (short term)
24 Thanks to Yann Esposito for our discussions at the start and to Nicolas
25 Pouillard (who mainly made it).
29 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
31 {-# LANGUAGE ConstraintKinds #-}
32 {-# LANGUAGE TemplateHaskell #-}
33 {-# LANGUAGE TypeOperators #-}
34 {-# LANGUAGE KindSignatures #-}
35 {-# LANGUAGE ScopedTypeVariables #-}
36 {-# LANGUAGE TypeFamilies #-}
37 {-# LANGUAGE UndecidableInstances #-}
39 ---------------------------------------------------------------------
42 ---------------------------------------------------------------------
43 import Control.Exception (finally)
45 import Control.Monad.Except (withExceptT)
46 import Control.Monad.Reader (runReaderT)
47 import Data.Aeson.Encode.Pretty (encodePretty)
48 import Data.List (lookup)
50 import Data.Text (Text)
51 import Data.Text.Encoding (encodeUtf8)
53 import Data.Version (showVersion)
54 import GHC.Base (Applicative)
55 import GHC.Generics (D1, Meta (..), Rep, Generic)
56 import GHC.TypeLits (AppendSymbol, Symbol)
57 import Gargantext.Prelude.Config (gc_url)
58 import Gargantext.API.Admin.Auth (AuthContext, auth)
59 import Gargantext.API.Admin.FrontEnd (frontEndServer)
60 import Gargantext.API.Admin.Settings
61 import Gargantext.API.Ngrams (HasRepoSaver(..), saveRepo)
62 import Gargantext.API.Prelude
63 import Gargantext.API.Routes
64 import Gargantext.Prelude
65 import Network.HTTP.Types hiding (Query)
67 import Network.Wai.Handler.Warp hiding (defaultSettings)
68 import Network.Wai.Middleware.Cors
69 import Network.Wai.Middleware.RequestLogger
71 import Servant.Auth.Server (AuthResult(..))
72 import Servant.Auth.Swagger ()
73 import Servant.Swagger
74 import Servant.Swagger.UI
75 import System.IO (FilePath)
76 import qualified Data.ByteString.Lazy.Char8 as BL8
77 import qualified Data.Text.IO as T
78 import qualified Paths_gargantext as PG -- cabal magic build module
79 import qualified Gargantext.API.Public as Public
82 data Mode = Dev | Mock | Prod
83 deriving (Show, Read, Generic)
85 -- | startGargantext takes as parameters port number and Ini file.
86 startGargantext :: Mode -> PortNumber -> FilePath -> IO ()
87 startGargantext mode port file = do
88 env <- newEnv port file
91 let baseUrl = env ^. env_gargConfig . gc_url
92 app <- makeApp env baseUrl
94 mid <- makeDevMiddleware mode
95 run port (mid app) `finally` stopGargantext env
97 portRouteInfo :: PortNumber -> IO ()
98 portRouteInfo port = do
99 T.putStrLn " ----Main Routes----- "
100 T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html"
101 T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui"
103 -- TODO clean this Monad condition (more generic) ?
104 stopGargantext :: HasRepoSaver env => env -> IO ()
105 stopGargantext env = do
106 T.putStrLn "----- Stopping gargantext -----"
107 runReaderT saveRepo env
109 -- | Output generated @swagger.json@ file for the @'TodoAPI'@.
110 swaggerWriteJSON :: IO ()
111 swaggerWriteJSON = BL8.writeFile "swagger.json" (encodePretty swaggerDoc)
113 -- | Swagger Specifications
114 swaggerDoc :: Swagger
115 swaggerDoc = toSwagger (Proxy :: Proxy GargAPI)
116 & info.title .~ "Gargantext"
117 & info.version .~ (cs $ showVersion PG.version)
118 -- & info.base_url ?~ (URL "http://gargantext.org/")
119 & info.description ?~ "REST API specifications"
120 -- & tags .~ Set.fromList [Tag "Garg" (Just "Main perations") Nothing]
121 & applyTagsFor (subOperations (Proxy :: Proxy GargAPI)(Proxy :: Proxy GargAPI))
122 ["Gargantext" & description ?~ "Main operations"]
123 & info.license ?~ ("AGPLV3 (English) and CECILL (French)" & url ?~ URL urlLicence )
125 urlLicence = "https://gitlab.iscpif.fr/gargantext/haskell-gargantext/blob/master/LICENSE"
128 startGargantextMock :: PortNumber -> IO ()
129 startGargantextMock port = do
131 application <- makeMockApp . MockEnv $ FireWall False
135 ----------------------------------------------------------------------
137 fireWall :: Applicative f => Request -> FireWall -> f Bool
139 let origin = lookup "Origin" (requestHeaders req)
140 let host = lookup "Host" (requestHeaders req)
142 if origin == Just (encodeUtf8 "http://localhost:8008")
143 && host == Just (encodeUtf8 "localhost:3000")
144 || (not $ unFireWall fw)
150 -- makeMockApp :: Env -> IO (Warp.Settings, Application)
151 makeMockApp :: MockEnv -> IO Application
153 let serverApp = appMock
155 -- logWare <- mkRequestLogger def { destination = RequestLogger.Logger $ env^.logger }
156 --logWare <- mkRequestLogger def { destination = RequestLogger.Logger "/tmp/logs.txt" }
157 let checkOriginAndHost app req resp = do
158 blocking <- fireWall req (env ^. menv_firewall)
161 False -> resp ( responseLBS status401 []
162 "Invalid Origin or Host header")
164 let corsMiddleware = cors $ \_ -> Just CorsResourcePolicy
165 -- { corsOrigins = Just ([env^.settings.allowedOrigin], False)
166 { corsOrigins = Nothing -- == /*
167 , corsMethods = [ methodGet , methodPost , methodPut
168 , methodDelete, methodOptions, methodHead]
169 , corsRequestHeaders = ["authorization", "content-type"]
170 , corsExposedHeaders = Nothing
171 , corsMaxAge = Just ( 60*60*24 ) -- one day
172 , corsVaryOrigin = False
173 , corsRequireOrigin = False
174 , corsIgnoreFailures = False
177 --let warpS = Warp.setPort (8008 :: Int) -- (env^.settings.appPort)
178 -- $ Warp.defaultSettings
180 --pure (warpS, logWare $ checkOriginAndHost $ corsMiddleware $ serverApp)
181 pure $ logStdoutDev $ checkOriginAndHost $ corsMiddleware $ serverApp
185 makeDevMiddleware :: Mode -> IO Middleware
186 makeDevMiddleware mode = do
187 -- logWare <- mkRequestLogger def { destination = RequestLogger.Logger $ env^.logger }
188 -- logWare <- mkRequestLogger def { destination = RequestLogger.Logger "/tmp/logs.txt" }
189 -- let checkOriginAndHost app req resp = do
190 -- blocking <- fireWall req (env ^. menv_firewall)
192 -- True -> app req resp
193 -- False -> resp ( responseLBS status401 []
194 -- "Invalid Origin or Host header")
196 let corsMiddleware = cors $ \_ -> Just CorsResourcePolicy
197 -- { corsOrigins = Just ([env^.settings.allowedOrigin], False)
198 { corsOrigins = Nothing -- == /*
199 , corsMethods = [ methodGet , methodPost , methodPut
200 , methodDelete, methodOptions, methodHead]
201 , corsRequestHeaders = ["authorization", "content-type"]
202 , corsExposedHeaders = Nothing
203 , corsMaxAge = Just ( 60*60*24 ) -- one day
204 , corsVaryOrigin = False
205 , corsRequireOrigin = False
206 , corsIgnoreFailures = False
209 --let warpS = Warp.setPort (8008 :: Int) -- (env^.settings.appPort)
210 -- $ Warp.defaultSettings
212 --pure (warpS, logWare . checkOriginAndHost . corsMiddleware)
214 Prod -> pure $ logStdout . corsMiddleware
215 _ -> pure $ logStdoutDev . corsMiddleware
217 ---------------------------------------------------------------------
219 ---------------------------------------------------------------------
220 -- | Server declarations
221 server :: forall env. EnvC env => env -> Text -> IO (Server API)
222 server env baseUrl = do
223 -- orchestrator <- scrapyOrchestrator env
224 pure $ schemaUiServer swaggerDoc
225 :<|> hoistServerWithContext
226 (Proxy :: Proxy GargAPI)
227 (Proxy :: Proxy AuthContext)
229 (serverGargAPI baseUrl)
232 transform :: forall a. GargServerM env GargError a -> Handler a
233 transform = Handler . withExceptT showAsServantErr . (`runReaderT` env)
237 showAsServantErr :: GargError -> ServerError
238 showAsServantErr (GargServerError err) = err
239 showAsServantErr a = err500 { errBody = BL8.pack $ show a }
241 ---------------------------
243 serverGargAPI :: Text -> GargServerT env err (GargServerM env err) GargAPI
244 serverGargAPI baseUrl -- orchestrator
247 :<|> serverPrivateGargAPI
248 :<|> (Public.api baseUrl)
252 gargVersion :: GargServer GargVersion
253 gargVersion = pure (cs $ showVersion PG.version)
255 serverPrivateGargAPI :: GargServerT env err (GargServerM env err) GargPrivateAPI
256 serverPrivateGargAPI (Authenticated auser) = serverPrivateGargAPI' auser
257 serverPrivateGargAPI _ = throwAll' (_ServerError # err401)
258 -- Here throwAll' requires a concrete type for the monad.
261 -- TODO-SECURITY admin only: withAdmin
262 -- Question: How do we mark admins?
264 serverGargAdminAPI :: GargServer GargAdminAPI
265 serverGargAdminAPI = roots
269 ---------------------------------------------------------------------
270 --gargMock :: Server GargAPI
271 --gargMock = mock apiGarg Proxy
272 ---------------------------------------------------------------------
273 makeApp :: EnvC env => env -> Text -> IO Application
274 makeApp env baseUrl = serveWithContext api cfg <$> server env baseUrl
276 cfg :: Servant.Context AuthContext
277 cfg = env ^. settings . jwtSettings
278 :. env ^. settings . cookieSettings
282 --appMock :: Application
283 --appMock = serve api (swaggerFront :<|> gargMock :<|> serverStatic)
284 ---------------------------------------------------------------------
288 apiGarg :: Proxy GargAPI
290 ---------------------------------------------------------------------
291 schemaUiServer :: (Server api ~ Handler Swagger)
292 => Swagger -> Server (SwaggerSchemaUI' dir api)
293 schemaUiServer = swaggerSchemaUIServer
295 ---------------------------------------------------------------------
296 -- Type Family for the Documentation
297 type family TypeName (x :: *) :: Symbol where
299 TypeName Text = "Text"
300 TypeName x = GenericTypeName x (Rep x ())
302 type family GenericTypeName t (r :: *) :: Symbol where
303 GenericTypeName t (D1 ('MetaData name mod pkg nt) f x) = name
305 type Desc t n = Description (AppendSymbol (TypeName t) (AppendSymbol " | " n))