aboutsummaryrefslogtreecommitdiff
path: root/hsm-web/Hsm/Web.hs
diff options
context:
space:
mode:
Diffstat (limited to 'hsm-web/Hsm/Web.hs')
-rw-r--r--hsm-web/Hsm/Web.hs32
1 files changed, 22 insertions, 10 deletions
diff --git a/hsm-web/Hsm/Web.hs b/hsm-web/Hsm/Web.hs
index ae8b1f2..4f6fb6e 100644
--- a/hsm-web/Hsm/Web.hs
+++ b/hsm-web/Hsm/Web.hs
@@ -8,15 +8,24 @@ module Hsm.Web
)
where
-import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, (:>))
-import Effectful.Dispatch.Static (SideEffects (WithSideEffects), StaticRep, evalStaticRep, getStaticRep, unsafeEff)
+import Data.Aeson (encode)
+import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, liftIO, (:>))
+import Effectful.Dispatch.Static
+ ( SideEffects (WithSideEffects)
+ , StaticRep
+ , evalStaticRep
+ , getStaticRep
+ , unEff
+ , unsafeEff
+ )
import Effectful.Dispatch.Static.Primitive (Env)
import Effectful.Exception (finally)
-import Hsm.Log (Log, Logs, Severity (Info, Trace), logMsg, makeLoggerIO)
+import Hsm.INA226 (I2CINA226, INA226, readINA226State)
+import Hsm.Log (Logs, Severity (Info, Trace), logMsg, makeLoggerIO)
import Network.Wai.Handler.Warp (defaultSettings, setLogger)
import Network.Wai.Middleware.Static (addBase, noDots, staticPolicy, (>->))
import Paths_hsm_web (getDataFileName)
-import Web.Scotty (Options (settings, verbose), defaultOptions, file, get, middleware, scottyOpts)
+import Web.Scotty (Options (settings, verbose), defaultOptions, file, get, middleware, raw, scottyOpts, setHeader)
data Web (a :: * -> *) (b :: *)
@@ -25,21 +34,24 @@ type instance DispatchOf Web = Static WithSideEffects
newtype instance StaticRep Web
= Web Options
-server :: Options -> Env es -> IO ()
-server options _ = do
+server :: (I2CINA226 :> es, INA226 :> es, Logs '["i2c", "ina226"] es) => Options -> Env es -> IO ()
+server options env = do
dist <- getDataFileName "Client/dist/"
scottyOpts options $ do
- get "/" . file $ dist <> "index.html"
middleware . staticPolicy $ noDots >-> addBase dist
+ get "/" . file $ dist <> "index.html"
+ get "/ina226" $ do
+ setHeader "Content-Type" "application/json"
+ liftIO (unEff readINA226State env) >>= raw . encode
-runServer :: (Log "web" :> es, Web :> es) => Eff es ()
+runServer :: (I2CINA226 :> es, INA226 :> es, Logs '["i2c", "ina226", "web"] es, Web :> es) => Eff es ()
runServer = finally startServer stopServer
where
startServer = do
Web options <- getStaticRep
- logMsg Info "Starting scotty web server"
+ logMsg @"web" Info "Starting scotty web server"
unsafeEff $ server options
- stopServer = logMsg Info "Stopping scotty web server"
+ stopServer = logMsg @"web" Info "Stopping scotty web server"
runWeb :: (IOE :> es, Logs '["scotty", "web"] es) => Eff (Web : es) a -> Eff es a
runWeb action = do