Merge pull request #25 from Start9Labs/feature/app-endpoints

endpoint and config fixes
This commit is contained in:
Lucy C
2020-10-10 19:14:46 -06:00
committed by GitHub
6 changed files with 57 additions and 26 deletions

View File

@@ -1,8 +1,8 @@
/apps AppsManifestR GET -- get current apps listing /apps AppsManifestR GET -- get current apps listing
/apps/#S9PK/#Text/manifest AppManifestR GET -- get app manifest from appmgr /apps/manifest/#AppIdentifier AppManifestR GET -- get app manifest from appmgr -- ?spec={semver-spec}
/apps/#S9PK/#Text/config AppConfigR GET -- get app config from appmgr /apps/config/#AppIdentifier AppConfigR GET -- get app config from appmgr -- ?spec={semver-spec}
/version VersionR GET /version VersionR GET

View File

@@ -17,6 +17,7 @@ import qualified Yesod.Core.Unsafe as Unsafe
import Settings import Settings
import Yesod.Persist.Core import Yesod.Persist.Core
import Lib.Types.AppIndex
-- | The foundation datatype for your application. This can be a good place to -- | The foundation datatype for your application. This can be a good place to
-- keep settings and values requiring initialization before your application -- keep settings and values requiring initialization before your application

View File

@@ -44,6 +44,8 @@ import Lib.External.AppMgr
import Settings import Settings
import Database.Queries import Database.Queries
import Network.Wai ( Request(requestHeaderUserAgent) ) import Network.Wai ( Request(requestHeaderUserAgent) )
import Util.Shared
pureLog :: Show a => a -> Handler a pureLog :: Show a => a -> Handler a
pureLog = liftA2 (*>) ($logInfo . show) pure pureLog = liftA2 (*>) ($logInfo . show) pure
@@ -86,19 +88,33 @@ getSysR e = do
sysResourceDir <- (</> "sys") . resourcesDir . appSettings <$> getYesod sysResourceDir <- (</> "sys") . resourcesDir . appSettings <$> getYesod
getApp sysResourceDir e getApp sysResourceDir e
getAppManifestR :: Extension "s9pk" -> Text -> Handler TypedContent getAppManifestR :: AppIdentifier -> Handler TypedContent
getAppManifestR e@(Extension appId) v = do getAppManifestR appId = do
appMgrDir <- (<> "/") . staticBinDir . appSettings <$> getYesod appSettings <- appSettings <$> getYesod
appDir <- (<> "/") . (</> toS v) . (</> appId) . (</> "apps") . resourcesDir . appSettings <$> getYesod let appsDir = (</> "apps") . resourcesDir $ appSettings
manifest <- handleS9ErrT $ getManifest appMgrDir appDir e let appMgrDir = staticBinDir $ appSettings
av <- getVersionFromQuery appsDir appExt >>= \case
Nothing -> sendResponseStatus status400 ("Specified App Version Not Found" :: Text)
Just v -> pure v
let appDir = (<> "/") . (</> show av) . (</> toS appId) $ appsDir
manifest <- handleS9ErrT $ getManifest appMgrDir appDir appExt
pure $ TypedContent "application/json" (toContent manifest) pure $ TypedContent "application/json" (toContent manifest)
where
appExt = Extension (toS appId) :: Extension "s9pk"
getAppConfigR :: Extension "s9pk" -> Text -> Handler TypedContent getAppConfigR :: AppIdentifier -> Handler TypedContent
getAppConfigR e@(Extension appId) v = do getAppConfigR appId = do
appMgrDir <- (<> "/") . staticBinDir . appSettings <$> getYesod appSettings <- appSettings <$> getYesod
appDir <- (<> "/") . (</> toS v) . (</> appId) . (</> "apps") . resourcesDir . appSettings <$> getYesod let appsDir = (</> "apps") . resourcesDir $ appSettings
config <- handleS9ErrT $ getConfig appMgrDir appDir e let appMgrDir = staticBinDir $ appSettings
av <- getVersionFromQuery appsDir appExt >>= \case
Nothing -> sendResponseStatus status400 ("Specified App Version Not Found" :: Text)
Just v -> pure v
let appDir = (<> "/") . (</> show av) . (</> toS appId) $ appsDir
config <- handleS9ErrT $ getConfig appMgrDir appDir appExt
pure $ TypedContent "application/json" (toContent config) pure $ TypedContent "application/json" (toContent config)
where
appExt = Extension (toS appId) :: Extension "s9pk"
getAppR :: Extension "s9pk" -> Handler TypedContent getAppR :: Extension "s9pk" -> Handler TypedContent
getAppR e = do getAppR e = do

View File

