on c136ea7 help sign in
71 lines 2.1 kB history raw
1{-# LANGUAGE QuasiQuotes, TemplateHaskell #-}
3import Control.Monad (msum)
4import Happstack.Server
5import Language.Elm
6import Language.Elm.Quasi
8elmRuntime="elm-runtime.js"
9elmRTPath='/':elmRuntime
11-- elmResponse is a "nice to have" helper function for compiling
12-- Elm code when using Elm with Happstack. At some point this might
13-- be moved to an elm-happstack package.
14elmResponse :: ElmSource a
15 => String -- ^ Page title
16 -> a -- ^ elm source
17 -> Response
18elmResponse title = toResponse . toHtml elmRTPath title
20-- embedding variables (in this case URLs)
21rootHandler :: ServerPart Response
22rootHandler = ok $ elmResponse "Welcome!" $ elmIndex
23 where
24 mouse = "/mouse" -- all three of these variables are used in elm_source/index.elm
25 clock = "/clock"
26 shapes = "/shapes"
27 elmIndex = $(elmFile "elm_source/index.elm")
29-- loading elm source from file
31mouseHandler :: ServerPart Response
32mouseHandler = ok $ elmResponse "Mouse position demo"
33 $(elmFile "elm_source/mouse.elm")
35clockHandler :: ServerPart Response
36clockHandler = ok $ elmResponse "A clock" $(elmFile "elm_source/clock.elm")
38-- embedding elm code inside Haskell using the QuasiQuoter:
39shapesPage = [elm|
40square = rect 200 200 (150,150)
41circle = oval 140 140 (150,150)
42pentagon = ngon 5 60 (150,150)
44main = collage 300 300
45 [ outlined black square
46 , filled green pentagon
47 , customOutline [8,4] blue circle
48 ]
49|]
51shapesHandler :: ServerPart Response
52shapesHandler = ok $ elmResponse "Simple shapes" $ shapesPage
54-- routing
55elmExample :: String -> ServerPart Response
56elmExample elmLoc = do
57 msum [ nullDir >> rootHandler
58 , dir elmRuntime $ nullDir >>
59 serveFile (guessContentTypeM mimeTypes) elmLoc
60 , dir "mouse" $ nullDir >>
61 mouseHandler
62 , dir "clock" $ nullDir >>
63 clockHandler
64 , dir "shapes" $ nullDir >>
65 shapesHandler
66 ]
68main :: IO ()
69main = do
70 elmLoc <- Language.Elm.runtimeLocation
71 simpleHTTP nullConf {port = 3000} $ elmExample elmLoc