Astro SQLite db added & adjusted.

This commit is contained in:
Alexander Granin 2020-02-03 01:37:29 +07:00
parent 7929263a17
commit 15bfb37cb8
4 changed files with 31 additions and 13 deletions

View File

@ -75,7 +75,7 @@ doOrFail :: Show e => L.AppL (Either e a) -> L.AppL a
doOrFail = doOrFail' OperationFailedException
connectOrFail :: D.DBConfig BS.SqliteM -> L.AppL (D.SqlConn BS.SqliteM)
connectOrFail cfg = doOrFail' ConnectionFailedException $ L.initSqlDB cfg
connectOrFail cfg = doOrFail' ConnectionFailedException $ L.getOrInitSqlConn cfg
getMeteors :: Maybe Int -> Maybe Int -> D.SqlConn BS.SqliteM -> L.AppL Meteors
@ -138,10 +138,7 @@ createMeteor mtp@(MeteorTemplate {..}) conn = do
$ B.all_ (SqlDB._meteors SqlDB.astroDb)
pure $ SqlDB._meteorId $ fromJust m
dbConfig :: D.DBConfig BS.SqliteM
-- dbConfig = D.mkSQLiteConfig "/tmp/astro.db"
dbConfig = D.mkSQLiteConfig "./astro.db"
-- TODO: move into the framework.
withDB
:: D.DBConfig BS.SqliteM
-> (D.SqlConn BS.SqliteM -> L.AppL a)

View File

@ -6,12 +6,7 @@
module Main where
import Control.Monad
import Hydra.Prelude
import qualified Hydra.Domain as D
import qualified Hydra.Runtime as R
import qualified Hydra.Interpreters as R
import qualified Hydra.Language as L
import System.Process (readCreateProcess, shell)
import Network.Wai.Handler.Warp (run)
import Servant
import Data.Time
@ -23,6 +18,12 @@ import qualified Database.Beam.Sqlite as BS
import qualified Database.Beam.Backend.SQL as B
import Database.Beam ((==.), (&&.), (<-.), (/=.), (==?.))
import Hydra.Prelude
import qualified Hydra.Domain as D
import qualified Hydra.Runtime as R
import qualified Hydra.Interpreters as R
import qualified Hydra.Language as L
import Astro.Domain.Meteor
import Astro.Catalogue
import Astro.Types
@ -62,7 +63,12 @@ astroBackendApp = serve astroAPI . astroServer
runApp :: L.AppL a -> AppHandler a
runApp flow = do
Env rt _ <- ask
lift $ lift $ R.runAppL rt flow
eRes <- lift $ lift $ try $ R.runAppL rt flow
case eRes of
Left (err :: SomeException) -> do
liftIO $ putStrLn @String $ "Exception handled: " <> show err
throwError err500
Right res -> pure res
astroServer' :: AppServer
@ -70,6 +76,10 @@ astroServer'
= meteors
:<|> meteor
-- TODO: configs from the command line
dbConfig :: D.DBConfig BS.SqliteM
dbConfig = D.mkSQLiteConfig "/tmp/astro.db"
meteors :: Maybe Int -> Maybe Int -> AppHandler Meteors
meteors mbMass mbSize = runApp
$ withDB dbConfig
@ -89,9 +99,20 @@ loggerCfg = D.LoggerConfig
, D._logToFile = False
}
prepareSQLiteDB :: IO ()
prepareSQLiteDB = do
putStrLn @String "Copying astro_template.db to /tmp/astro.db..."
eRes <- try $ void $ readCreateProcess (shell "cp ./app/astro/astro_template.db /tmp/astro.db") ""
case eRes of
Left (err :: SomeException) -> do
putStrLn @String $ "Exception got: " <> show err
error $ "Exception got: " <> show err
Right _ -> pure ()
main :: IO ()
main = do
putStrLn ("Starting Astro App Server..." :: String)
prepareSQLiteDB
putStrLn @String "Starting Astro App Server..."
R.withAppRuntime (Just loggerCfg) $ \rt -> do
appSt <- R.runAppL rt $ initState AppConfig
run 8080 $ astroBackendApp $ Env rt appSt

Binary file not shown.

Binary file not shown.