]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API.hs
[MOCK] More credible count.
[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 {-# LANGUAGE DataKinds #-}
23 {-# LANGUAGE TemplateHaskell #-}
24 {-# LANGUAGE TypeOperators #-}
25 {-# LANGUAGE DeriveGeneric #-}
26
27 module Gargantext.API
28 where
29
30 import Gargantext.Prelude
31
32 import Network.Wai
33 import Network.Wai.Handler.Warp
34
35 import Servant
36 import Servant.Mock (mock)
37 -- import Servant.API.Stream
38
39 import Data.Text (pack)
40 import Database.PostgreSQL.Simple (Connection, connect)
41 import System.IO (FilePath, print)
42
43 -- import Gargantext.API.Auth
44 import Gargantext.API.Node ( Roots , roots
45 , NodeAPI , nodeAPI
46 , NodesAPI , nodesAPI
47 )
48 import Gargantext.API.Count ( CountAPI, count, Query)
49
50 import Gargantext.Database.Utils (databaseParameters)
51
52 ---------------------------------------------------------------------
53 ---------------------------------------------------------------------
54 type PortNumber = Int
55 ---------------------------------------------------------------------
56
57 -- | startGargantext takes as parameters port number and Ini file.
58 startGargantext :: PortNumber -> FilePath -> IO ()
59 startGargantext port file = do
60 print ("Starting Gargantext server" <> show port)
61 print ("http://localhost:" <> show port)
62 param <- databaseParameters file
63 conn <- connect param
64 run port ( app conn )
65
66 startGargantextMock :: PortNumber -> IO ()
67 startGargantextMock port = do
68 print (pack "Starting Mock server")
69 print (pack $ "curl "
70 <> "-H \"content-type: application/json"
71 <> "-d \'{\"query_query\":\"query\"}\' "
72 <> "-v http://localhost:"
73 <> show port
74 <>"/count"
75 )
76 run port ( serve apiMock $ mock apiMock Proxy )
77
78 ---------------------------------------------------------------------
79 ---------------------------------------------------------------------
80
81 -- | Main routes of the API are typed
82 type API = "roots" :> Roots
83
84 :<|> "node" :> Capture "id" Int :> NodeAPI
85 :<|> "nodes" :> ReqBody '[JSON] [Int] :> NodesAPI
86
87 :<|> APIMock
88 -- :<|> "counts" :> Stream GET NewLineFraming '[JSON] Count :> CountAPI
89 type APIMock = "count" :> ReqBody '[JSON] Query :> CountAPI
90
91 -- /mv/<id>/<id>
92 -- /merge/<id>/<id>
93 -- /rename/<id>
94 -- :<|> "static"
95 -- :<|> "list" :> Capture "id" Int :> NodeAPI
96 -- :<|> "ngrams" :> Capture "id" Int :> NodeAPI
97 -- :<|> "auth" :> Capture "id" Int :> NodeAPI
98
99
100 -- | Server declaration
101 server :: Connection -> Server API
102 server conn = roots conn
103 :<|> nodeAPI conn
104 :<|> nodesAPI conn
105 :<|> count
106
107 ---------------------------------------------------------------------
108 ---------------------------------------------------------------------
109 app :: Connection -> Application
110 app = serve api . server
111
112 api :: Proxy API
113 api = Proxy
114
115 apiMock :: Proxy APIMock
116 apiMock = Proxy
117
118