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 authored and The1Penguin committed Oct 21, 2024
1 parent c8de91a commit c0d44bb
Showing 1 changed file with 27 additions and 9 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 @@ -40,11 +41,13 @@ 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 ( ResponderM
, get
, html
, middleware
, redirect
, scotty
, notFound
, redirect302
, send
)

import Config
Expand Down Expand Up @@ -106,8 +109,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")

0 comments on commit c0d44bb

Please sign in to comment.