PTI891: Deklarative Programmierung
→ Web-Applikation mit Happstack Framework
Lizenz:
BSD3
Author:
Happstack team, HAppS LLC
Verfechter:
Happstack team <[email protected]>
Home page:
http://happstack.com
Dokumentation:
http://www.happstack.com/c/view-page-slug/3/documentation/
Package & repositories
Hackage - Darcs
Hallo Welt!
module Main where
import
Happstack.Server
(
nullConf
,
simpleHTTP
,
toResponse
,
ok
)
main
::
IO
()
Hallo Welt!
Was ist Passiert?
Die Funktion Simple HTTP lauscht auf einen Request, ab den Moment, wo das
Programm gestartet wird.
Hallo Welt!
Was ist Passiert?
Die Funktion Simple HTTP lauscht auf einen Request, ab den Moment, wo das
programm gestartet wird.
simpleHTTP
:: (
ToMessage
a
) =>
Conf
->
ServerPartT IO
a
->
IO
()
Die Konfiguration setzt die Eigenschaften des Servers:
data
Conf
=
Conf
{
port
::
Int
,
validator
::
Maybe
(
Response
->
IO Response
)
,
logAccess
::
forall t
.
FormatTime
t
=>
Maybe
(
String
->
String
->
t
->
String
->
Int
->
Integer
->
String
->
String
->
IO
())
,
timeout
::
Int
Statische Routen I
module
Main
where
import
Control.Monad
import
Happstack.Server
(
nullConf
,
simpleHTTP
,
ok
,
dir
)
main
::
IO
()
main
=
simpleHTTP nullConf $ msum
[
ok
"Hello, World!"
,
ok
"Unreachable ServerPartT"
Statische Routen II
module
Main
where
import
Control.Monad
(
msum
)
import
Happstack.Server
(
nullConf
,
simpleHTTP
,
ok
,
dir
)
main
::
IO
()
main
=
simpleHTTP nullConf $ msum
[
dir
"hello"
$ ok
"Hello, World!"
,
dir
"goodbye"
$
dir
"world"
$ ok
"Goodbye, World!"
Statische Routen II
module
Main
where
import
Control.Monad
(
msum
)
import
Happstack.Server
(
nullConf
,
simpleHTTP
,
ok
,
dir
)
main
::
IO
()
main
=
simpleHTTP nullConf $ msum
[
dir
"hello"
$ ok
"Hello, World!"
,
dir
"goodbye"
$
dir
"world"
$ ok
"Goodbye, World!"
dirs
"hello/haskell"
$ ok
"Hello, Haskell!"
Statiche Routen III
module
Main
where
import
Control.Monad
(
msum
)
import
Happstack.Server
(
nullConf
,
simpleHTTP
,
ok
,
dir
,
path
)
main
::
IO
()
main
=
simpleHTTP nullConf $ msum
[
dir
"hello"
$ path $
\
s
->
ok $
"Hello, "
++ s
]
Request unterscheiden
module
Main
where
import
Control.Monad
(
msum
)
import
Happstack.Server
(
Method
(
GET
,
POST
),
dir
,
methodM
,
nullConf
,
ok
,
simpleHTTP
)
main
::
IO
()
main
=
simpleHTTP nullConf $ msum
[
do
methodM
GET
ok $ "You did a GET request.\n"
,
do
methodM
POST
ok $ "You did a POST request.\n"
,
dir "foo" $
do
methodM
GET
ok $ "You did a GET request on /foo\n"
]
HTML
<html><head>
<title>Hello, HSP!</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head> <body>
<h1>Hello HSP!</h1>
<p>We can insert Haskell expression such
as this: <% sum [1 .. (10 :: Int)] %></p> <p>We can use the ServerPartT monad too.
Your request method was: <% getMethod %></p> </body>
</html>
H.html $ do
H.head $ do
H.title "Hello Blaze HTML!"
H.meta ! A.httpEquiv "Content-Type" !
A.content "text/html;charset=utf-8" sequence_ headers
H.body $ do
H.h1 $ "Hello Blaze HTML!"
H.p $ "We can insert Haskell expression such as this: <% sum [1 .. (10 :: Int)] %>" H.p $ "Symbols like & or > will be escape!"
HTML
<html><head>
<title>Hello, HSP!</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head> <body>
<h1>Hello HSP!</h1>
<p>We can insert Haskell expression such
as this: <% sum [1 .. (10 :: Int)] %></p> <p>We can use the ServerPartT monad too.
Your request method was: <% getMethod %></p> </body>
</html>
H.html $ do
H.head $ do
H.title "Hello Blaze HTML!"
H.meta ! A.httpEquiv "Content-Type" !
A.content "text/html;charset=utf-8" sequence_ headers
H.body $ do
H.h1 $ "Hello Blaze HTML!"
H.p $ "We can insert Haskell expression such as this: <% sum [1 .. (10 :: Int)] %>" H.p $ "Symbols like & or > will be escape!"
HTML
<html><head>
<title>Hello, HSP!</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head> <body>
<h1>Hello HSP!</h1>
<p>We can insert Haskell expression such
as this: <% sum [1 .. (10 :: Int)] %></p> <p>We can use the ServerPartT monad too.
Your request method was: <% getMethod %></p> </body>
</html>
H.html $ do
H.head $ do
H.title "Hello Blaze HTML!"
H.meta ! A.httpEquiv "Content-Type" !
A.content "text/html;charset=utf-8" sequence_ headers
H.body $ do
H.h1 $ "Hello Blaze HTML!"
H.p $ "We can insert Haskell expression such as this: <% sum [1 .. (10 :: Int)] %>" H.p $ "Symbols like & or > will be escape!"
HSX/HSP
Durch das Vorkompiliren wandelt "trhsx" den
XML-Code in Haskell-Ausdrücke um:
aus:
foo
::
XMLGenT
(
ServerPartT IO
)
XML
foo
=
<span
class
=
"bar"
>foo</span>
wird:
foo
::
XMLGenT
(
ServerPartT IO
)
XML
foo
=
genElement
(
Nothing
,
"span"
) [
asAttr
(
"class"
:=
"bar"
) ] [
asChild
(
"foo"
)]
MVC
Model
Controller
MVC
Model
Controller
MVC – Controller
main
::
IO
()
main
=
simpleHTTP nullConf $ msum
[
-- Seiten-Navigation
dir
"helloworld"
helloWorld
,
dir
"mvc"
mvc
,
dir
"defaultlayout"
defaultLayout
,
dir
"login"
login
,
startPage
]
MVC – View
startPage :: ServerPart ResponsestartPage =
ok $ toResponse $
appTemplate "Deklarative Programmierung!"
[H.meta ! A.name "keywords" ! A.content "happstack, StartUp, html"]
(H.img !A.class_ "teaser" !A.src "/public/image/dual_neurons.png" !A.alt "teaser")
(H.div !A.class_ "content" $ "Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \
\Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla laoreet dolore magna aliquam erat volutpat."