hack: a sexy Haskell Webserver Interface

[ bsd3, deprecated, library, web ] [ Propose Tags ]
Deprecated in favor of hack2

a sexy Haskell Webserver Interface


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 2009.4.20, 2009.4.21, 2009.4.22, 2009.4.23, 2009.4.25, 2009.4.26, 2009.4.27, 2009.4.28, 2009.4.29, 2009.4.30, 2009.4.51, 2009.4.52, 2009.5.19, 2009.7.15, 2009.10.30, 2012.2.6
Change log changelog.md
Dependencies base, cgi, containers, data-default (>=0.2), directory, filepath, haskell98, kibro (>=0.4.3), mps (>=2009.4.21), network, old-locale, old-time [details]
License LicenseRef-GPL
Author Wang, Jinjing
Maintainer Wang, Jinjing <nfjinjing@gmail.com>
Category Web
Home page http://github.com/nfjinjing/hack/tree/master
Uploaded by JinjingWang at 2009-04-21T11:50:29Z
Distributions NixOS:2012.2.6
Reverse Dependencies 28 direct, 3 indirect [details]
Downloads 12032 total (49 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for hack-2009.4.22

[back to package description]

Hack: a sexy Haskell Webserver Interface

Hack is a brain-dead port of the brilliant Ruby Rack webserver interface.

What does a Hack app look like

module Main where

import Hack
import Hack.Handler.Kibro

hello :: Application
hello = \env -> return $ Response 
    { status  = 200
    , headers = [ ("Content-Type", "text/plain") ]
    , body    = "Hello World"
    }

main = run hello

1 minute tutorial

Install Hack

cabal install hack

Install Kibro (the only handler at the moment)

cabal install kibro

Install lighttpd 1.4.19 (used by kibro)

wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar zxfv lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.19
./configure --prefix=$HOME
make
make install

Create a new Kibro project

kibro new hello-world

Test if Kibro works

cd hello-world
kibro start

Create our hack app

put the following code in src/Main.hs

module Main where

import Hack
import Hack.Handler.Kibro

hello :: Application
hello = \env -> return $ Response 
    { status  = 200
    , headers = [ ("Content-Type", "text/plain") ]
    , body    = "Hello World"
    }

main = run hello

restart kibro

kibro restart

demo usage of middle-ware

module Main where

import Hack
import Hack.Utils
import Hack.SimpleRoute
import Hack.Handler.Kibro

import Data.Default
import MPS
import Prelude hiding ((.))

hello :: Application
hello = \env -> def {body = env.show} .return

app :: Application
app = route [("/hello", hello), ("", hello)] empty_app

main = run app

make a middle-ware

inside Hack.hs:

type MiddleWare = Application -> Application

since Haskell has curry, middle-ware api can be of type

Params -> Application -> Application

just pass an applied middle-ware into a chain.

finally the source code of SimpleRoute.hs:

{-# LANGUAGE NoMonomorphismRestriction#-}
{-# LANGUAGE QuasiQuotes #-}

module Hack.SimpleRoute where

import Hack
import Hack.Utils
import List (find)
import Prelude hiding ((.), (^), (>))
import MPS

type RoutePath = (String, Application)

route :: [RoutePath] -> Application -> Application
route h _ = \env ->
  let path = env.path_info
      script = env.script_name
      mod_env location = env 
        { script_name = script ++ location
        , path_info = path.drop (location.length)
        }
  in
  case h.find (fst > flip starts_with path) of
    Nothing -> not_found [$here|Not Found: #{path}|]
    Just (location, app) -> app (mod_env location)