2 Module : Gargantext.API.Prelude
3 Description : Server API main Types
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE ConstraintKinds #-}
13 {-# LANGUAGE FunctionalDependencies #-}
14 {-# LANGUAGE TemplateHaskell #-}
15 {-# LANGUAGE TypeOperators #-}
16 {-# LANGUAGE UndecidableInstances #-}
18 module Gargantext.API.Prelude
19 ( module Gargantext.API.Prelude
25 import Control.Concurrent (threadDelay)
26 import Control.Exception (Exception)
27 import Control.Lens (Prism', (#))
28 import Control.Lens.TH (makePrisms)
29 import Control.Monad.Error.Class (MonadError(throwError))
30 import Control.Monad.Except (ExceptT)
31 import Control.Monad.Reader (ReaderT)
32 import Crypto.JOSE.Error as Jose
33 import Data.Aeson.Types
36 import Gargantext.API.Admin.Orchestrator.Types
37 import Gargantext.API.Admin.Settings
38 import Gargantext.API.Ngrams
39 import Gargantext.Core.Types
40 import Gargantext.Database.Prelude
41 import Gargantext.Database.Query.Table.Node.Error (NodeError(..), HasNodeError(..))
42 import Gargantext.Database.Query.Tree
43 import Gargantext.Prelude
45 import Servant.Job.Async (HasJobEnv)
46 import Servant.Job.Core (HasServerError(..), serverError)
48 class HasJoseError e where
49 _JoseError :: Prism' e Jose.Error
51 joseError :: (MonadError e m, HasJoseError e) => Jose.Error -> m a
52 joseError = throwError . (_JoseError #)
54 class ThrowAll' e a | a -> e where
55 -- | 'throwAll' is a convenience function to throw errors across an entire
59 -- > throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
60 -- > == throwError err400 :<|> throwError err400 :<|> err400
63 instance (ThrowAll' e a, ThrowAll' e b) => ThrowAll' e (a :<|> b) where
64 throwAll' e = throwAll' e :<|> throwAll' e
66 -- Really this shouldn't be necessary - ((->) a) should be an instance of
68 instance {-# OVERLAPPING #-} ThrowAll' e b => ThrowAll' e (a -> b) where
69 throwAll' e = const $ throwAll' e
71 instance {-# OVERLAPPABLE #-} (MonadError e m) => ThrowAll' e (m a) where
72 throwAll' = throwError
74 type GargServerC env err m =
81 , ToJSON err -- TODO this is arguable
85 , HasJobEnv env JobLog JobLog
88 type GargServerT env err m api = GargServerC env err m => ServerT api m
91 forall env err m. GargServerT env err m api
93 -- This is the concrete monad. It needs to be used as little as possible,
94 -- instead, prefer GargServer, GargServerT, GargServerC.
95 type GargServerM env err = ReaderT env (ExceptT err IO)
98 ( HasConnectionPool env
101 , HasJobEnv env JobLog JobLog
104 -------------------------------------------------------------------
105 runCmdReplEasy :: Cmd' DevEnv GargError a -> IO a
106 runCmdReplEasy f = withDevEnv "gargantext.ini" $ \env -> runCmdDev env f
108 -------------------------------------------------------------------
109 -- | This Type is needed to prepare the function before the GargServer
110 type GargNoServer' env err m =
117 type GargNoServer t =
118 forall env err m. GargNoServer' env err m => m t
119 -------------------------------------------------------------------
122 = GargNodeError NodeError
123 | GargTreeError TreeError
124 | GargInvalidError Validation
125 | GargJoseError Jose.Error
126 | GargServerError ServerError
127 deriving (Show, Typeable)
129 makePrisms ''GargError
131 instance ToJSON GargError where
132 toJSON _ = String "SomeGargErrorPleaseReport"
134 instance Exception GargError
136 instance HasNodeError GargError where
137 _NodeError = _GargNodeError
139 instance HasInvalidError GargError where
140 _InvalidError = _GargInvalidError
142 instance HasTreeError GargError where
143 _TreeError = _GargTreeError
145 instance HasServerError GargError where
146 _ServerError = _GargServerError
148 instance HasJoseError GargError where
149 _JoseError = _GargJoseError
152 ------------------------------------------------------------------------
155 simuLogs :: MonadBase IO m
159 simuLogs logStatus t = do
160 _ <- mapM (\n -> simuTask logStatus n t) $ take t [0,1..]
161 pure $ JobLog { _scst_succeeded = Just t
162 , _scst_failed = Just 0
163 , _scst_remaining = Just 0
164 , _scst_events = Just []
167 simuTask :: MonadBase IO m
172 simuTask logStatus cur total = do
173 let m = (10 :: Int) ^ (6 :: Int)
174 liftBase $ threadDelay (m*5)
176 let status = JobLog { _scst_succeeded = Just cur
177 , _scst_failed = Just 0
178 , _scst_remaining = (-) <$> Just total <*> Just cur
179 , _scst_events = Just []
181 printDebug "status" status