• No results found

PTI891: Deklarative Programmierung

N/A
N/A
Protected

Academic year: 2021

Share "PTI891: Deklarative Programmierung"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

Hallo Welt!

module Main where

import

Happstack.Server

(

nullConf

,

simpleHTTP

,

toResponse

,

ok

)

main

::

IO

()

(3)

Hallo Welt!

Was ist Passiert?

Die Funktion Simple HTTP lauscht auf einen Request, ab den Moment, wo das

Programm gestartet wird.

(4)

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

(5)

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"

(6)

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!"

(7)

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!"

(8)

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

]

(9)

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"

]

(10)

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!"

(11)

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!"

(12)

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!"

(13)

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"

)]

(14)

MVC

Model

Controller

(15)

MVC

Model

Controller

(16)

MVC – Controller

main

::

IO

()

main

=

simpleHTTP nullConf $ msum

[

-- Seiten-Navigation

dir

"helloworld"

helloWorld

,

dir

"mvc"

mvc

,

dir

"defaultlayout"

defaultLayout

,

dir

"login"

login

,

startPage

]

(17)

MVC – View

startPage :: ServerPart Response

startPage =

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."

(18)

Template (default Layout)

appTemplate

::

String

-> [

H.Html

] ->

H.Html

->

H.Html

appTemplate title headers body

=

H.html $

do

H.head $

do

H.title

(

H.toHtml title

)

H.meta

!

A.httpEquiv

"Content-Type"

!

A.content

"text/html;charset=utf-8"

sequence_ headers

H.body $

do

(19)

Quellen

http://happstack.com/clck/view-page-slug/3/doc

umentation

http://www.haskell.org/haskellwiki/Web/Framew

orks

http://jaspervdj.be/blaze/docs/index.html

http://www.haskell.org/pipermail/haskell-cafe/2

010-April/076856.html

http://google.de

...

References

Related documents

Outcome measures included an accurate diagnosis of COPD; documentation of smoking history and smoking cessation; appropriate COPD treatment including inhaler technique and

As extraction from natural sources and total synthesis have proven infeasible to meet the market demands, the compound is currently produced by two different approaches:

Potentially inappropriate prescribing (PIP) in long-term care (LTC) patients: validation of the 2014 STOPP-START and 2012 Beers criteria in a LTC population—a protocol for

By retaining only strictly positive values of DIR (informative rules), the user discards all the rules whose deviation from indepedence is bad (rules between negatively

Similarly, crop and management diversity in space at the landscape scale (i.e. different crops in different fields) can limit weed population spread by reducing suitable

The perceived needs for treatment of normal occlusion to mild maxillary protrusion did not depend on the level of professional expertise; however, when the cast of

Blacks are known to have larger tooth crown diameters than Whites (e.g., Richardson and Malhotra 1975; Macko et al. 1979), and Woods’ study reported that these differences extended

Meta-analysis of two studies indicated that 6 –9 months post-treatment, the temporomandibular joints of treated patients pre- sented shrunken anterior joint space (MD = 0.7 mm),