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
11 Main REST API of Gargantext (both Server and Client sides)
15 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 module Gargantext.Server
25 import Gargantext.Prelude
28 import Control.Monad.IO.Class
31 import Network.Wai.Handler.Warp
33 import Servant.Multipart
34 import Database.PostgreSQL.Simple (Connection, connect)
36 import System.IO (FilePath, putStrLn, readFile, print)
37 import Data.Text (Text(), pack)
38 import Gargantext.Types.Main (Node, NodeId)
39 import Gargantext.Database.Node (getNodesWithParentId, getNode)
40 import Gargantext.Database.Private (databaseParameters)
42 -- | TODO, use MOCK feature of Servant to generate fake data (for tests)
44 type NodeAPI = Get '[JSON] (Node Value)
45 :<|> "children" :> Get '[JSON] [Node Value]
47 type API = "roots" :> Get '[JSON] [Node Value]
48 :<|> "node" :> Capture "id" Int :> NodeAPI
49 :<|> "echo" :> Capture "string" Text :> Get '[JSON] Text
50 :<|> "upload" :> MultipartForm MultipartData :> Post '[JSON] Text
52 -- :<|> "node" :> Capture "id" Int :> Get '[JSON] Node
54 server :: Connection -> Server API
56 = liftIO (getNodesWithParentId conn 0)
63 startGargantext :: Int -> FilePath -> IO ()
64 startGargantext port file = do
66 print ("Starting server on port " <> show port)
67 param <- databaseParameters file
72 -- | TODO App type, the main monad in which the bot code is written with.
73 -- Provide config, state, logs and IO
74 -- type App m a = ( MonadState AppState m
75 -- , MonadReader Conf m
76 -- , MonadLog (WithSeverity Doc) m
77 -- , MonadIO m) => m a
78 -- Thanks @yannEsposito for this.
79 app :: Connection -> Application
80 app = serve api . server
85 nodeAPI :: Connection -> NodeId -> Server NodeAPI
87 = liftIO (getNode conn id')
88 :<|> liftIO (getNodesWithParentId conn id)
93 -- TODO Is it possible to adapt the function according to iValue input ?
94 upload :: MultipartData -> Handler Text
95 upload multipartData = do
98 forM_ (inputs multipartData) $ \input ->
99 putStrLn $ " " <> show (iName input)
100 <> " -> " <> show (iValue input)
102 forM_ (files multipartData) $ \file -> do
103 content <- readFile (fdFilePath file)
104 putStrLn $ "Content of " <> show (fdFileName file)
105 <> " at " <> fdFilePath file
107 pure (pack "Data loaded")