Skip to content

Commit

Permalink
Update /api/status to match output from lorax-composer
Browse files Browse the repository at this point in the history
Fixes issue weldr#71

Output now looks like:

{
    "build": "b18d239",
    "backend": "weldr",
    "schema_version": "4",
    "db_supported": true,
    "api": "0",
    "db_version": "4"
}
  • Loading branch information
bcl committed May 15, 2018
1 parent 7c27b23 commit 27282a2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
38 changes: 20 additions & 18 deletions src/BDCS/API/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import BDCS.API.Config(ServerConfig(..))
import BDCS.API.Recipes(openOrCreateRepo, commitRecipeDirectory)
import BDCS.API.Utils(GitLock(..))
import BDCS.API.V0(V0API, v0ApiServer)
import BDCS.API.Version(apiVersion)
import BDCS.API.Version(buildVersion)
import BDCS.DB(schemaVersion, getDbVersion)
import Control.Concurrent.Async(Async, async, cancel, replicateConcurrently_, waitCatch)
import qualified Control.Concurrent.ReadWriteLock as RWL
Expand All @@ -49,7 +49,6 @@ import Control.Monad.Except(runExceptT)
import Control.Monad.Logger(runFileLoggingT, runStderrLoggingT)
import Control.Monad.STM(atomically)
import Data.Aeson
import Data.Int(Int64)
import Data.IORef(IORef, atomicModifyIORef', newIORef, readIORef)
import qualified Data.Map as Map
import Data.Sequence((|>), Seq(..), deleteAt, empty, findIndexL, index)
Expand All @@ -70,28 +69,31 @@ type InProgressMap = Map.Map T.Text (Async (), ComposeInfo)

-- | The status of the server, the database, and the API.
data ServerStatus = ServerStatus
{ srvBackend :: String -- ^ Backend implementation (weldr, lorax-composer)
, srvVersion :: String -- ^ Server version
, srvSchema :: Int64 -- ^ Supported Database Schema version
, srvDb :: Int64 -- ^ Database version
, srvSupported :: Bool -- ^ True if the Database is supported by the Server
{ srvApi :: String -- ^ Supported API version
, srvBackend :: String -- ^ Backend implementation (weldr, lorax-composer)
, srvBuild :: String -- ^ Server build version
, srvSchemaVersion :: String -- ^ Supported Database Schema version
, srvDbVersion :: String -- ^ Database version
, srvDbSupported :: Bool -- ^ True if the Database is supported by the Server
} deriving (Eq, Show)

instance ToJSON ServerStatus where
toJSON ServerStatus{..} = object
[ "backend" .= srvBackend
, "version" .= srvVersion
, "schema" .= srvSchema
, "db" .= srvDb
, "supported" .= srvSupported ]
[ "api" .= srvApi
, "backend" .= srvBackend
, "build" .= srvBuild
, "schema_version" .= srvSchemaVersion
, "db_version" .= srvDbVersion
, "db_supported" .= srvDbSupported ]

instance FromJSON ServerStatus where
parseJSON = withObject "server status" $ \o -> do
srvBackend <- o .: "backend"
srvVersion <- o .: "version"
srvSchema <- o .: "schema"
srvDb <- o .: "db"
srvSupported <- o .: "supported"
srvApi <- o .: "api"
srvBackend <- o .: "backend"
srvBuild <- o .: "build"
srvSchemaVersion <- o .: "schema_version"
srvDbVersion <- o .: "db_version"
srvDbSupported <- o .: "db_supported"
return ServerStatus{..}

-- | The /status route
Expand All @@ -105,7 +107,7 @@ maxComposes = 1
serverStatus :: ServerConfig -> Handler ServerStatus
serverStatus ServerConfig{..} = do
version <- dbVersion
return (ServerStatus "weldr" apiVersion schemaVersion version (schemaVersion == version))
return (ServerStatus "0" "weldr" buildVersion (show schemaVersion) (show version) (schemaVersion == version))
where
dbVersion = do
result <- runExceptT $ runSqlPool getDbVersion cfgPool
Expand Down
6 changes: 3 additions & 3 deletions src/BDCS/API/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
-- along with bdcs-api. If not, see <http://www.gnu.org/licenses/>.
{-# LANGUAGE TemplateHaskell #-}

module BDCS.API.Version(apiVersion)
module BDCS.API.Version(buildVersion)
where

import Data.Version (showVersion)
import Development.GitRev
import Paths_bdcs_api(version)

apiVersion :: String
apiVersion = do
buildVersion :: String
buildVersion = do
let git_version = $(gitDescribe)
if git_version == "UNKNOWN" then
"v" ++ showVersion version
Expand Down
6 changes: 3 additions & 3 deletions tests/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,17 @@ modulesListResponse4 = ModulesListResponse
checkStatusResponse :: ClientM Bool
checkStatusResponse = do
response <- getStatus
return $ weldrOK response && schemaOK response && dbOK response && srvSupported response
return $ weldrOK response && schemaOK response && dbOK response && srvDbSupported response
where
weldrOK :: ServerStatus -> Bool
weldrOK response = srvBackend response == "weldr"

schemaOK :: ServerStatus -> Bool
schemaOK response = srvSchema response == schemaVersion
schemaOK response = srvSchemaVersion response == show schemaVersion

-- During testing the schema and the database should always be equal
dbOK :: ServerStatus -> Bool
dbOK response = srvDb response == schemaVersion
dbOK response = srvDbVersion response == show schemaVersion



Expand Down
4 changes: 2 additions & 2 deletions tools/bdcs-api-server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-- along with bdcs-api. If not, see <http://www.gnu.org/licenses/>.

import BDCS.API.Server(runServer)
import BDCS.API.Version(apiVersion)
import BDCS.API.Version(buildVersion)
import Cmdline(CliOptions(..),
parseArgs)
import Control.Monad(when)
Expand All @@ -25,6 +25,6 @@ main :: IO ()
main = do
opts <- parseArgs

when (optShowVersion opts) $ putStrLn ("bdcs-api " ++ apiVersion)
when (optShowVersion opts) $ putStrLn ("bdcs-api " ++ buildVersion)

runServer (optPort opts) (optBDCS opts) (optRecipeRepo opts) (optMetadataDB opts)

0 comments on commit 27282a2

Please sign in to comment.