Features Download
From: S. Alexander Jacobson <alex <at> alexjacobson.com>
Subject: Re: Where to start about writing web/HTTP apps ?
Newsgroups: gmane.comp.lang.haskell.cafe
Date: Monday 12th September 2005 19:54:23 UTC (over 12 years ago)
I will be launching (beta) a new commercial chat site written using 
Haskell and AJAX in the next month.  The server is written on top of 
the HAppS library I made available at http://HappS.org.

My general pattern for writing an application in this framework is 
write the server based on a wire-level spec where the server takes in 
XML/x-www-form-encoded/multipart-form-data and returns XML.  Client 
side XSLT stylesheets then handle conversion of the XML to HTML.  Here 
are the details if you are interested:

1. Write a web service spec as a reference for both client and server 
development.  I use an informal language for this that relies on a 
mix of the reader understanding HTTP semantics and RelaxNG XML specs.

   POST /login ? {username {text},password{text},url{URL},captcha(text}}
   200 session {attribute userId{text},attribute href {rel-URL}}

   Some text here describing other details that don't fit into the
   syntactic model described above.  Server is assumed to handle any of
   application/x-www-form-urlencoded, multipart/form-data, or
   application/xml.  Client is assumed to receive application/xml.
   Browser is assumed to handle XSL stylesheet PIs.  (All conversion
   from XML to HTML is handled client side or on proxy using XSLT

2. Define Server State and state model

   data State = State {users::UserData}
   ...definition of user data...

   startState = State mzero
   addStateUser state user = ....

3. Define serialization for server state

   instance Show State where ...
   instance Read State where ---

4. Define exposed business logic

   addUser appCtx regInfo = if regGood then actionOk newState []
                            else qOk badRegInfo

5. define required wire formats for types

   --convert internal data type to XML
   instance ToElement NewUserInfo where
      toElement n = ...

   --convert posted data to internal data type
   instance FromMessage RegInfo where
      fromMessage msg = ...

6. define how busines logic maps to URLs

   a POST _ ["u"] = doXML addUser
   a GET "myapp.com" ("s":path) = fileServe mimeTypes "static" path

   myApp appCtx = let ?style=XSL "/s/style.xsl" in simpleHTTP a appCtx

7. define the app to execute

   run  path host = do
                    app <- startApp $ simpleConfig path startState confro
                    serve host app
                    return ()

   main = run "appdir" "localhost:80"

8. put client side stuff (including style.xsl) in the "static" directory

9. install searchpath (see http://searchpath.org)

10 run it!

   $ searchPath ghc -o MyApp --internet http://searchpath.org/default.map
   $ MyApp

HAppS and the chat applications are still works in progress, but I 
have this running successfully internally already!

S. Alexander Jacobson tel:917-770-6565 http://alexjacobson.com

On Mon, 12 Sep 2005, Sam Mason wrote:

> gary ng wrote:
>> I want to write apps for WEB and have briefly read
>> WASH.
> I'm thinking of doing the same.  The approach taken by WASH seems very
> cool and I'd love to use it.  I've only started looking at it, but
> WASH seems to require that JavaScript be enabled on the user's web
> browser, even for the simplest forms.  e.g. from the authors web page:
>  http://nakalele.informatik.uni-freiburg.de/cgi/WASH/Counter.cgi
> Also, the error checking doesn't seem to work as I'd expect:  if
> you submit a page without a required field it will complain the
> first time, then make something up the second time around -- even
> if you haven't entered anything in the field.
>  Sam
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
CD: 2ms