| 2 | 2 |
|
| 3 | 3 |
module Main where
|
| 4 | 4 |
|
| 5 | |
import qualified Control.Exception as Exn
|
| 6 | |
import Control.Monad ((>=>))
|
| 5 |
import qualified Data.ByteString as BS
|
| 7 | 6 |
import qualified Data.Text as TS
|
| 8 | 7 |
import qualified Data.Text.Encoding as EncodingS
|
| 9 | 8 |
import qualified Data.Text.Lazy as T
|
| 10 | |
import qualified Data.Text.Lazy.Encoding as Encoding
|
| 11 | 9 |
import qualified Network.HTTP.Types as HTTP
|
| 12 | 10 |
import qualified Network.Wai as W
|
| 13 | |
import qualified Network.Wai.Handler.Warp as W
|
| 14 | 11 |
import System.FilePath ((</>))
|
| 15 | |
import qualified System.Environment as Env
|
| 16 | |
import qualified Text.Read as Read
|
| 17 | 12 |
|
| 18 | 13 |
import qualified Inf.DB.Monad as DB
|
| 19 | 14 |
import qualified Inf.DB as DB
|
| 20 | 15 |
import qualified Inf.Feed as Feed
|
| 21 | 16 |
import qualified Inf.Templates as Template
|
| 22 | 17 |
import Inf.Types
|
| 18 |
import qualified Inf.Web as Web
|
| 23 | 19 |
|
| 24 | 20 |
main :: IO ()
|
| 25 | |
main = do
|
| 26 | |
portVar <- Env.lookupEnv "port"
|
| 27 | |
let port = case portVar of
|
| 28 | |
Just x
|
| 29 | |
| Just p <- Read.readMaybe x -> p
|
| 30 | |
_ -> 8080
|
| 31 | |
conn <- DB.open "test.db"
|
| 32 | |
W.run port $ \ r k -> do
|
| 33 | |
resp <- route conn r
|
| 34 | |
`Exn.catch` (Template.dbException >=> raise500)
|
| 35 | |
`Exn.catch` (Template.someException >=> raise500)
|
| 36 | |
k resp
|
| 21 |
main = Web.run route
|
| 37 | 22 |
|
| 38 | |
page :: T.Text -> T.Text -> IO W.Response
|
| 39 | |
page title stuff = do
|
| 40 | |
pg <- Template.main title stuff
|
| 41 | |
let headers = [(HTTP.hContentType, "text/html")]
|
| 42 | |
pure (W.responseLBS HTTP.status200 [] (Encoding.encodeUtf8 pg))
|
| 23 |
getUser :: DB.Connection -> Maybe BS.ByteString -> IO (Maybe TS.Text)
|
| 24 |
getUser _ Nothing = pure Nothing
|
| 25 |
getUser _ (Just c) =
|
| 26 |
let _cookies = TS.splitOn "; " (EncodingS.decodeUtf8 c)
|
| 27 |
in pure Nothing
|
| 43 | 28 |
|
| 44 | |
raise404 :: [TS.Text] -> IO W.Response
|
| 45 | |
raise404 msgs = do
|
| 46 | |
contents <- Template.page $ TS.unwords
|
| 47 | |
[ "No handler found for page "
|
| 48 | |
, "`/" <> TS.intercalate "/" msgs <> "`"
|
| 49 | |
]
|
| 50 | |
pg <- Template.main "404" contents
|
| 51 | |
pure (W.responseLBS HTTP.status404 [] (Encoding.encodeUtf8 pg))
|
| 29 |
route :: Web.Routes
|
| 30 |
route c req = do
|
| 31 |
_user <- getUser c (lookup HTTP.hCookie (W.requestHeaders req))
|
| 32 |
case (W.requestMethod req, W.pathInfo req) of
|
| 33 |
("GET", []) -> do
|
| 34 |
post <- DB.runDB DB.newestPostRef c
|
| 35 |
Web.redirect (postRefURL post)
|
| 52 | 36 |
|
| 53 | |
raise500 :: T.Text -> IO W.Response
|
| 54 | |
raise500 msgs =
|
| 55 | |
let body = Encoding.encodeUtf8 msgs
|
| 56 | |
in pure (W.responseLBS HTTP.status500 [] body)
|
| 37 |
("POST", []) -> undefined
|
| 57 | 38 |
|
| 58 | |
route :: DB.Connection -> W.Request -> IO W.Response
|
| 59 | |
route c req = case (W.requestMethod req, W.pathInfo req) of
|
| 60 | |
("GET", []) -> do
|
| 61 | |
post <- DB.runDB DB.newestPostRef c
|
| 62 | |
let headers = [(HTTP.hLocation, EncodingS.encodeUtf8 (postRefURL post))]
|
| 63 | |
pure (W.responseLBS HTTP.status307 headers "redirecting...")
|
| 39 |
("GET", ["auth"]) -> undefined
|
| 64 | 40 |
|
| 65 | |
("POST", []) -> undefined
|
| 41 |
("GET", ["archive"]) -> do
|
| 42 |
posts <- DB.runDB DB.allPostRefs c
|
| 43 |
contents <- Template.list posts
|
| 44 |
Web.ok "Past Entries" contents
|
| 66 | 45 |
|
| 67 | |
("GET", ["auth"]) -> undefined
|
| 46 |
("GET", ["create"]) -> undefined
|
| 68 | 47 |
|
| 69 | |
("GET", ["archive"]) -> do
|
| 70 | |
posts <- DB.runDB DB.allPostRefs c
|
| 71 | |
contents <- Template.list posts
|
| 72 | |
page "Past Entries" contents
|
| 48 |
("GET", [y, m, s]) -> do
|
| 49 |
post <- DB.runDB (DB.postByDateAndSlug y m s) c
|
| 50 |
contents <- Template.page (postContents post)
|
| 51 |
Web.ok (T.fromStrict (postTitle post)) contents
|
| 73 | 52 |
|
| 74 | |
("GET", ["create"]) -> undefined
|
| 53 |
("POST", [_y, _m, _s]) -> undefined
|
| 75 | 54 |
|
| 76 | |
("GET", [y, m, s]) -> do
|
| 77 | |
post <- DB.runDB (DB.postByDateAndSlug y m s) c
|
| 78 | |
contents <- Template.page (postContents post)
|
| 79 | |
page (T.fromStrict (postTitle post)) contents
|
| 55 |
("GET", [_y, _m, _s, "edit"]) -> undefined
|
| 80 | 56 |
|
| 81 | |
("POST", [y, m, s]) -> undefined
|
| 57 |
("GET", [_y, _m, _s, "delete"]) -> undefined
|
| 82 | 58 |
|
| 83 | |
("GET", [y, m, s, "edit"]) -> undefined
|
| 59 |
("GET", ["login"]) -> undefined
|
| 84 | 60 |
|
| 85 | |
("GET", [y, m, s, "delete"]) -> undefined
|
| 61 |
("GET", ["logout"]) -> undefined
|
| 86 | 62 |
|
| 87 | |
("GET", ["login"]) -> undefined
|
| 63 |
("GET", ["newest"]) -> do
|
| 64 |
post <- DB.runDB DB.newestPostRef c
|
| 65 |
Web.redirect (postRefURL post)
|
| 88 | 66 |
|
| 89 | |
("GET", ["logout"]) -> undefined
|
| 67 |
("GET", ["oldest"]) -> do
|
| 68 |
post <- DB.runDB DB.oldestPostRef c
|
| 69 |
Web.redirect (postRefURL post)
|
| 90 | 70 |
|
| 91 | |
("GET", ["newest"]) -> do
|
| 92 | |
post <- DB.runDB DB.newestPostRef c
|
| 93 | |
let headers = [(HTTP.hLocation, EncodingS.encodeUtf8 (postRefURL post))]
|
| 94 | |
pure (W.responseLBS HTTP.status307 headers "redirecting...")
|
| 71 |
("GET", ["static", fp]) ->
|
| 72 |
let path = "static" </> TS.unpack fp
|
| 73 |
in pure (W.responseFile HTTP.status200 [] path Nothing)
|
| 95 | 74 |
|
| 96 | |
("GET", ["oldest"]) -> do
|
| 97 | |
post <- DB.runDB DB.oldestPostRef c
|
| 98 | |
let headers = [(HTTP.hLocation, EncodingS.encodeUtf8 (postRefURL post))]
|
| 99 | |
pure (W.responseLBS HTTP.status307 headers "redirecting...")
|
| 75 |
("GET", ["rss"]) -> do
|
| 76 |
posts <- DB.runDB DB.allPosts c
|
| 77 |
feed <- Feed.renderFeed posts
|
| 78 |
Web.atomFeed feed
|
| 100 | 79 |
|
| 101 | |
("GET", ["static", fp]) ->
|
| 102 | |
let path = "static" </> TS.unpack fp
|
| 103 | |
in pure (W.responseFile HTTP.status200 [] path Nothing)
|
| 80 |
("GET", [pagename]) -> do
|
| 81 |
pg <- DB.runDB (DB.staticPage pagename) c
|
| 82 |
contents <- Template.page (pageText pg)
|
| 83 |
Web.ok (T.fromStrict pagename) contents
|
| 104 | 84 |
|
| 105 | |
("GET", ["rss"]) -> do
|
| 106 | |
posts <- DB.runDB DB.allPosts c
|
| 107 | |
feed <- Feed.renderFeed posts
|
| 108 | |
let headers =
|
| 109 | |
[(HTTP.hContentType, "application/atom+xml")]
|
| 110 | |
pure (W.responseLBS HTTP.status200 headers feed)
|
| 85 |
("GET", [_page, "edit"]) -> undefined
|
| 111 | 86 |
|
| 112 | |
("GET", [pagename]) -> do
|
| 113 | |
pg <- DB.runDB (DB.staticPage pagename) c
|
| 114 | |
contents <- Template.page (pageText pg)
|
| 115 | |
page (T.fromStrict pagename) contents
|
| 116 | |
|
| 117 | |
("GET", [page, "edit"]) -> undefined
|
| 118 | |
|
| 119 | |
(_, path) -> raise404 path
|
| 87 |
(_, path) -> Web.raise404 path
|