@hackage miku2016.3.16
A minimum web dev DSL in Haskell
Categories
License
BSD-3-Clause
Maintainer
Jinjing Wang <nfjinjing@gmail.com>
Links
- Homepage
- Documentation
- No source repository
- Changelog
- Security
Versions
- 2016.3.17 Wed, 16 Mar 2016
- 2016.3.16.1 Wed, 16 Mar 2016
- 2016.3.16 Wed, 16 Mar 2016
- 2014.11.17 Mon, 17 Nov 2014
- 2014.5.19 Mon, 19 May 2014
- 2014.4.14 Mon, 14 Apr 2014 Show all…
Installation
Dependencies (9)
- base >4 && <=6
- bytestring
- containers
- data-default
- filepath >=1.4.0.0
- hack2 >=2012.1.19 Show all…
Dependents (2)
@hackage/acme-everything, @hackage/geek
miku
A tiny web dev DSL
Example
{-# LANGUAGE OverloadedStrings #-}
import Network.Miku
import Hack2.Handler.SnapServer
main = run . miku $ get "/" (text "miku power")
Installation
cabal update
cabal install miku
cabal install hack2-handler-snap-server
-- copy and paste the above example to myapp.hs
runghc myapp.hs
check: http://localhost:3000
Quick reference
https://github.com/nfjinjing/miku/blob/master/test/route_example.hs
Routes
Verbs
{-# LANGUAGE OverloadedStrings #-}
-- use - instead of $ for clarity
import Air.Light ((-))
import Prelude hiding ((-))
import Network.Miku
import Hack2.Handler.SnapServer
main = run . miku - do
get "/" - do
-- something for a get request
post "/" - do
-- for a post request
put "/" - do
-- put ..
delete "/" - do
-- ..
Captures
get "/say/:user/:message" - do
text . show =<< captures
-- /say/miku/hello will output
-- [("user","miku"),("message","hello")]
Static
-- public serve, only allows `./src`
public (Just ".") ["/src"]
Mime types
-- treat .hs extension as text/plain
mime "hs" "text/plain"
Filters
-- before takes a function of type (Env -> IO Env)
before - \e -> do
putStrLn "before called"
return e
-- after takes that of type (Response -> IO Response)
after return
Hack2 integration
Use hack2 middleware
import Hack2.Contrib.Middleware.SimpleAccessLogger
middleware - simple_access_logger Nothing
Convert miku into a hack2 application
-- in Network.Miku.Engine
miku :: MikuMonad -> Application
Hints
- It's recommended to use your own html combinator / template engine. Try DIY with, e.g. moe.
- Example view using custom html combinator (moe in this case)
- When inspecting the request, use
askdefined inReaderTmonad to get theHack2.Environment, then use helper method defined inHack2.Contrib.Requestto query it. Responseis inStateT,htmlandtextare simply helper methods that update the state, i.e. setting the response body, content-type, etc.- You do need to understand monad transformers to reach the full power of
miku.