
Low-level web programming in Racket and a wiki in 500 lines - ihodes
http://matt.might.net/articles/low-level-web-in-racket/
======
mkozlows
I'm glad to see someone talking about using the lower-level web libraries in
Racket. The high-level continuation-based ones are ridiculously unsuitable for
actual web programming (they store full state on the server at all times, and
the URL gives you full access to the session, among other issues), but the
lower-level ones actually look pretty clean and modern. And yet all the
documentation keeps talking about the continuation stuff, bizarrely.

~~~
shriramkmurthi
The continuation-based libraries are the simplest, easiest, quickest way to
get a program off the ground. They make interactive Web programming just as
easy as writing “scanf” or its equivalent, _and_ are safe in the face of
various browser interactions. Therefore, they're a good default for
prototyping. However, I agree that they also have issues, and the
documentation should not over-emphasize them, but rather indicate what they
are suitable for and what not and make clearer to readers a transition path
out of there.

~~~
mkozlows
I think that depends on where you're coming from. If you're a longtime web
developer, stateless request/response are in your blood, and stateful,
persistent programming is deeply weird.

------
nosefrog
This is very useful. I'm in the middle of a Racket web project right now and I
got hung up on parsing/processing form data. The Racket docs are very
thorough, but they can be confusing if you don't know what you're looking for.

If anyone here has experience with web stuff in Racket, I've set up a small
skeleton for Racket web projects: [https://github.com/samertm/web-project-
bootstrap.rkt](https://github.com/samertm/web-project-bootstrap.rkt)

Right now it contains skeletons for talking to sqlite, setting up a router and
handlers, and manipulating the web server from the command line. I need to add
an ORM (I'm not sure if Racket has any up-to-date ORMs?) and static file
handlers. I'd love help or feedback!

~~~
truncate
How exactly are you hung up with form data? Its low level, so you certainly
can't get validation out of box, but otherwise in my experience getting form
data out is fairly straight forward. I recently wrote my first Racket web app.
Its not pretty or following best practices but you can take a look.

[https://github.com/vishesh/whalebin](https://github.com/vishesh/whalebin)

~~~
nosefrog
Thank you! Your project was helpful to skim through. Do you have any additions
to the web-project-bootstrap.rkt project?

BTW, this is what I mean by form bindings being confusing: according to the
docs, request-bindings is dangerous and shouldn't be used [http://docs.racket-
lang.org/web-server/http.html#%28mod-path...](http://docs.racket-lang.org/web-
server/http.html#%28mod-path._web-server%2Fhttp%2Fbindings%29) . That's kind
of confusing, because request-bindings is used in the tutorial docs:
[http://docs.racket-
lang.org/continue/#%28def._%28%28lib._web...](http://docs.racket-
lang.org/continue/#%28def._%28%28lib._web-server%2Fservlet..rkt%29._extract-
binding%2Fsingle%29%29)

And the function that Matt Might uses, request-post-data/raw, isn't even in
the docs! Haha, so handling forms has been a bit of a pain for me :)

~~~
truncate
Good to know! I followed the tutorial and they were using it there, and didn't
see the docs after that. I see that the Matt's blog handles the problem with
request-binding himself, by doing ensuring encoding conversions, and gets over
the file uploading problem. So I would replace my code with the way he does.

For your boilerplate, you may want to add bunch of options to serve/servlet
such static files path, stateless? (to not use continuations) etc...

------
baldfat
Just being exposed to Racket last month I was extremely surprised at how well
it clicked with me as opposed to Haskel which I tried to learn for a few
months on my own and just tried out SML and it moved to Racket. Can't put a
finger on it but Racket just makes sense to me.

~~~
snissn
Have you tried python..? You might like it

~~~
shriramkmurthi
Racket and Python aren't even remotely comparable as languages. Python is a
far less sophisticated language with far richer libraries, and Racket is vice
versa.

------
networked
How fast is web-server/servlet? Is it fit for production use?

Last time I checked (which was, admittedly, a long time ago) it served
noticeably fewer "hello, template world" requests per second than Flask
running on Python 2.7 while using more RAM. This was a pity to me because I
thought Racket could otherwise be a very interesting language for web
development.

------
59nadir
Isn't your `any?` function actually `ormap`[0]?

You should be able to do the same thing with:

    
    
        (ormap password-matches? line)
    

0 - [http://docs.racket-
lang.org/reference/pairs.html?q=ormap#%28...](http://docs.racket-
lang.org/reference/pairs.html?q=ormap#%28def._%28%28lib._racket%2Fprivate%2Fmap..rkt%29._ormap%29%29)

~~~
mattmight
Yep! Jay McCarthy pointed this out, and I changed it. :)

------
cneu
Very nice proof of concept. Just a word of warning: In practice you want to
sanitize your input. The code in "Serving static files" allows to break out of
document-root with "..".

~~~
brlewis
Have you tried to actually exploit that? I don't currently use Racket myself,
but I would expect request-uri to prevent ".." and if it didn't surely url-
path would.

    
    
      ; extract the URI from the request:
      (define uri (request-uri req))
      
      ; extract the resource from the URI:
      (define resource 
        (map path/param-path (url-path uri)))
      
      ; find the file location:
      (define file (string-append
                    document-root
                    "/" 
                    (string-join resource "/")))

------
octatoan
Hijacking: Can anyone tell me how to make a Scribble website have CSS like in
HTDP?

~~~
jeapostrophe
Extra CSS can be added when running Scribble:

[http://docs.racket-lang.org/scribble/config-
style.html?q=css](http://docs.racket-lang.org/scribble/config-
style.html?q=css)

