Skip to content

Commit

Permalink
Merge pull request #204 from ds-wizard/release/4.2.0
Browse files Browse the repository at this point in the history
Release 4.2.0
  • Loading branch information
vknaisl authored Jan 2, 2024
2 parents 614ab47 + b5162a2 commit e4593d3
Show file tree
Hide file tree
Showing 130 changed files with 1,104 additions and 1,821 deletions.
3 changes: 2 additions & 1 deletion registry-public/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ default-extensions:
- ExistentialQuantification
- ExtendedDefaultRules
- FlexibleContexts
- FlexibleInstances
- FunctionalDependencies
- GeneralizedNewtypeDeriving
- FlexibleInstances
- MultiParamTypeClasses
- OverloadedStrings
- QuasiQuotes
- RankNTypes
- RecordWildCards
- ScopedTypeVariables
- TemplateHaskell
- TypeApplications
- TypeFamilies
- TypeOperators
- TypeSynonymInstances
Expand Down
4 changes: 1 addition & 3 deletions registry-server/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: registry-server
version: '4.1.1'
version: '4.2.0'
synopsis: Engine Registry
description: Engine Registry
category: Web
Expand Down Expand Up @@ -34,7 +34,6 @@ library:
- cron
- data-default
- exceptions
- ginger
- hashable
- http-client
- http-types
Expand All @@ -55,7 +54,6 @@ library:
- regex-compat
- resource-pool
- servant
- servant-errors
- servant-multipart
- servant-server
- servant-swagger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Registry.Api.Api where
module Registry.Api.Handler.Api where

import Servant

Expand Down
41 changes: 16 additions & 25 deletions registry-server/src/Registry/Api/Handler/Common.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Registry.Api.Handler.Common where

import Control.Monad.Except (runExceptT)
import Control.Monad.Reader (asks, liftIO, runReaderT)
import Control.Monad.Reader (ask, liftIO, runReaderT)
import Data.Pool
import Servant (throwError)

Expand Down Expand Up @@ -29,49 +29,41 @@ runInAuthService org = runIn (Just org)

