Skip to content

Commit

Permalink
Replace Scotty with Twain
Browse files Browse the repository at this point in the history
This works when running `cabal run`, but I can't build it with nix,
probably because of versions.

I don't know if this version if cleaner than the one with Scotty, but
it's a bit cleaner than the previous version of this PR.
  • Loading branch information
Rembane committed May 30, 2024
1 parent 85dadfc commit b21e770
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
36 changes: 27 additions & 9 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Data.Time.Format ( defaultTimeLocale
import Lens.Micro.Platform ( set
, view
)
import Network.Wai.Handler.Warp ( run )
import Network.Wai.Middleware.RequestLogger ( logStdout )
import Network.Wai.Middleware.StaticEmbedded ( static )
import System.Console.GetOpt ( ArgDescr(..)
Expand All @@ -39,11 +40,13 @@ import System.Console.GetOpt ( ArgDescr(..)
)
import System.Environment ( getArgs )
import System.IO ( stdout )
import Web.Scotty ( get
import Data.Text.Lazy.Encoding ( encodeUtf8 )
import Web.Twain ( ResponderM
, get
, html
, middleware
, redirect
, scotty
, notFound
, redirect302
, send
)

import Config
Expand Down Expand Up @@ -102,8 +105,23 @@ webserver
-> IORef View -- ^ View model
-> MVar () -- ^ Update signal
-> IO ()
webserver conf viewRef upd = scotty (view cPort conf) $ do
middleware logStdout
middleware (static $(embedDir "static"))
get "/" ((html . render) =<< liftIO (readIORef viewRef))
get "/r" (liftIO (tryPutMVar upd ()) >> redirect "/") -- force update
webserver Config{_cPort=webserverPort} viewRef upd =
run webserverPort $ foldr ($) (notFound missing)
[ middleware . get "/" index
, middleware . get "/r" forceUpdate
]
where
index :: ResponderM a
index = do
theCurrentView <- liftIO (readIORef viewRef)
(send . html . encodeUtf8 . render) theCurrentView

forceUpdate :: ResponderM a
forceUpdate = do
_ <- liftIO $ tryPutMVar upd ()
send $ redirect302 "/"

missing :: ResponderM a
missing = send $ html "Not found..."

middleware = logStdout . static $(embedDir "static")
4 changes: 3 additions & 1 deletion mat-chalmers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ executable mat-chalmers
, mtl
, req
, prettyprinter
, scotty >= 0.12.1 && < 0.13
, text
, time >= 1.12 && < 1.13
, twain >= 2.1.2.0 && < 3.0.0.0
, wai-extra >= 3.1.13.0 && < 4.0
, wai-middleware-static-embedded == 0.1.0.0
, warp == 3.3.30
, async >= 2.2.4 && <= 3.0
default-language: Haskell2010

Expand Down

0 comments on commit b21e770

Please sign in to comment.