X-Git-Url: https://git.lukelau.me/?p=blog.git;a=blobdiff_plain;f=Main.hs;h=b7a6847e97996eccefc7d5964cc3c46d4e4b9e36;hp=57edc52839e94f230b35cf47b610b99c08e0f7fc;hb=c8e8274981a5050ff1247f2a8244d394ba92ecc2;hpb=c353ebcf5759dc6d64973b85afbf7a3d794ed25f diff --git a/Main.hs b/Main.hs index 57edc52..b7a6847 100644 --- a/Main.hs +++ b/Main.hs @@ -29,6 +29,7 @@ import Network.HTTP.Types type Sessions = Map.Map T.Text T.Text type ActionM = ActionT T.Text (S.StateT Sessions IO) +-- | Checks if the user is logged in loggedIn :: ActionM Bool loggedIn = do mCookie <- getCookie "session" @@ -36,6 +37,7 @@ loggedIn = do Nothing -> return False Just cookie -> Map.member (T.fromStrict cookie) <$> lift S.get +-- | Returns the current username currentUser :: ActionM (Maybe T.Text) currentUser = do mCookie <- getCookie "session" @@ -72,31 +74,31 @@ main = do get "/create" $ do authed <- loggedIn if authed - then html $ renderText $ html_ $ do - head_ $ do - title_ "Create" - style - body_ $ do + then template "Create" $ do h1_ "Create a new post" - with form_ [action_ "create", method_ "post"] $ do + form_ [action_ "create", method_ "post"] $ do input_ [name_ "title", type_ "text", placeholder_ "Title"] br_ [] - with textarea_ [name_ "body", placeholder_ "# Your markdown here"] "" + textarea_ [name_ "body", placeholder_ "# Your markdown here"] "" br_ [] - input_ [type_ "submit"] - else status unauthorized401 + input_ [type_ "submit", value_ "Post"] + else do + text "You're not logged in" + status unauthorized401 post "/create" $ do t <- param "title" b <- param "body" - p <- createPost b t + p <- createPost t b redirect (linkToPost p) post "/login" $ do username <- param "username" password <- param "password" if Map.lookup username logins /= Just password - then status unauthorized401 + then do + text "Invalid username and/or password" + status unauthorized401 else do session <- T.pack . show <$> liftIO (randomIO :: IO Int) lift $ S.modify (Map.insert session username) @@ -106,26 +108,11 @@ main = do post "/logout" $ deleteCookie "session" >> redirect "/" get "/" $ do - authed <- loggedIn posts <- liftIO $ do files <- listDirectory "posts" posts <- catMaybes <$> mapM load files return $ take 5 $ sortOn (Down . date) posts - html $ renderText $ html_ $ do - head_ $ style >> title_ "Cool Blog" - body_ $ do - p_ $ a_ [href_ "/"] "Cool Blog" - if authed - then do - a_ [href_ "/create"] "Create a Post" - br_ [] - with form_ [action_ "/logout", method_ "post"] $ - input_ [type_ "submit", value_ "Log Out"] - else with form_ [action_ "/login", method_ "post"] $ do - input_ [name_ "username", placeholder_ "Username", type_ "text"] - input_ [name_ "password", placeholder_ "Password", type_ "password"] - input_ [type_ "submit"] - sequence (mapMaybe render posts) + template "Cool blog" $ sequence (mapMaybe render posts) get "/posts/:post" $ do name <- param "post" @@ -135,14 +122,27 @@ main = do Just post -> case render post of Nothing -> status internalServerError500 - Just content -> + Just content -> template (title post) content + +template :: T.Text -> Html a -> ActionM () +template title content = do + authed <- loggedIn html $ renderText $ html_ $ do - head_ $ style >> title_ "Post" + head_ $ style >> title_ (toHtml title) body_ $ do - p_ $ a_ [href_ "/"] "Home" + term "nav" $ do + a_ [href_ "/"] "🏠 Cool Blog" + if authed + then do + " " >> a_ [href_ "/create"] "Create a Post" + form_ [action_ "/logout", method_ "post", class_ "login-form"] $ + input_ [type_ "submit", value_ "Log Out"] + else form_ [action_ "/login", method_ "post", class_ "login-form"] $ do + input_ [name_ "username", placeholder_ "Username", type_ "text"] + input_ [name_ "password", placeholder_ "Password", type_ "password"] + input_ [type_ "submit", value_ "Log In"] content - -style = link_ [Attribute "rel" "stylesheet", Attribute "href" "/style.css"] + where style = link_ [Attribute "rel" "stylesheet", Attribute "href" "/style.css"] data Post = Post { markdown :: T.Text @@ -162,11 +162,12 @@ render p@(Post markdown title date author) = case M.parse (pathToPost p) (T.toStrict markdown) of Left e -> Nothing Right doc -> Just $ - with div_ [id_ (T.toStrict title)] $ do - with a_ [href_ (T.toStrict $ linkToPost p)] $ + article_ [id_ (T.toStrict title)] $ do + header_ $ do + a_ [href_ (T.toStrict $ linkToPost p)] $ h1_ $ toHtml title - i_ $ toHtml $ "By " <> author <> ", " <> dateStr - br_ [] + address_ $ "By " >> toHtml author + time_ [datetime_ (T.toStrict $ T.pack (show date))] $ toHtml dateStr M.render (M.useExtensions extensions doc) where extensions = [M.ghcSyntaxHighlighter, M.skylighting, M.footnotes] dateStr = T.pack $ formatTime defaultTimeLocale "%a %e %B %Y" date @@ -177,8 +178,14 @@ linkToPost p = T.pack $ "/posts" kebab (T.unpack (title p)) pathToPost :: Post -> FilePath pathToPost p = "posts" kebab (T.unpack (title p)) -createPost :: T.Text -> T.Text -> ActionM Post -createPost md title = do +-- | Creates and saves a new post +createPost :: T.Text + -- ^ The title of the post + -> T.Text + -- ^ The markdown body + -> ActionM Post + -- ^ The freshly created post +createPost title md = do author <- do ma <- currentUser case ma of