diff --git a/.gitignore b/.gitignore index 8838380..65bca3c 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ start9-registry.ps shell.nix testdata/ lbuild.sh +icon \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index 53baf7b..7a2a33c 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -37,6 +37,8 @@ static-bin-dir: "_env:STATIC_BIN:/usr/local/bin/" error-log-root: "_env:ERROR_LOG_ROOT:/var/log/registry/" marketplace-name: "_env:MARKETPLACE_NAME:CHANGE ME" icon-path: "_env:ICON_PATH:/var/www/html/resources" +max-eos-version: "_env:MAX_VERSION:0.3.3.0" +run-migration: "_env:RUN_MIGRATION:false" database: database: "_env:PG_DATABASE:start9_registry" diff --git a/src/Application.hs b/src/Application.hs index b0ded17..839a590 100644 --- a/src/Application.hs +++ b/src/Application.hs @@ -254,11 +254,14 @@ makeFoundation appSettings = do flip runLoggingT logFunc $ createPostgresqlPool (pgConnStr $ appDatabaseConf appSettings) (pgPoolSize . appDatabaseConf $ appSettings) - runSqlPool - (Database.Persist.Migration.Postgres.runMigration Database.Persist.Migration.defaultSettings manualMigration) - pool - -- Preform database migration using application logging settings - runLoggingT (runSqlPool (runMigration migrateAll) pool) logFunc + if (needsMigration appSettings) + then + runSqlPool + (Database.Persist.Migration.Postgres.runMigration Database.Persist.Migration.defaultSettings manualMigration) + pool + else + -- Preform database migration using application logging settings + runLoggingT (runSqlPool (runMigration migrateAll) pool) logFunc -- Return the foundation return $ mkFoundation pool diff --git a/src/Handler/Eos/V0/Latest.hs b/src/Handler/Eos/V0/Latest.hs index 4cf4990..371f04d 100644 --- a/src/Handler/Eos/V0/Latest.hs +++ b/src/Handler/Eos/V0/Latest.hs @@ -1,4 +1,5 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE TemplateHaskell #-} module Handler.Eos.V0.Latest where @@ -13,15 +14,18 @@ import Database.Esqueleto.Experimental ( table, (^.), ) -import Foundation (Handler) +import Foundation (Handler, RegistryCtx (appSettings)) import Handler.Package.V0.ReleaseNotes (ReleaseNotes (..)) import Handler.Util (queryParamAs, tickleMAU) import Lib.Types.Emver (Version, parseVersion) import Model (EntityField (..), OsVersion (..)) import Orphans.Emver () -import Startlude (Bool (..), Down (..), Eq, Generic, Maybe, Ord ((<)), Show, Text, const, filter, fst, head, maybe, pure, sortOn, ($), (&&&), (.), (<$>), (<&>)) -import Yesod (ToContent (toContent), ToTypedContent (..), YesodPersist (runDB)) +import Startlude (Bool (..), Down (..), Eq, Generic, Maybe (..), Ord ((<)), Show, Text, const, filter, fst, head, maybe, pure, sortOn, ($), (&&&), (.), (<$>), (<&>), (<=)) +import Yesod (ToContent (toContent), ToTypedContent (..), YesodPersist (runDB), getsYesod, sendResponseStatus) import Yesod.Core.Types (JSONResponse (..)) +import Settings (AppSettings(maxEosVersion)) +import Network.HTTP.Types (status400) +import Lib.Error (S9Error(InvalidParamsE)) data EosRes = EosRes @@ -41,26 +45,36 @@ instance ToTypedContent EosRes where getEosVersionR :: Handler (JSONResponse (Maybe EosRes)) getEosVersionR = do - eosVersion <- queryParamAs "eos-version" parseVersion - allEosVersions <- runDB $ - select $ do - vers <- from $ table @OsVersion - orderBy [desc (vers ^. OsVersionCreatedAt)] - pure vers - let osV = entityVal <$> allEosVersions - let mLatest = head osV - let mappedVersions = - ReleaseNotes $ - HM.fromList $ - sortOn (Down . fst) $ - filter (maybe (const True) (<) eosVersion . fst) $ - ((osVersionNumber &&& osVersionReleaseNotes)) - <$> osV - tickleMAU - pure . JSONResponse $ - mLatest <&> \latest -> - EosRes - { eosResVersion = osVersionNumber latest - , eosResHeadline = osVersionHeadline latest - , eosResReleaseNotes = mappedVersions - } + currentEosVersion <- queryParamAs "eos-version" parseVersion + case currentEosVersion of + Nothing -> sendResponseStatus status400 (InvalidParamsE "Param is required" "eos-version") + Just currentEosVersion' -> do + maxVersion <- getsYesod $ maxEosVersion . appSettings + allEosVersions <- runDB $ + select $ do + vers <- from $ table @OsVersion + orderBy [desc (vers ^. OsVersionNumber)] + pure vers + let osV = determineMaxEosVersionAvailable maxVersion currentEosVersion' $ entityVal <$> allEosVersions + let mLatest = head osV + let mappedVersions = + ReleaseNotes $ + HM.fromList $ + sortOn (Down . fst) $ + filter (maybe (const True) (<) currentEosVersion . fst) $ + ((osVersionNumber &&& osVersionReleaseNotes)) + <$> osV + tickleMAU + pure . JSONResponse $ + mLatest <&> \latest -> + EosRes + { eosResVersion = osVersionNumber latest + , eosResHeadline = osVersionHeadline latest + , eosResReleaseNotes = mappedVersions + } + +determineMaxEosVersionAvailable :: Version -> Version -> [OsVersion] -> [OsVersion] +determineMaxEosVersionAvailable maxEosVersion currentEosVersion versions = do + if (currentEosVersion < maxEosVersion) + then sortOn (Down . osVersionNumber) $ filter (\v -> osVersionNumber v <= maxEosVersion) $ versions + else versions \ No newline at end of file diff --git a/src/Settings.hs b/src/Settings.hs index a553d97..dc90d84 100644 --- a/src/Settings.hs +++ b/src/Settings.hs @@ -76,9 +76,11 @@ data AppSettings = AppSettings , iconPath :: !FilePath , errorLogRoot :: !FilePath , marketplaceName :: !Text + , maxEosVersion :: !Version , registryHostname :: !Text , registryVersion :: !Version , resourcesDir :: !FilePath + , needsMigration :: !Bool , sslAuto :: !Bool , sslCertLocation :: !FilePath , sslCsrLocation :: !FilePath @@ -110,8 +112,10 @@ instance FromJSON AppSettings where errorLogRoot <- o .: "error-log-root" iconPath <- o .: "icon-path" marketplaceName <- o .: "marketplace-name" + maxEosVersion <- o .: "max-eos-version" registryHostname <- o .: "registry-hostname" resourcesDir <- o .: "resources-path" + needsMigration <- o .: "run-migration" sslAuto <- o .: "ssl-auto" sslPath <- o .: "ssl-path" staticBinDir <- o .: "static-bin-dir"