Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe url #76

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions examples/mig-example-apps/HtmlTemplate/src/Api.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module Api (
Routes (..),
Urls (..),
GreetingRoute,
BlogPostRoute,
QuoteRoute,
WriteFormRoute,
WriteSubmitRoute,
ListPostsRoute,
urls,
server,
) where

import Mig.Html.IO
import Types

-- routes

type GreetingRoute = Get Html
type BlogPostRoute = Optional "id" BlogPostId -> Get Html
type QuoteRoute = Get Html
type WriteFormRoute = Get Html
type WriteSubmitRoute = Body FormUrlEncoded SubmitBlogPost -> Post Html
type ListPostsRoute = Get Html

data Routes = Routes
{ greeting :: GreetingRoute
, blogPost :: BlogPostRoute
, quote :: QuoteRoute
, listPosts :: ListPostsRoute
, writeForm :: WriteFormRoute
, writeSubmit :: WriteSubmitRoute
}

-- URLs

data Urls = Urls
{ greeting :: UrlOf GreetingRoute
, blogPost :: UrlOf BlogPostRoute
, quote :: UrlOf QuoteRoute
, listPosts :: UrlOf ListPostsRoute
, writeForm :: UrlOf WriteFormRoute
, writeSubmit :: UrlOf WriteSubmitRoute
}

{-| Site URL's
URL's should be listed in the same order as they appear in the server
-}
urls :: Urls
urls = Urls{..}
where
greeting
:| blogPost
:| quote
:| listPosts
:| writeForm
:| writeSubmit =
toUrl (server undefined)

-- server definition

-- | Server definition. Note how we assemble it from parts with monoid method mconcat.
server :: Routes -> Server IO
server routes =
addIndex $
mconcat
[ defaultPage
, "blog"
/. [ readServer
, writeServer
]
]
where
addIndex = addPathLink "index.html" "/"

-- default main page
defaultPage =
"/" /. routes.greeting

-- server to read info.
-- We can read blog posts and quotes.
readServer =
toServer
[ "read"
/. mconcat
[ "post" /. routes.blogPost
, "quote" /. routes.quote
]
, "list" /. routes.listPosts
]

-- server to write new blog posts
writeServer =
"write"
/. [ toServer routes.writeForm
, toServer routes.writeSubmit
]
Loading