Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace scotty with twain #121

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 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 @@ -40,11 +41,12 @@ import System.Console.GetOpt ( ArgDescr(..)
import System.Directory ( createDirectoryIfMissing )
import System.Environment ( getArgs )
import System.IO ( stdout )
import Web.Scotty ( get
import Data.Text.Lazy.Encoding ( encodeUtf8 )
import Web.Twain ( get
, html
, middleware
, redirect
, scotty
, notFound
, redirect302
, send
)

import Config
Expand Down Expand Up @@ -106,8 +108,10 @@ 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
(logStdout . static $(embedDir "static") <$>)
(notFound (send $ html "not found..."))
[ get "/" (liftIO (readIORef viewRef) >>= send . html . encodeUtf8 . render)
, get "/r" (liftIO (tryPutMVar upd ()) >> send (redirect302 "/"))
]
4 changes: 3 additions & 1 deletion mat-chalmers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ executable mat-chalmers
, logging-effect
, mtl
, prettyprinter
, scotty >= 0.20 && < 0.30
, text
, time >= 1.12.2 && < 1.13
, twain >= 2.1.2.0 && < 3.0.0.0
, wai-extra >= 3.1.14 && < 4.0
, wai-middleware-static-embedded == 0.1.0.0
, directory >= 1.3.5.0 && < 1.4
, warp >= 3.4.0 && < 4.0.0
, async >= 2.2.5 && <= 3.0
default-language: GHC2021

Expand Down
Loading