]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API.hs
[MOCK] all routes completed, builds but need to be adapted to fite the practices.
[gargantext.git] / src / Gargantext / API.hs
1 {-|
2 Module : Gargantext.API
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Main REST API of Gargantext (both Server and Client sides)
11
12 TODO App type, the main monad in which the bot code is written with.
13 Provide config, state, logs and IO
14 type App m a = ( MonadState AppState m
15 , MonadReader Conf m
16 , MonadLog (WithSeverity Doc) m
17 , MonadIO m) => m a
18 Thanks @yannEsposito for this.
19 -}
20
21 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
22
23 {-# LANGUAGE DataKinds #-}
24 {-# LANGUAGE DeriveGeneric #-}
25 {-# LANGUAGE FlexibleInstances #-}
26 {-# LANGUAGE TypeOperators #-}
27 {-# LANGUAGE TemplateHaskell #-}
28 {-# LANGUAGE OverloadedStrings #-}
29
30 module Gargantext.API
31 where
32
33 import Gargantext.Prelude
34
35 import Network.Wai
36 import Network.Wai.Handler.Warp
37
38 import Servant
39 import Servant.Mock (mock)
40 -- import Servant.API.Stream
41
42 import Data.Text (pack)
43 import Database.PostgreSQL.Simple (Connection, connect)
44 import System.IO (FilePath, print)
45
46 -- import Gargantext.API.Auth
47 import Gargantext.API.Node ( Roots , roots
48 , NodeAPI , nodeAPI
49 , NodesAPI , nodesAPI
50 )
51 import Gargantext.API.Count ( CountAPI, count, Query)
52
53 import Gargantext.Database.Utils (databaseParameters)
54
55 ---------------------------------------------------------------------
56 ---------------------------------------------------------------------
57 type PortNumber = Int
58 ---------------------------------------------------------------------
59
60 -- | startGargantext takes as parameters port number and Ini file.
61 startGargantext :: PortNumber -> FilePath -> IO ()
62 startGargantext port file = do
63 print ("Starting Gargantext server" <> show port)
64 print ("http://localhost:" <> show port)
65 param <- databaseParameters file
66 conn <- connect param
67 run port ( app conn )
68
69 startGargantextMock :: PortNumber -> IO ()
70 startGargantextMock port = do
71 print (pack "Starting Mock server")
72 print (pack $ "curl "
73 <> "-H \"content-type: application/json"
74 <> "-d \'{\"query_query\":\"query\"}\' "
75 <> "-v http://localhost:"
76 <> show port
77 <>"/count"
78 )
79 run port ( serve api $ mock api Proxy )
80
81 ---------------------------------------------------------------------
82 ---------------------------------------------------------------------
83
84 -- | Main routes of the API are typed
85 type API = "roots" :> Roots
86
87 :<|> "node" :> Capture "id" Int :> NodeAPI
88 :<|> "nodes" :> ReqBody '[JSON] [Int] :> NodesAPI
89
90 -- :<|> "counts" :> Stream GET NewLineFraming '[JSON] Count :> CountAPI
91 :<|> "count" :> ReqBody '[JSON] Query :> CountAPI
92
93 -- /mv/<id>/<id>
94 -- /merge/<id>/<id>
95 -- /rename/<id>
96 -- :<|> "static"
97 -- :<|> "list" :> Capture "id" Int :> NodeAPI
98 -- :<|> "ngrams" :> Capture "id" Int :> NodeAPI
99 -- :<|> "auth" :> Capture "id" Int :> NodeAPI
100
101
102 -- | Server declaration
103 server :: Connection -> Server API
104 server conn = roots conn
105 :<|> nodeAPI conn
106 :<|> nodesAPI conn
107 :<|> count
108
109 ---------------------------------------------------------------------
110 ---------------------------------------------------------------------
111 app :: Connection -> Application
112 app = serve api . server
113
114 api :: Proxy API
115 api = Proxy
116 ---------------------------------------------------------------------
117 ---------------------------------------------------------------------
118