1 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE TemplateHaskell #-}
4 {-# LANGUAGE TypeOperators #-}
5 module Data.Gargantext.Server
11 import Prelude hiding (null)
13 import Control.Monad.IO.Class
16 import Network.Wai.Handler.Warp
18 import Servant.Multipart
19 import Database.PostgreSQL.Simple (Connection, connect)
22 import Data.Gargantext.Types.Main (Node, NodeId)
23 import Data.Gargantext.Database.Node (getNodesWithParentId, getNode, getNodesWithType)
24 import Data.Gargantext.Database.Private (infoGargandb)
26 -- | TODO, use MOCK feature of Servant to generate fake data (for tests)
28 type NodeAPI = Get '[JSON] (Node Value)
29 :<|> "children" :> Get '[JSON] [Node Value]
31 type API = "roots" :> Get '[JSON] [Node Value]
32 :<|> "node" :> Capture "id" Int :> NodeAPI
33 :<|> "echo" :> Capture "string" String :> Get '[JSON] String
34 :<|> "upload" :> MultipartForm MultipartData :> Post '[JSON] String
36 -- :<|> "node" :> Capture "id" Int :> Get '[JSON] Node
38 server :: Connection -> Server API
40 = liftIO (getNodesWithParentId conn 0)
47 connectGargandb :: IO Connection
48 connectGargandb = connect infoGargandb
50 startGargantext :: IO ()
52 print ("Starting server on port " ++ show port)
53 conn <- connectGargandb
58 -- | TODO App type, the main monad in which the bot code is written with.
59 -- Provide config, state, logs and IO
60 -- type App m a = ( MonadState AppState m
61 -- , MonadReader Conf m
62 -- , MonadLog (WithSeverity Doc) m
63 -- , MonadIO m) => m a
64 -- Thanks @yannEsposito for this.
65 app :: Connection -> Application
66 app = serve api . server
71 nodeAPI :: Connection -> NodeId -> Server NodeAPI
73 = liftIO (getNode conn id')
74 :<|> liftIO (getNodesWithParentId conn id)
80 -- TODO Is it possible to adapt the function according to iValue input ?
81 upload :: MultipartData -> Handler String
82 upload multipartData = do
85 forM_ (inputs multipartData) $ \input ->
86 putStrLn $ " " ++ show (iName input)
87 ++ " -> " ++ show (iValue input)
89 forM_ (files multipartData) $ \file -> do
90 content <- readFile (fdFilePath file)
91 putStrLn $ "Content of " ++ show (fdFileName file)
92 ++ " at " ++ fdFilePath file