commit 0d4da6ec93d6cc4a9fcc3f5d8ffe62adeedf5596
parent 12f1de6e58c5c2176bb0267a5f49a244b6d66067
Author: Daniel Barlow <dan@telent.net>
Date: Sun, 3 Nov 2024 12:04:43 +0000
add glue to serve frontend js from backend
Diffstat:
3 files changed, 54 insertions(+), 8 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,7 @@
+default: frontend/frontend.js dist-newstyle/build/x86_64-linux/ghc-9.6.5/souplesse-0.1.0.0/x/souplesse/build/souplesse/souplesse
+
+dist-newstyle/build/x86_64-linux/ghc-9.6.5/souplesse-0.1.0.0/x/souplesse/build/souplesse/souplesse: app/*.hs lib/*.hs
+ cabal build
+
+frontend/frontend.js: frontend/src/Main.elm
+ elm make --output=$@ $<
diff --git a/app/Main.hs b/app/Main.hs
@@ -2,6 +2,7 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
+
module Main where
import Track(parseFile)
@@ -9,19 +10,56 @@ import Data.List as List
import Yesod.Core
-data HelloWorld = HelloWorld
+-- https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Static-file-subsite-Hello-World.md
+import Yesod.Static
+staticFiles "frontend" -- this param appears to be a pathname
+
+data Souplesse = Souplesse
+ { getStatic :: Static
+ }
+
+-- ref https://www.yesodweb.com/book/routing-and-handlers
+-- for adding params (start/end) to the timeline route
+mkYesod "Souplesse" [parseRoutes|
+/ CalendarR GET
+/timeline TimelineR GET
+/static StaticR Static getStatic
+|]
+
+instance Yesod Souplesse
-mkYesod "HelloWorld" [parseRoutes|
-/ HomeR GET
+getCalendarR :: Handler Html
+getCalendarR = defaultLayout [whamlet|
+<h1>Calendar</h1>
+
+<p>A calendar view goes here
+
+<a href="/timeline">timeline</a>
+© 2024 Daniel Barlow
|]
-instance Yesod HelloWorld
+getTimelineR :: Handler Html
+
+-- what's this about? <img src=@{StaticR image_png}/>|]
+getTimelineR = defaultLayout $ do
+ setTitle "timeline"
+ addScriptRemote "/static/frontend.js"
+ toWidgetBody [julius|
+window.addEventListener("load", function(){
+var app = Elm.Main.init({
+ node: document.getElementById("elm")
+});
+})
+|]
+ toWidget [hamlet|
+<pre id="elm">
+<p>Copyright © 2024 Daniel Barlow
+|]
-getHomeR :: Handler Html
-getHomeR = defaultLayout [whamlet|Hello World!|]
main :: IO ()
main = do
points <- Track.parseFile "track.gpx"
- putStrLn ("loaded " ++ (show (List.length points)) ++ " points from GPX")
- warp 3000 HelloWorld
+ static'@(Static settings) <- static "frontend"
+ putStrLn ("loaded " ++ show (List.length points) ++ " points from GPX")
+ warp 3000 $ Souplesse static'
diff --git a/souplesse.cabal b/souplesse.cabal
@@ -72,6 +72,7 @@ executable souplesse
base ^>=4.18.2.1
, souplesse-lib
, yesod-core == 1.6.25.1
+ , yesod-static
-- Directories containing source files.
hs-source-dirs: app