1 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE TemplateHaskell #-}
4 {-# LANGUAGE TypeOperators #-}
5 module Data.Gargantext.Server
12 import Control.Monad.IO.Class
16 import Network.Wai.Handler.Warp
18 import Servant.Multipart
20 -- | TODO, use MOCK feature of Servant to generate fake data (for tests)
22 data FakeNode = FakeNode
24 , fakeNodeName :: String
27 $(deriveJSON defaultOptions ''FakeNode)
29 type API = "nodes" :> Get '[JSON] [FakeNode]
30 :<|> "node" :> Capture "id" Int :> Get '[JSON] FakeNode
31 :<|> "echo" :> Capture "string" String :> Get '[JSON] String
32 :<|> "upload" :> MultipartForm MultipartData :> Post '[JSON] String
34 -- :<|> "node" :> Capture "id" Int :> Get '[JSON] Node
37 server = pure fakeNodes
45 startGargantext :: IO ()
46 startGargantext = print ("Starting server on port " ++ show port) >> run port app
50 -- | TODO App type, the main monad in which the bot code is written with.
51 -- Provide config, state, logs and IO
52 -- type App m a = ( MonadState AppState m
53 -- , MonadReader Conf m
54 -- , MonadLog (WithSeverity Doc) m
55 -- , MonadIO m) => m a
56 -- Thanks @yannEsposito for this.
58 app = serve api server
64 fakeNode :: Monad m => Int -> m FakeNode
65 fakeNode id = pure (fakeNodes !! id)
67 fakeNodes :: [FakeNode]
68 fakeNodes = [ FakeNode 1 "Poincare"
69 , FakeNode 2 "Grothendieck"
73 -- TODO Is it possible to adapt the function according to iValue input ?
74 upload :: MultipartData -> Handler String
75 upload multipartData = do
78 forM_ (inputs multipartData) $ \input ->
79 putStrLn $ " " ++ show (iName input)
80 ++ " -> " ++ show (iValue input)
82 forM_ (files multipartData) $ \file -> do
83 content <- readFile (fdFilePath file)
84 putStrLn $ "Content of " ++ show (fdFileName file)
85 ++ " at " ++ fdFilePath file