A peer-to-peer stack for code collaboration
Add logging to `radicle-server`
Thomas Scholtes9c44c26ec923
diff --git a/exe/Server.hs b/exe/Server.hsindex 9b5810c..5c90cab 100644--- a/exe/Server.hs+++ b/exe/Server.hs@@ -8,6 +8,7 @@ import qualified Data.Aeson as Aimport Data.ByteString.Lazy (fromStrict)import qualified Data.Map as Mapimport qualified Data.Sequence as Seq+import qualified Data.Text as Timport Database.PostgreSQL.Simple(ConnectInfo(..), Connection, connect)import Network.Wai.Handler.Warp (run)@@ -78,13 +79,19 @@ serverApi = ProxyinsertExpr :: Connection -> Chains -> Text -> [Value] -> IO (Either Text ())insertExpr conn chains name vals = modifyMVar (getChains chains) $ \c -> do- let x = Map.lookup name c- let chain = fromMaybe (Chain name pureEnv mempty) x+ let maybeChain = Map.lookup name c+ chain <- case maybeChain of+ Nothing -> do+ logInfo "Creating new machine" [("machine", name)]+ pure $ Chain name pureEnv mempty+ Just chain' -> pure chain'let (r :: Either (LangError Value) [Value], newSt) = runIdentity$ runLang (chainState chain)$ traverse eval valscase r of- Left e -> pure . (c ,) . Left $ renderPrettyDef e+ Left e -> do+ logInfo "Submitted expressions failed to evaluate" [("machine", name)]+ pure . (c ,) . Left $ renderPrettyDef eRight valsRes -> dotraverse_ (insertExprDB conn name) valslet news = Seq.fromList $ zip vals valsRes@@ -112,6 +119,7 @@ loadState conn = do}pure (name, c)chains' <- newMVar $ Map.fromList chainPairs+ logInfo "Loaded machines into memory" [("machine-count", show $ length chainPairs)]pure $ Chains chains'@@ -124,6 +132,7 @@ loadState conn = do-- guarantee).submit :: Connection -> Chains -> Text -> Values -> Handler Valuesubmit conn chains name (Values vals) = do+ logInfo "Submitted expressions" [("machine", name), ("expression-count", show $ length vals)]res <- liftIO $ insertExpr conn chains name valscase res ofLeft err -> throwError $ err400 { errBody = fromStrict $ encodeUtf8 err }@@ -132,7 +141,9 @@ submit conn chains name (Values vals) = do-- | Get all expressions submitted to a chain since 'index'.since :: Connection -> Text -> Int -> Handler Values-since conn name index = liftIO $ Values <$> getSinceDB conn name index+since conn name index = do+ logInfo "Requested expressions" [("machine", name), ("from-index", show index)]+ liftIO $ Values <$> getSinceDB conn name indexjsonOutputs :: Chains -> Text -> Handler [Maybe A.Value]jsonOutputs st name = do@@ -144,6 +155,21 @@ jsonOutputs st name = dostatic :: Server Rawstatic = serveDirectoryFileServer "static/"+-- | Log a message to @stdout@.+--+-- The second argument is a list of key-value pairs that will be joined with "="+-- and appended to the message.+--+-- @+-- logInfo "the message" [("foo", "5)]+-- -- prints "INFO the message foo=5"+-- @+logInfo :: MonadIO m => Text -> [(Text, Text)] -> m ()+logInfo msg dat = do+ putStrLn $ "INFO " <> msg <> datString+ where+ datString = T.intercalate "" $ map (\(key, val) -> " " <> key <> "=" <> val) dat+-- * Optsdata Opts = Opts@@ -208,6 +234,7 @@ main = dost <- loadState connlet gzipSettings = def { gzipFiles = GzipPreCompressed GzipIgnore }app = simpleCors $ gzip gzipSettings (serve serverApi (server conn st))+ logInfo "Start listening" [("port", show $ serverPort opts')]run (serverPort opts') appwhereallOpts = info (opts <**> helper)diff --git a/package.yaml b/package.yamlindex 291f903..6351fd0 100644--- a/package.yaml+++ b/package.yaml@@ -112,6 +112,7 @@ executables:- postgresql-simple- servant- servant-server+ - text- warp- wai-extra- wai-cors