runIn :: Maybe Organization -> TransactionState -> AppContext.AppContextM a -> BaseContextM a
runIn mOrganization transactionState function = do
baseContext <- ask
traceUuid <- liftIO generateUuid
serverConf <- asks serverConfig
buildInfoConfig <- asks buildInfoConfig
dbPool <- asks dbPool
s3Client <- asks s3Client
httpClientManager <- asks httpClientManager
let appContext =
AppContext.AppContext
{ serverConfig = serverConf
, buildInfoConfig = buildInfoConfig
, dbPool = dbPool
{ serverConfig = baseContext.serverConfig
, buildInfoConfig = baseContext.buildInfoConfig
, dbPool = baseContext.dbPool
, dbConnection = Nothing
, s3Client = s3Client
, httpClientManager = httpClientManager
, s3Client = baseContext.s3Client
, httpClientManager = baseContext.httpClientManager
, traceUuid = traceUuid
, currentOrganization = mOrganization
}
let loggingLevel = serverConf.logging.level
let loggingLevel = baseContext.serverConfig.logging.level
eResult <-
case transactionState of
Transactional -> do
liftIO $ withResource dbPool $ \dbConn -> do
let appContextWithConn = appContext {AppContext.dbConnection = Just dbConn} :: AppContext.AppContext
liftIO $ runExceptT $ runLogging loggingLevel $ runReaderT (AppContext.runAppContextM function) appContextWithConn
Transactional ->
liftIO $ withResource baseContext.dbPool $ \dbConn ->
liftIO $ runExceptT $ runLogging loggingLevel $ runReaderT (AppContext.runAppContextM function) (appContext {AppContext.dbConnection = Just dbConn})
NoTransaction -> liftIO $ runExceptT $ runLogging loggingLevel $ runReaderT (AppContext.runAppContextM function) appContext
case eResult of
Right result -> return result
Left error -> throwError =<< sendError error

getMaybeAuthServiceExecutor
:: Maybe String -> ((TransactionState -> AppContext.AppContextM a -> BaseContextM a) -> BaseContextM b) -> BaseContextM b
getMaybeAuthServiceExecutor :: Maybe String -> ((TransactionState -> AppContext.AppContextM a -> BaseContextM a) -> BaseContextM b) -> BaseContextM b
getMaybeAuthServiceExecutor (Just tokenHeader) callback = do
organization <- getCurrentOrganization tokenHeader
callback (runInAuthService organization)
getMaybeAuthServiceExecutor Nothing callback = callback runInUnauthService

getAuthServiceExecutor
:: Maybe String -> ((TransactionState -> AppContext.AppContextM a -> BaseContextM a) -> BaseContextM b) -> BaseContextM b
getAuthServiceExecutor :: Maybe String -> ((TransactionState -> AppContext.AppContextM a -> BaseContextM a) -> BaseContextM b) -> BaseContextM b
getAuthServiceExecutor (Just token) callback = do
org <- getCurrentOrganization token
callback (runInAuthService org)
getAuthServiceExecutor Nothing _ =
throwError =<< (sendError . UnauthorizedError $ _ERROR_API_COMMON__UNABLE_TO_GET_TOKEN)
getAuthServiceExecutor Nothing _ = throwError =<< (sendError . UnauthorizedError $ _ERROR_API_COMMON__UNABLE_TO_GET_TOKEN)

getCurrentOrganization :: String -> BaseContextM Organization
getCurrentOrganization tokenHeader = do
Expand All @@ -82,8 +74,7 @@ getCurrentOrganization tokenHeader = do
Nothing -> throwError =<< (sendError . UnauthorizedError $ _ERROR_API_COMMON__UNABLE_TO_GET_ORGANIZATION)

getCurrentOrgToken :: String -> BaseContextM String
getCurrentOrgToken tokenHeader = do
let orgTokenMaybe = separateToken tokenHeader
case orgTokenMaybe of
getCurrentOrgToken tokenHeader =
case separateToken tokenHeader of
Just orgToken -> return orgToken
Nothing -> throwError =<< (sendError . UnauthorizedError $ _ERROR_API_COMMON__UNABLE_TO_GET_TOKEN)
4 changes: 2 additions & 2 deletions registry-server/src/Registry/Api/Handler/Swagger/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Servant
import Servant.Swagger
import Servant.Swagger.UI

import Registry.Api.Api
import Registry.Api.Handler.Api
import Registry.Api.Resource.ActionKey.ActionKeySM ()
import Registry.Api.Resource.Config.ClientConfigSM ()
import Registry.Api.Resource.DocumentTemplate.DocumentTemplateDetailSM ()
Expand Down Expand Up @@ -38,7 +38,7 @@ swagger =
s._swaggerInfo
{ _infoTitle = "Registry API"
, _infoDescription = Just "API specification for Registry"
, _infoVersion = "4.1.1"
, _infoVersion = "4.2.0"
, _infoLicense =
Just $
License
Expand Down
6 changes: 6 additions & 0 deletions registry-server/src/Registry/Api/Sentry.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Registry.Api.Sentry where

import Data.Aeson (Value (..))

getSentryIdentity :: Maybe String -> [(String, Value)]
getSentryIdentity _ = []
19 changes: 19 additions & 0 deletions registry-server/src/Registry/Api/Web.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Registry.Api.Web where

import Servant

import Registry.Api.Handler.Api
import Registry.Api.Handler.Swagger.Api
import Registry.Model.Config.ServerConfig
import Registry.Model.Context.BaseContext
import Shared.Common.Bootstrap.Web

type WebAPI =
SwaggerAPI
:<|> ApplicationAPI

webApi :: Proxy WebAPI
webApi = Proxy

webServer :: BaseContext -> Server WebAPI
webServer baseContext = swaggerServer :<|> hoistServer applicationApi (convert baseContext runBaseContextM) applicationServer
91 changes: 45 additions & 46 deletions registry-server/src/Registry/Application.hs
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
module Registry.Application (
runApplication,
) where
module Registry.Application where

import Control.Concurrent
import Control.Concurrent.Async
import Control.Monad.Reader (liftIO)
import Data.Foldable (forM_)
import System.Exit
import System.IO
import Control.Concurrent (MVar)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Logger (MonadLogger)
import Data.Pool (Pool)
import Database.PostgreSQL.Simple (Connection)
import Network.HTTP.Client (Manager)
import Network.Minio (MinioConn)

import Registry.Bootstrap.DatabaseMigration
import Registry.Bootstrap.Web
import Registry.Bootstrap.Worker
import Registry.Api.Middleware.LoggingMiddleware
import Registry.Api.Sentry
import Registry.Api.Web
import Registry.Constant.ASCIIArt
import Registry.Constant.Resource
import qualified Registry.Database.Migration.Development.Migration as DevDB
import qualified Registry.Database.Migration.Production.Migration as ProdDB
import Registry.Model.Config.ServerConfig
import Registry.Model.Config.ServerConfigIM ()
import Registry.Model.Config.ServerConfigJM ()
import Registry.Model.Context.BaseContext
import Registry.Model.Context.ContextMappers
import Registry.Service.Config.Server.ServerConfigValidation
import Shared.Common.Bootstrap.Config
import Shared.Common.Bootstrap.HttpClient
import Shared.Common.Bootstrap.Postgres
import Shared.Common.Bootstrap.S3
import Registry.Worker.CronWorkers
import Registry.Worker.PermanentWorkers
import Shared.Common.Application
import Shared.Common.Bootstrap.Web
import Shared.Common.Model.Config.BuildInfoConfig
import Shared.Common.Model.Config.ServerConfig
import Shared.Common.Service.Config.BuildInfo.BuildInfoConfigService
import Shared.Common.Service.Config.Server.ServerConfigService
import Shared.Common.Util.Logger
import Shared.Worker.Bootstrap.Worker

runApplication :: IO ()
runApplication = do
hSetBuffering stdout LineBuffering
putStrLn asciiLogo
serverConfig <- loadConfig serverConfigFile (getServerConfig validateServerConfig)
buildInfoConfig <- loadConfig buildInfoFile getBuildInfoConfig
result <-
runLogging serverConfig.logging.level $ do
logInfo _CMP_ENVIRONMENT $ "set to " ++ show serverConfig.general.environment
shutdownFlag <- liftIO newEmptyMVar
dbPool <- connectPostgresDB serverConfig.logging serverConfig.database
httpClientManager <- setupHttpClientManager serverConfig.logging
s3Client <- setupS3Client serverConfig.s3 httpClientManager
let baseContext =
BaseContext
{ serverConfig = serverConfig
, buildInfoConfig = buildInfoConfig
, dbPool = dbPool
, s3Client = s3Client
, httpClientManager = httpClientManager
}
result <- liftIO $ runDBMigrations baseContext
case result of
Just error -> return . Just $ error
Nothing -> do
liftIO $ race_ (takeMVar shutdownFlag) (concurrently (runWebServer baseContext) (runWorker shutdownFlag baseContext))
return Nothing
forM_ result die
runApplication =
runWebServerWithWorkers
asciiLogo
serverConfigFile
validateServerConfig
buildInfoFile
createBaseContext
ProdDB.migrationDefinitions
DevDB.runMigration
afterDbMigrationHook
runWebServer
runWorker

createBaseContext :: (MonadIO m, MonadLogger m) => ServerConfig -> BuildInfoConfig -> Pool Connection -> MinioConn -> Manager -> MVar () -> m BaseContext
createBaseContext serverConfig buildInfoConfig dbPool s3Client httpClientManager shutdownFlag = return BaseContext {..}

afterDbMigrationHook :: BaseContext -> IO ()
afterDbMigrationHook _ = return ()

runWebServer :: BaseContext -> IO ()
runWebServer context = runWebServerFactory context getSentryIdentity loggingMiddleware webApi webServer

runWorker :: MVar () -> BaseContext -> IO ()
runWorker shutdownFlag context =
worker runAppContextWithBaseContext runAppContextWithBaseContext'' shutdownFlag context workers permanentWorker
31 changes: 0 additions & 31 deletions registry-server/src/Registry/Bootstrap/Common.hs

This file was deleted.

15 changes: 0 additions & 15 deletions registry-server/src/Registry/Bootstrap/DatabaseMigration.hs

This file was deleted.

Loading

0 comments on commit e4593d3

Please sign in to comment.