3 Module : Gargantext.Server
4 Description : Server API
5 Copyright : (c) CNRS, 2017-Present
6 License : AGPL + CECILL v3
7 Maintainer : team@gargantext.org
8 Stability : experimental
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 module Gargantext.Server
23 import Prelude hiding (null)
25 import Control.Monad.IO.Class
28 import Network.Wai.Handler.Warp
30 import Servant.Multipart
31 import Database.PostgreSQL.Simple (Connection, connect)
33 import System.IO (FilePath)
35 import Gargantext.Types.Main (Node, NodeId)
36 import Gargantext.Database.Node (getNodesWithParentId, getNode)
37 import Gargantext.Database.Private (databaseParameters)
39 -- | TODO, use MOCK feature of Servant to generate fake data (for tests)
41 type NodeAPI = Get '[JSON] (Node Value)
42 :<|> "children" :> Get '[JSON] [Node Value]
44 type API = "roots" :> Get '[JSON] [Node Value]
45 :<|> "node" :> Capture "id" Int :> NodeAPI
46 :<|> "echo" :> Capture "string" String :> Get '[JSON] String
47 :<|> "upload" :> MultipartForm MultipartData :> Post '[JSON] String
49 -- :<|> "node" :> Capture "id" Int :> Get '[JSON] Node
51 server :: Connection -> Server API
53 = liftIO (getNodesWithParentId conn 0)
60 startGargantext :: FilePath -> IO ()
61 startGargantext file = do
63 print ("Starting server on port " ++ show port)
64 param <- databaseParameters file
71 -- | TODO App type, the main monad in which the bot code is written with.
72 -- Provide config, state, logs and IO
73 -- type App m a = ( MonadState AppState m
74 -- , MonadReader Conf m
75 -- , MonadLog (WithSeverity Doc) m
76 -- , MonadIO m) => m a
77 -- Thanks @yannEsposito for this.
78 app :: Connection -> Application
79 app = serve api . server
84 nodeAPI :: Connection -> NodeId -> Server NodeAPI
86 = liftIO (getNode conn id')
87 :<|> liftIO (getNodesWithParentId conn id)
92 -- TODO Is it possible to adapt the function according to iValue input ?
93 upload :: MultipartData -> Handler String
94 upload multipartData = do
97 forM_ (inputs multipartData) $ \input ->
98 putStrLn $ " " ++ show (iName input)
99 ++ " -> " ++ show (iValue input)
101 forM_ (files multipartData) $ \file -> do
102 content <- readFile (fdFilePath file)
103 putStrLn $ "Content of " ++ show (fdFileName file)
104 ++ " at " ++ fdFilePath file