Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A tiny web dev DSL

branch: master
Octocat-spinner-32 src no dep on utf8-string April 14, 2014
Octocat-spinner-32 .gitignore update ignore December 18, 2012
Octocat-spinner-32 LICENSE init June 10, 2011
Octocat-spinner-32 Nemesis add desc for run task January 19, 2012
Octocat-spinner-32 Setup.lhs init June 10, 2011
Octocat-spinner-32 changelog.md init June 10, 2011
Octocat-spinner-32 known-issues.md init June 10, 2011
Octocat-spinner-32 miku.cabal no dep on utf8-string April 14, 2014
Octocat-spinner-32 readme.md remove outdated readme content May 22, 2013
readme.md

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/src/Test/Test.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 ask defined in ReaderT monad to get the Hack2.Environment, then use helper method defined in Hack2.Contrib.Request to query it.
  • Response is in StateT, html and text are 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.

Reference

Something went wrong with that request. Please try again.