]> Git — Sourcephile - gargantext.git/blob - bin/gargantext-server/Main.hs
Merge remote-tracking branch 'origin/203-dev-corpus-json-import' into dev
[gargantext.git] / bin / gargantext-server / Main.hs
1 {-|
2 Module : Main.hs
3 Description : Gargantext starter
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Script to start gargantext with different modes (Dev, Prod, Mock).
11
12 -}
13
14 {-# LANGUAGE QuasiQuotes #-}
15 {-# LANGUAGE StandaloneDeriving #-}
16 {-# LANGUAGE Strict #-}
17 {-# LANGUAGE TypeOperators #-}
18 {-# OPTIONS_GHC -fno-warn-orphans #-}
19
20
21 module Main where
22
23
24 import Data.Maybe (fromMaybe)
25 import Data.Text (unpack)
26 import Data.Version (showVersion)
27 import Database.PostgreSQL.Simple.SqlQQ (sql)
28 import GHC.IO.Exception (IOException)
29 import Gargantext.API (startGargantext, Mode(..)) -- , startGargantextMock)
30 import Gargantext.API.Admin.EnvTypes (DevEnv)
31 import Gargantext.API.Dev (withDevEnv, runCmdDev)
32 import Gargantext.Database.Prelude (Cmd'', Cmd, execPGSQuery)
33 import Gargantext.Prelude
34 import Options.Generic
35 import System.Cron.Schedule
36 import System.Exit (exitSuccess)
37 import qualified Paths_gargantext as PG -- cabal magic build module
38
39
40 instance ParseRecord Mode
41 instance ParseField Mode
42 instance ParseFields Mode
43
44 data MyOptions w =
45 MyOptions { run :: w ::: Mode
46 <?> "Possible modes: Dev | Mock | Prod"
47 , port :: w ::: Maybe Int
48 <?> "By default: 8008"
49 , ini :: w ::: Maybe Text
50 <?> "Ini-file path of gargantext.ini"
51 , version :: w ::: Bool
52 <?> "Show version number and exit"
53 }
54 deriving (Generic)
55
56 instance ParseRecord (MyOptions Wrapped)
57 deriving instance Show (MyOptions Unwrapped)
58
59
60 main :: IO ()
61 main = do
62 MyOptions myMode myPort myIniFile myVersion <- unwrapRecord
63 "Gargantext server"
64 ---------------------------------------------------------------
65 if myVersion then do
66 putStrLn $ "Version: " <> showVersion PG.version
67 System.Exit.exitSuccess
68 else
69 return ()
70 ---------------------------------------------------------------
71 let myPort' = case myPort of
72 Just p -> p
73 Nothing -> 8008
74
75 myIniFile' = case myIniFile of
76 Nothing -> panic "[ERROR] gargantext.ini needed"
77 Just i -> i
78
79 ---------------------------------------------------------------
80 let start = case myMode of
81 Mock -> panic "[ERROR] Mock mode unsupported"
82 _ -> startGargantext myMode myPort' (unpack myIniFile')
83 putStrLn $ "Starting with " <> show myMode <> " mode."
84 start
85 ---------------------------------------------------------------
86
87 putStrLn $ "Starting Schedule Jobs"
88
89 withDevEnv (unpack myIniFile') $ \env -> do
90 tids <- execSchedule $ do
91 addJob (runCmdDev env refreshIndex) "5 * * * *"
92 putStrLn ("Refresh Index Cron Job started" <> show tids)
93
94
95
96
97 refreshIndex :: Cmd'' DevEnv IOException ()
98 refreshIndex = do
99 _ <- execPGSQuery [sql| refresh materialized view context_node_ngrams_view; |] ()
100 pure ()
101
102
103
104