init nix-features
This commit is contained in:
parent
d04ddd4c32
commit
b929e21f82
14 changed files with 1646 additions and 61 deletions
|
@ -1 +0,0 @@
|
|||
../backend/app/Main.hs
|
108
src/Main.hs
Normal file
108
src/Main.hs
Normal file
|
@ -0,0 +1,108 @@
|
|||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
module Main (main) where
|
||||
|
||||
import ElmskellTypes
|
||||
import Data.Text.Lazy (Text)
|
||||
import Network.Wai.Handler.Warp (Port)
|
||||
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
|
||||
import Text.Blaze ((!))
|
||||
import Text.Blaze.Html.Renderer.Text (renderHtml)
|
||||
|
||||
import Network.HTTP.Types
|
||||
import Network.Wai.Middleware.Gzip
|
||||
import Web.Scotty as S
|
||||
|
||||
import qualified Control.Exception as E
|
||||
import qualified Text.Blaze.Html5 as H
|
||||
import qualified Text.Blaze.Html5.Attributes as A
|
||||
|
||||
-- HTML
|
||||
index :: H.Html -> Text
|
||||
index compiledElmApp = renderHtml $ do
|
||||
H.docTypeHtml $ do
|
||||
H.head $ do
|
||||
H.title "TESTING Scotty+Elm"
|
||||
H.meta ! A.charset "utf-8"
|
||||
H.style "body{margin:0px;}"
|
||||
(H.body ! A.id "body") $ do
|
||||
embedJs compiledElmApp
|
||||
|
||||
-- CONF
|
||||
port :: Port
|
||||
port = 8080
|
||||
|
||||
adminContact :: String
|
||||
adminContact = "[Matrix] @mtgmonkey:calitabby.net"
|
||||
|
||||
compiledElmAppFile :: AssetPath
|
||||
compiledElmAppFile = "/js/main.js"
|
||||
|
||||
boilerplateJsFile :: AssetPath
|
||||
boilerplateJsFile = "/js/init.js"
|
||||
|
||||
assetsFolder :: FilePath
|
||||
assetsFolder = "/home/mtgmonkey/elmskell/assets"
|
||||
|
||||
-- MAIN
|
||||
main :: IO ()
|
||||
main = do
|
||||
|
||||
generateElmskellTypes
|
||||
|
||||
compiledElmAppOrExc <- E.try $ readFile $ assetsFolder ++ compiledElmAppFile :: IO (Either E.IOException String)
|
||||
let compiledElmApp = case compiledElmAppOrExc of
|
||||
Left e -> serverErrorReadFile e
|
||||
Right contents -> H.toHtml $ contents
|
||||
|
||||
boilerplateJsOrExc <- E.try $ readFile $ assetsFolder ++ boilerplateJsFile :: IO (Either E.IOException String)
|
||||
let boilerplateJs = case boilerplateJsOrExc of
|
||||
Left e -> serverErrorReadFile e
|
||||
Right contents -> H.toHtml $ contents
|
||||
|
||||
let anyRoute = regex "^.*$"
|
||||
scotty port $ do
|
||||
|
||||
middleware $ gzip $ def { gzipFiles = GzipCompress }
|
||||
middleware logStdoutDev
|
||||
|
||||
-- GET requests
|
||||
get "/" $ do
|
||||
shortCache
|
||||
status ok200
|
||||
S.html $ index $ do
|
||||
compiledElmApp
|
||||
boilerplateJs
|
||||
|
||||
get "/favicon.ico/" $ do
|
||||
shortCache
|
||||
status notFound404
|
||||
S.html $ "you want a favi-<i>what</i>now!?"
|
||||
|
||||
-- ERR
|
||||
notFound $ do
|
||||
noCache
|
||||
status methodNotAllowed405
|
||||
S.text "Verb disallowed; OR, route doesn't exist :("
|
||||
|
||||
|
||||
-- FUNC
|
||||
serverErrorReadFile :: E.IOException -> Js
|
||||
serverErrorReadFile e = H.toHtml $ "document.getElementById('body').innerHTML='Server-side error occurred: "
|
||||
++ (show e)
|
||||
++ "; report this to a site administrator: "
|
||||
++ adminContact
|
||||
++ "';"
|
||||
|
||||
shortCache :: ActionM ()
|
||||
shortCache = addHeader "Cache-Control" "max-age=21600"
|
||||
|
||||
noCache :: ActionM ()
|
||||
noCache = addHeader "Cache-Control" "no-cache"
|
||||
|
||||
embedJs :: Js -> H.Html
|
||||
embedJs js = H.script $ js
|
||||
|
||||
-- TYPES
|
||||
type AssetPath = FilePath
|
||||
type Js = H.Html
|
Loading…
Add table
Add a link
Reference in a new issue