@@ -9,18 +9,15 @@ module Handler.Version where
import Startlude import Startlude
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import Data.Char
import qualified Data.Text as T
import Network.HTTP.Types
import Yesod.Core import Yesod.Core
import Foundation import Foundation
import Handler.Types.Status import Handler.Types.Status
import Lib.Registry import Lib.Registry
import Lib.Semver
import Lib.Types.Semver import Lib.Types.Semver
import Settings import Settings
import System.FilePath ( (</>) ) import System.FilePath ( (</>) )
import Util.Shared
getVersionR :: Handler AppVersionRes getVersionR :: Handler AppVersionRes
getVersionR = do getVersionR = do
@@ -42,10 +39,5 @@ getVersionSysR sysAppId = runMaybeT $ do
getVersionWSpec :: KnownSymbol a => FilePath -> Extension a -> Handler (Maybe AppVersionRes) getVersionWSpec :: KnownSymbol a => FilePath -> Extension a -> Handler (Maybe AppVersionRes)
getVersionWSpec rootDir ext = do getVersionWSpec rootDir ext = do
specString <- T.filter (not . isSpace) . fromMaybe "*" <$> lookupGetParam "spec" av <- getVersionFromQuery rootDir ext
spec <- case readMaybe specString of pure $ liftA2 AppVersionRes av (pure Nothing)
Nothing -> sendResponseStatus status400 ("Invalid App Version Specification" :: Text)
Just t -> pure t
appVersions <- liftIO $ getAvailableAppVersions rootDir ext
let av = version <$> getSpecifiedAppVersion spec appVersions
pure $ liftA2 AppVersionRes av (pure Nothing)

View File

@@ -44,14 +44,14 @@ readProcessInheritStderr a b c = liftIO $ do
getConfig :: (MonadIO m, KnownSymbol a) => FilePath -> FilePath -> Extension a -> S9ErrT m Text getConfig :: (MonadIO m, KnownSymbol a) => FilePath -> FilePath -> Extension a -> S9ErrT m Text
getConfig appmgrPath appPath e@(Extension appId) = fmap decodeUtf8 $ do getConfig appmgrPath appPath e@(Extension appId) = fmap decodeUtf8 $ do
(ec, out) <- readProcessInheritStderr (appmgrPath <> "appmgr") ["inspect", "info", appPath <> show e, "-C", "--json"] "" (ec, out) <- readProcessInheritStderr (appmgrPath <> "appmgr") ["inspect", "info", appPath <> (show e), "-C", "--json"] ""
case ec of case ec of
ExitSuccess -> pure out ExitSuccess -> pure out
ExitFailure n -> throwE $ AppMgrE [i|info #{appId} -C \--json|] n ExitFailure n -> throwE $ AppMgrE [i|info #{appId} -C \--json|] n
getManifest :: (MonadIO m, KnownSymbol a) => FilePath -> FilePath -> Extension a -> S9ErrT m ByteString getManifest :: (MonadIO m, KnownSymbol a) => FilePath -> FilePath -> Extension a -> S9ErrT m ByteString
getManifest appmgrPath appPath e@(Extension appId) = do getManifest appmgrPath appPath e@(Extension appId) = do
(ec, bs) <- readProcessInheritStderr (appmgrPath <> "appmgr") ["inspect", "info", appPath <> show e, "-M", "--json"] "" (ec, bs) <- readProcessInheritStderr (appmgrPath <> "appmgr") ["inspect", "info", appPath <> (show e), "-M", "--json"] ""
case ec of case ec of
ExitSuccess -> pure bs ExitSuccess -> pure bs
ExitFailure n -> throwE $ AppMgrE [i|info -M #{appId} \--json|] n ExitFailure n -> throwE $ AppMgrE [i|info -M #{appId} \--json|] n

22
src/Util/Shared.hs Normal file
View File

@@ -0,0 +1,22 @@
module Util.Shared where
import Startlude
import Data.Char
import qualified Data.Text as T
import Network.HTTP.Types
import Yesod.Core
import Foundation
import Lib.Registry
import Lib.Semver
import Lib.Types.Semver
getVersionFromQuery :: KnownSymbol a => FilePath -> Extension a -> Handler (Maybe AppVersion)
getVersionFromQuery rootDir ext = do
specString <- T.filter (not . isSpace) . fromMaybe "*" <$> lookupGetParam "spec"
spec <- case readMaybe specString of
Nothing -> sendResponseStatus status400 ("Invalid App Version Specification" :: Text)
Just t -> pure t
appVersions <- liftIO $ getAvailableAppVersions rootDir ext
pure $ version <$> getSpecifiedAppVersion spec appVersions