2018-06-27 16:11:32 +03:00
|
|
|
module Main where
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
import Migrate (migrateCatalog)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Ops
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
import Control.Monad.STM (atomically)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Time.Clock (getCurrentTime)
|
|
|
|
import Options.Applicative
|
2018-12-14 06:21:41 +03:00
|
|
|
import System.Environment (getEnvironment, lookupEnv)
|
2018-12-19 14:38:33 +03:00
|
|
|
import System.Exit (exitFailure)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-07-27 12:34:50 +03:00
|
|
|
import qualified Control.Concurrent as C
|
2018-06-27 16:11:32 +03:00
|
|
|
import qualified Data.Aeson as A
|
|
|
|
import qualified Data.ByteString.Char8 as BC
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BLC
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Yaml as Y
|
2018-07-27 12:34:50 +03:00
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Client.TLS as HTTP
|
|
|
|
import qualified Network.Wai.Handler.Warp as Warp
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.Events.Lib
|
2019-01-02 14:24:17 +03:00
|
|
|
import Hasura.Logging (Logger (..), defaultLoggerSettings,
|
|
|
|
mkLogger, mkLoggerCtx)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Metadata (fetchMetadata)
|
2018-12-19 14:38:33 +03:00
|
|
|
import Hasura.RQL.Types (QErr, adminUserInfo,
|
2018-12-13 10:26:15 +03:00
|
|
|
emptySchemaCache)
|
2018-07-20 10:22:46 +03:00
|
|
|
import Hasura.Server.App (mkWaiApp)
|
2018-09-27 14:22:49 +03:00
|
|
|
import Hasura.Server.Auth
|
2018-07-27 12:34:50 +03:00
|
|
|
import Hasura.Server.CheckUpdates (checkForUpdates)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Server.Init
|
2018-12-13 10:26:15 +03:00
|
|
|
import Hasura.Server.Query (peelRun)
|
2019-01-28 16:55:28 +03:00
|
|
|
import Hasura.Server.Telemetry
|
2018-12-14 06:21:41 +03:00
|
|
|
import Hasura.Server.Version (currentVersion)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
printErrExit :: forall a . String -> IO a
|
|
|
|
printErrExit = (>> exitFailure) . putStrLn
|
|
|
|
|
|
|
|
printErrJExit :: A.ToJSON a => forall b . a -> IO b
|
|
|
|
printErrJExit = (>> exitFailure) . printJSON
|
|
|
|
|
|
|
|
parseHGECommand :: Parser RawHGECommand
|
|
|
|
parseHGECommand =
|
2018-12-14 06:21:41 +03:00
|
|
|
subparser
|
2018-12-19 14:38:33 +03:00
|
|
|
( command "serve" (info (helper <*> (HCServe <$> serveOpts))
|
2018-12-14 06:21:41 +03:00
|
|
|
( progDesc "Start the GraphQL Engine Server"
|
|
|
|
<> footerDoc (Just serveCmdFooter)
|
|
|
|
))
|
2018-12-19 14:38:33 +03:00
|
|
|
<> command "export" (info (pure HCExport)
|
2018-12-14 06:21:41 +03:00
|
|
|
( progDesc "Export graphql-engine's metadata to stdout" ))
|
2018-12-19 14:38:33 +03:00
|
|
|
<> command "clean" (info (pure HCClean)
|
2018-12-14 06:21:41 +03:00
|
|
|
( progDesc "Clean graphql-engine's metadata to start afresh" ))
|
2018-12-19 14:38:33 +03:00
|
|
|
<> command "execute" (info (pure HCExecute)
|
2018-12-14 06:21:41 +03:00
|
|
|
( progDesc "Execute a query" ))
|
2018-12-19 14:38:33 +03:00
|
|
|
<> command "version" (info (pure HCVersion)
|
2018-12-14 06:21:41 +03:00
|
|
|
(progDesc "Prints the version of GraphQL Engine"))
|
|
|
|
)
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2018-12-19 14:38:33 +03:00
|
|
|
serveOpts = RawServeOptions
|
|
|
|
<$> parseServerPort
|
2019-01-11 14:07:13 +03:00
|
|
|
<*> parseServerHost
|
2018-12-19 14:38:33 +03:00
|
|
|
<*> parseConnParams
|
|
|
|
<*> parseTxIsolation
|
2019-02-14 12:37:47 +03:00
|
|
|
<*> (parseAdminSecret <|> parseAccessKey)
|
2018-12-19 14:38:33 +03:00
|
|
|
<*> parseWebHook
|
|
|
|
<*> parseJwtSecret
|
|
|
|
<*> parseUnAuthRole
|
|
|
|
<*> parseCorsConfig
|
|
|
|
<*> parseEnableConsole
|
2019-01-28 16:55:28 +03:00
|
|
|
<*> parseEnableTelemetry
|
2019-03-04 10:46:53 +03:00
|
|
|
<*> parseWsReadCookie
|
2019-03-01 14:45:04 +03:00
|
|
|
<*> parseStringifyNum
|
2019-02-28 16:53:03 +03:00
|
|
|
<*> parseEnabledAPIs
|
2018-12-19 14:38:33 +03:00
|
|
|
|
2019-03-04 10:46:53 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseArgs :: IO HGEOptions
|
|
|
|
parseArgs = do
|
|
|
|
rawHGEOpts <- execParser opts
|
|
|
|
env <- getEnvironment
|
|
|
|
let eitherOpts = runWithEnv env $ mkHGEOptions rawHGEOpts
|
|
|
|
either printErrExit return eitherOpts
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2018-12-19 14:38:33 +03:00
|
|
|
opts = info (helper <*> hgeOpts)
|
2018-06-27 16:11:32 +03:00
|
|
|
( fullDesc <>
|
2018-12-14 06:21:41 +03:00
|
|
|
header "Hasura GraphQL Engine: Realtime GraphQL API over Postgres with access control" <>
|
|
|
|
footerDoc (Just mainCmdFooter)
|
|
|
|
)
|
2018-12-19 14:38:33 +03:00
|
|
|
hgeOpts = HGEOptionsG <$> parseRawConnInfo <*> parseHGECommand
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
printJSON :: (A.ToJSON a) => a -> IO ()
|
|
|
|
printJSON = BLC.putStrLn . A.encode
|
|
|
|
|
|
|
|
printYaml :: (A.ToJSON a) => a -> IO ()
|
|
|
|
printYaml = BC.putStrLn . Y.encode
|
|
|
|
|
|
|
|
main :: IO ()
|
2018-07-20 10:22:46 +03:00
|
|
|
main = do
|
2018-12-19 14:38:33 +03:00
|
|
|
(HGEOptionsG rci hgeCmd) <- parseArgs
|
|
|
|
-- global http manager
|
2018-07-27 12:34:50 +03:00
|
|
|
httpManager <- HTTP.newManager HTTP.tlsManagerSettings
|
2019-01-02 14:24:17 +03:00
|
|
|
loggerCtx <- mkLoggerCtx $ defaultLoggerSettings True
|
|
|
|
let logger = mkLogger loggerCtx
|
2018-12-19 14:38:33 +03:00
|
|
|
case hgeCmd of
|
2019-02-14 12:37:47 +03:00
|
|
|
HCServe so@(ServeOptions port host cp isoL mAdminSecret mAuthHook mJwtSecret
|
2019-03-04 10:46:53 +03:00
|
|
|
mUnAuthRole corsCfg enableConsole enableTelemetry strfyNum enabledAPIs) -> do
|
2019-01-02 14:24:17 +03:00
|
|
|
-- log serve options
|
|
|
|
unLogger logger $ serveOptsToLog so
|
2018-12-19 14:38:33 +03:00
|
|
|
hloggerCtx <- mkLoggerCtx $ defaultLoggerSettings False
|
2018-09-27 14:22:49 +03:00
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
authModeRes <- runExceptT $ mkAuthMode mAdminSecret mAuthHook mJwtSecret
|
2018-12-14 06:21:41 +03:00
|
|
|
mUnAuthRole httpManager loggerCtx
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
am <- either (printErrExit . T.unpack) return authModeRes
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
ci <- procConnInfo rci
|
2019-01-02 14:24:17 +03:00
|
|
|
-- log postgres connection info
|
|
|
|
unLogger logger $ connInfoToLog ci
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
-- safe init catalog
|
2019-01-28 16:55:28 +03:00
|
|
|
initRes <- initialise logger ci httpManager
|
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
-- prepare event triggers data
|
|
|
|
prepareEvents logger ci
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
pool <- Q.initPGPool ci cp
|
2018-12-21 10:51:02 +03:00
|
|
|
(app, cacheRef) <- mkWaiApp isoL loggerCtx pool httpManager
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum am corsCfg enableConsole enableTelemetry enabledAPIs
|
2019-01-11 14:07:13 +03:00
|
|
|
|
|
|
|
let warpSettings = Warp.setPort port $ Warp.setHost host Warp.defaultSettings
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
maxEvThrds <- getFromEnv defaultMaxEventThreads "HASURA_GRAPHQL_EVENTS_HTTP_POOL_SIZE"
|
2018-10-30 18:20:18 +03:00
|
|
|
evFetchMilliSec <- getFromEnv defaultFetchIntervalMilliSec "HASURA_GRAPHQL_EVENTS_FETCH_INTERVAL"
|
2018-10-26 19:28:03 +03:00
|
|
|
logEnvHeaders <- getFromEnv False "LOG_HEADERS_FROM_ENV"
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-10-30 18:20:18 +03:00
|
|
|
eventEngineCtx <- atomically $ initEventEngineCtx maxEvThrds evFetchMilliSec
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
unLogger logger $
|
|
|
|
mkGenericStrLog "event_triggers" "starting workers"
|
2019-02-14 10:37:59 +03:00
|
|
|
void $ C.forkIO $ processEventQueue hloggerCtx logEnvHeaders httpManager pool cacheRef eventEngineCtx
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
-- start a background thread to check for updates
|
|
|
|
void $ C.forkIO $ checkForUpdates loggerCtx httpManager
|
|
|
|
|
|
|
|
-- start a background thread for telemetry
|
|
|
|
when enableTelemetry $ do
|
|
|
|
unLogger logger $ mkGenericStrLog "telemetry" telemetryNotice
|
|
|
|
void $ C.forkIO $ runTelemetry logger httpManager cacheRef initRes
|
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
unLogger logger $
|
|
|
|
mkGenericStrLog "server" "starting API server"
|
2018-07-20 10:22:46 +03:00
|
|
|
Warp.runSettings warpSettings app
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExport -> do
|
|
|
|
ci <- procConnInfo rci
|
2018-06-27 16:11:32 +03:00
|
|
|
res <- runTx ci fetchMetadata
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrJExit printJSON res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCClean -> do
|
|
|
|
ci <- procConnInfo rci
|
2018-06-27 16:11:32 +03:00
|
|
|
res <- runTx ci cleanCatalog
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrJExit (const cleanSuccess) res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExecute -> do
|
2018-06-27 16:11:32 +03:00
|
|
|
queryBs <- BL.getContents
|
2018-12-19 14:38:33 +03:00
|
|
|
ci <- procConnInfo rci
|
2018-12-13 10:26:15 +03:00
|
|
|
res <- runAsAdmin ci httpManager $ execQuery queryBs
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrJExit BLC.putStrLn res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCVersion -> putStrLn $ "Hasura GraphQL Engine: " ++ T.unpack currentVersion
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runTx :: Q.ConnInfo -> Q.TxE QErr a -> IO (Either QErr a)
|
2018-06-27 16:11:32 +03:00
|
|
|
runTx ci tx = do
|
|
|
|
pool <- getMinimalPool ci
|
|
|
|
runExceptT $ Q.runTx pool (Q.Serializable, Nothing) tx
|
2018-12-13 10:26:15 +03:00
|
|
|
|
|
|
|
runAsAdmin ci httpManager m = do
|
|
|
|
pool <- getMinimalPool ci
|
|
|
|
res <- runExceptT $ peelRun emptySchemaCache adminUserInfo
|
2019-03-01 14:45:04 +03:00
|
|
|
httpManager False pool Q.Serializable m
|
2018-12-13 10:26:15 +03:00
|
|
|
return $ fmap fst res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
|
|
|
procConnInfo rci =
|
|
|
|
either (printErrExit . connInfoErrModifier) return $
|
|
|
|
mkConnInfo rci
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
getMinimalPool ci = do
|
|
|
|
let connParams = Q.defaultConnParams { Q.cpConns = 1 }
|
|
|
|
Q.initPGPool ci connParams
|
2019-01-02 14:24:17 +03:00
|
|
|
|
|
|
|
initialise (Logger logger) ci httpMgr = do
|
2018-06-27 16:11:32 +03:00
|
|
|
currentTime <- getCurrentTime
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
-- initialise the catalog
|
|
|
|
initRes <- runAsAdmin ci httpMgr $ initCatalogSafe currentTime
|
|
|
|
either printErrJExit (logger . mkGenericStrLog "db_init") initRes
|
|
|
|
|
|
|
|
-- migrate catalog if necessary
|
|
|
|
migRes <- runAsAdmin ci httpMgr $ migrateCatalog currentTime
|
|
|
|
either printErrJExit (logger . mkGenericStrLog "db_migrate") migRes
|
|
|
|
|
|
|
|
-- generate and retrieve uuids
|
|
|
|
getUniqIds ci
|
2019-01-02 14:24:17 +03:00
|
|
|
|
|
|
|
prepareEvents (Logger logger) ci = do
|
|
|
|
logger $ mkGenericStrLog "event_triggers" "preparing data"
|
2018-09-05 14:26:46 +03:00
|
|
|
res <- runTx ci unlockAllEvents
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrJExit return res
|
2018-11-15 07:55:39 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
getUniqIds ci = do
|
|
|
|
eDbId <- runTx ci getDbId
|
|
|
|
dbId <- either printErrJExit return eDbId
|
|
|
|
fp <- liftIO generateFingerprint
|
|
|
|
return (dbId, fp)
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
getFromEnv :: (Read a) => a -> String -> IO a
|
|
|
|
getFromEnv defaults env = do
|
|
|
|
mEnv <- lookupEnv env
|
|
|
|
let mRes = case mEnv of
|
|
|
|
Nothing -> Just defaults
|
|
|
|
Just val -> readMaybe val
|
2018-10-26 19:28:03 +03:00
|
|
|
eRes = maybe (Left $ "Wrong expected type for environment variable: " <> env) Right mRes
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrExit return eRes
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
cleanSuccess =
|
|
|
|
putStrLn "successfully cleaned graphql-engine related data"
|
2019-01-28 16:55:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
telemetryNotice :: String
|
|
|
|
telemetryNotice =
|
|
|
|
"Help us improve Hasura! The graphql-engine server collects anonymized "
|
|
|
|
<> "usage stats which allows us to keep improving Hasura at warp speed. "
|
|
|
|
<> "To read more or opt-out, visit https://docs.hasura.io/1.0/graphql/manual/guides/telemetry.html"
|