Stability : experimental
Portability : POSIX
-Main REST API of Gargantext (both Server and Client sides)
+Main (RESTful) API of the instance Gargantext.
-TODO App type, the main monad in which the bot code is written with.
+The Garg-API is typed to derive the documentation, the mock and tests.
-Provide config, state, logs and IO
- type App m a = ( MonadState AppState m
- , MonadReader Conf m
- , MonadLog (WithSeverity Doc) m
- , MonadIO m) => m a
-Thanks @yannEsposito for this.
--}
+This API is indeed typed in order to be able to derive both the server
+and the client sides.
-{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
+The Garg-API-Monad enables:
+ - Security (WIP)
+ - Features (WIP)
+ - Database connection (long term)
+ - In Memory stack management (short term)
+ - Logs (WIP)
+Thanks to Yann Esposito for our discussions at the start and to Nicolas
+Pouillard (who mainly made it).
-{-# LANGUAGE NoImplicitPrelude #-}
-{-# LANGUAGE DataKinds #-}
-{-# LANGUAGE DeriveGeneric #-}
-{-# LANGUAGE FlexibleInstances #-}
-{-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE TemplateHaskell #-}
-{-# LANGUAGE TypeOperators #-}
-{-# LANGUAGE KindSignatures #-}
-{-# LANGUAGE TypeFamilies #-}
-{-# LANGUAGE UndecidableInstances #-}
+-}
----------------------------------------------------------------------
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE TypeOperators #-}
module Gargantext.API
where
----------------------------------------------------------------------
-import Gargantext.Prelude
-
-import System.IO (FilePath)
-
-import GHC.Generics (D1, Meta (..), Rep)
-import GHC.TypeLits (AppendSymbol, Symbol)
-
-import Control.Lens
-import Data.Aeson.Encode.Pretty (encodePretty)
-import qualified Data.ByteString.Lazy.Char8 as BL8
-import Data.Swagger
-import Data.Text (Text)
-import qualified Data.Text.IO as T
---import qualified Data.Set as Set
-
-import Network.Wai
-import Network.Wai.Handler.Warp hiding (defaultSettings)
-
-import Servant
-import Servant.Mock (mock)
---import Servant.Job.Server (WithCallbacks)
-import Servant.Swagger
-import Servant.Swagger.UI
--- import Servant.API.Stream
-
---import Gargantext.API.Swagger
-import Gargantext.API.FrontEnd (FrontEndAPI, frontEndServer)
-
-import Gargantext.API.Node ( Roots , roots
- , NodeAPI , nodeAPI
- , NodesAPI , nodesAPI
- , GraphAPI , graphAPI
- , TreeAPI , treeAPI
- )
-import Gargantext.API.Count ( CountAPI, count, Query)
-import Gargantext.API.Search ( SearchAPI, search, SearchQuery)
---import Gargantext.API.Orchestrator
---import Gargantext.API.Orchestrator.Types
-
----------------------------------------------------------------------
-
-import GHC.Base (Applicative)
--- import Control.Lens
+import Control.Exception (finally)
+import Control.Lens
+import Control.Monad.Reader (runReaderT)
import Data.List (lookup)
import Data.Text.Encoding (encodeUtf8)
-
---import Network.Wai (Request, requestHeaders, responseLBS)
-import Network.Wai (Request, requestHeaders)
---import qualified Network.Wai.Handler.Warp as Warp
+import Data.Text.IO (putStrLn)
+import Data.Validity
+import GHC.Base (Applicative)
+import GHC.Generics (Generic)
+import Gargantext.API.Admin.Auth.Types (AuthContext)
+import Gargantext.API.Admin.Settings (newEnv)
+import Gargantext.API.Admin.Types (FireWall(..), PortNumber, cookieSettings, jwtSettings, settings)
+import Gargantext.API.EKG
+import Gargantext.API.Ngrams (saveNodeStory)
+import Gargantext.API.Prelude
+import Gargantext.API.Routes
+import Gargantext.API.Server (server)
+import Gargantext.Core.NodeStory
+import Gargantext.Prelude hiding (putStrLn)
+import Network.HTTP.Types hiding (Query)
+import Network.Wai
+import Network.Wai.Handler.Warp hiding (defaultSettings)
import Network.Wai.Middleware.Cors
-
import Network.Wai.Middleware.RequestLogger
--- import qualified Network.Wai.Middleware.RequestLogger as RequestLogger
+import Paths_gargantext (getDataDir)
+import Servant
+import System.FilePath
-import Network.HTTP.Types hiding (Query)
+data Mode = Dev | Mock | Prod
+ deriving (Show, Read, Generic)
+
+-- | startGargantext takes as parameters port number and Ini file.
+startGargantext :: Mode -> PortNumber -> FilePath -> IO ()
+startGargantext mode port file = do
+ env <- newEnv port file
+ portRouteInfo port
+ app <- makeApp env
+ mid <- makeDevMiddleware mode
+ run port (mid app) `finally` stopGargantext env
+
+portRouteInfo :: PortNumber -> IO ()
+portRouteInfo port = do
+ putStrLn " ----Main Routes----- "
+ putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html"
+ putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui"
+
+-- TODO clean this Monad condition (more generic) ?
+stopGargantext :: HasNodeStorySaver env => env -> IO ()
+stopGargantext env = do
+ putStrLn "----- Stopping gargantext -----"
+ runReaderT saveNodeStory env
+{-
+startGargantextMock :: PortNumber -> IO ()
+startGargantextMock port = do
+ portRouteInfo port
+ application <- makeMockApp . MockEnv $ FireWall False
+ run port application
+-}
-import Gargantext.API.Settings
+----------------------------------------------------------------------
fireWall :: Applicative f => Request -> FireWall -> f Bool
fireWall req fw = do
let origin = lookup "Origin" (requestHeaders req)
let host = lookup "Host" (requestHeaders req)
- let hostOk = Just (encodeUtf8 "localhost:3000")
- let originOk = Just (encodeUtf8 "http://localhost:8008")
-
- if origin == originOk
- && host == hostOk
+ if origin == Just (encodeUtf8 "http://localhost:8008")
+ && host == Just (encodeUtf8 "localhost:3000")
|| (not $ unFireWall fw)
-
+
then pure True
else pure False
-
+{-
-- makeMockApp :: Env -> IO (Warp.Settings, Application)
makeMockApp :: MockEnv -> IO Application
makeMockApp env = do
--pure (warpS, logWare $ checkOriginAndHost $ corsMiddleware $ serverApp)
pure $ logStdoutDev $ checkOriginAndHost $ corsMiddleware $ serverApp
+-}
---
-makeDevApp :: Env -> IO Application
-makeDevApp env = do
- serverApp <- makeApp env
-
- -- logWare <- mkRequestLogger def { destination = RequestLogger.Logger $ env^.logger }
- --logWare <- mkRequestLogger def { destination = RequestLogger.Logger "/tmp/logs.txt" }
+makeDevMiddleware :: Mode -> IO Middleware
+makeDevMiddleware mode = do
+-- logWare <- mkRequestLogger def { destination = RequestLogger.Logger $ env^.logger }
+-- logWare <- mkRequestLogger def { destination = RequestLogger.Logger "/tmp/logs.txt" }
-- let checkOriginAndHost app req resp = do
-- blocking <- fireWall req (env ^. menv_firewall)
-- case blocking of
-- True -> app req resp
-- False -> resp ( responseLBS status401 []
-- "Invalid Origin or Host header")
---
+--
let corsMiddleware = cors $ \_ -> Just CorsResourcePolicy
-- { corsOrigins = Just ([env^.settings.allowedOrigin], False)
{ corsOrigins = Nothing -- == /*
--let warpS = Warp.setPort (8008 :: Int) -- (env^.settings.appPort)
-- $ Warp.defaultSettings
-
- --pure (warpS, logWare $ checkOriginAndHost $ corsMiddleware $ serverApp)
- pure $ logStdoutDev $ corsMiddleware $ serverApp
---
+ --pure (warpS, logWare . checkOriginAndHost . corsMiddleware)
+ case mode of
+ Prod -> pure $ logStdout . corsMiddleware
+ _ -> pure $ logStdoutDev . corsMiddleware
---------------------------------------------------------------------
-- | API Global
-
--- | API for serving @swagger.json@
-type SwaggerAPI = SwaggerSchemaUI "swagger-ui" "swagger.json"
-
--- | API for serving main operational routes of @gargantext.org@
-
-
-type GargAPI = "api" :> Summary "API " :> GargAPIVersion
--- | TODO :<|> Summary "Latest API" :> GargAPI'
-
-
-type GargAPIVersion = "v1.0" :> Summary "v1.0: " :> GargAPI'
-
-type GargAPI' =
-
- -- Roots endpoint
- "user" :> Summary "First user endpoint"
- :> Roots
-
-
- -- Node endpoint
- :<|> "node" :> Summary "Node endpoint"
- :> Capture "id" Int :> NodeAPI
-
-
- -- Corpus endpoint
- :<|> "corpus":> Summary "Corpus endpoint"
- :> Capture "id" Int :> NodeAPI
-
- -- Corpus endpoint
- :<|> "nodes" :> Summary "Nodes endpoint"
- :> ReqBody '[JSON] [Int] :> NodesAPI
-
- -- :<|> "counts" :> Stream GET NewLineFraming '[JSON] Count :> CountAPI
- -- Corpus endpoint
- :<|> "count" :> Summary "Count endpoint"
- :> ReqBody '[JSON] Query :> CountAPI
-
- -- Corpus endpoint
- :<|> "search":> Summary "Search endpoint"
- :> ReqBody '[JSON] SearchQuery :> SearchAPI
-
- :<|> "graph" :> Summary "Graph endpoint"
- :> Capture "id" Int :> GraphAPI
-
- -- Tree endpoint
- :<|> "tree" :> Summary "Tree endpoint"
- :> Capture "id" Int :> TreeAPI
-
-
- -- :<|> "scraper" :> WithCallbacks ScraperAPI
-
--- /mv/<id>/<id>
--- /merge/<id>/<id>
--- /rename/<id>
- -- :<|> "static"
- -- :<|> "list" :> Capture "id" Int :> NodeAPI
- -- :<|> "ngrams" :> Capture "id" Int :> NodeAPI
- -- :<|> "auth" :> Capture "id" Int :> NodeAPI
---------------------------------------------------------------------
-type SwaggerFrontAPI = SwaggerAPI :<|> FrontEndAPI
-
-type API = SwaggerFrontAPI :<|> GargAPI
----------------------------------------------------------------------
--- | Server declaration
-server :: Env -> IO (Server API)
-server env = do
- -- orchestrator <- scrapyOrchestrator env
- pure $ swaggerFront
- :<|> roots conn
- :<|> nodeAPI conn
- :<|> nodeAPI conn
- :<|> nodesAPI conn
- :<|> count -- TODO: undefined
- :<|> search conn
- :<|> graphAPI conn -- TODO: mock
- :<|> treeAPI conn
- -- :<|> orchestrator
- where
- conn = env ^. env_conn
+---------------------------
----------------------------------------------------------------------
-swaggerFront :: Server SwaggerFrontAPI
-swaggerFront = schemaUiServer swaggerDoc
- :<|> frontEndServer
-gargMock :: Server GargAPI
-gargMock = mock apiGarg Proxy
+-- TODO-SECURITY admin only: withAdmin
+-- Question: How do we mark admins?
+{-
+serverGargAdminAPI :: GargServer GargAdminAPI
+serverGargAdminAPI = roots
+ :<|> nodesAPI
+-}
---------------------------------------------------------------------
-makeApp :: Env -> IO Application
-makeApp = fmap (serve api) . server
-
-appMock :: Application
-appMock = serve api (swaggerFront :<|> gargMock)
+--gargMock :: Server GargAPI
+--gargMock = mock apiGarg Proxy
+---------------------------------------------------------------------
+makeApp :: (Typeable env, EnvC env) => env -> IO Application
+makeApp env = do
+ serv <- server env
+ (ekgStore, ekgMid) <- newEkgStore api
+ ekgDir <- (</> "ekg-assets") <$> getDataDir
+ return $ ekgMid $ serveWithContext apiWithEkg cfg
+ (ekgServer ekgDir ekgStore :<|> serv)
+ where
+ cfg :: Servant.Context AuthContext
+ cfg = env ^. settings . jwtSettings
+ :. env ^. settings . cookieSettings
+ -- :. authCheck env
+ :. EmptyContext
+
+--appMock :: Application
+--appMock = serve api (swaggerFront :<|> gargMock :<|> serverStatic)
---------------------------------------------------------------------
api :: Proxy API
api = Proxy
+apiWithEkg :: Proxy (EkgAPI :<|> API)
+apiWithEkg = Proxy
+
apiGarg :: Proxy GargAPI
apiGarg = Proxy
---------------------------------------------------------------------
-schemaUiServer :: (Server api ~ Handler Swagger)
- => Swagger -> Server (SwaggerSchemaUI' dir api)
-schemaUiServer = swaggerSchemaUIServer
-
-
+{- UNUSED
+--import GHC.Generics (D1, Meta (..), Rep, Generic)
+--import GHC.TypeLits (AppendSymbol, Symbol)
+---------------------------------------------------------------------
-- Type Family for the Documentation
type family TypeName (x :: *) :: Symbol where
TypeName Int = "Int"
GenericTypeName t (D1 ('MetaData name mod pkg nt) f x) = name
type Desc t n = Description (AppendSymbol (TypeName t) (AppendSymbol " | " n))
-
-
--- | Swagger Specifications
-swaggerDoc :: Swagger
-swaggerDoc = toSwagger (Proxy :: Proxy GargAPI)
- & info.title .~ "Gargantext"
- & info.version .~ "0.1.0"
- -- & info.base_url ?~ (URL "http://gargantext.org/")
- & info.description ?~ "REST API specifications"
- -- & tags .~ Set.fromList [Tag "Garg" (Just "Main perations") Nothing]
- & applyTagsFor (subOperations (Proxy :: Proxy GargAPI)(Proxy :: Proxy GargAPI))
- ["Garg" & description ?~ "Main operations"]
- & info.license ?~ ("AGPLV3 (English) and CECILL (French)" & url ?~ URL urlLicence )
- where
- urlLicence = "https://gitlab.iscpif.fr/gargantext/haskell-gargantext/blob/master/LICENSE"
-
--- | Output generated @swagger.json@ file for the @'TodoAPI'@.
-swaggerWriteJSON :: IO ()
-swaggerWriteJSON = BL8.writeFile "swagger.json" (encodePretty swaggerDoc)
-
-portRouteInfo :: PortNumber -> IO ()
-portRouteInfo port = do
- T.putStrLn " ----Main Routes----- "
- T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/index.html"
- T.putStrLn $ "http://localhost:" <> toUrlPiece port <> "/swagger-ui"
-
--- | startGargantext takes as parameters port number and Ini file.
-startGargantext :: PortNumber -> FilePath -> IO ()
-startGargantext port file = do
- env <- newEnv port file
- portRouteInfo port
- app <- makeDevApp env
- run port app
-
-startGargantextMock :: PortNumber -> IO ()
-startGargantextMock port = do
- portRouteInfo port
- application <- makeMockApp . MockEnv $ FireWall False
- run port application
-
+-}