Posts tagged web

Minimal Racket web app

:: racket, web

This is the simplest module that responds to HTTP requests.

#lang racket/base

(require
 (only-in web-server/http
          response/full)
 (only-in web-server/http/request-structs
          make-header)
 (only-in web-server/servlet-env
          serve/servlet))

;; hello: request? -> response?
(define (hello req)
  ;; struct response/full
  (response/full
   200                          ;; code
   (string->bytes/utf-8 "OK")   ;; message
   (current-seconds)            ;; timestamp in s
   #f                           ;; mime or #f
   (list (make-header           ;; list of headers
          (string->bytes/utf-8 "Server")
          (string->bytes/utf-8 "Racket")))
   (list                        ;; body: list of bytes
     (string->bytes/utf-8 "Hello world!\n"))))

(module+ main
  (serve/servlet
   hello
   #:port 8080
   #:servlet-regexp #rx""))

Save it as hello.rkt and run it with $ racket hello.rkt

Check that it works:

$ curl -v http://localhost:8080/
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 19 Dec 2017 18:13:14 GMT
< Last-Modified: Tue, 19 Dec 2017 18:13:14 GMT
< Content-Length: 13
< Server: Racket
<
Hello world!
* Connection #0 to host localhost left intact

How do you develop with Yesod

:: haskell, web

Yesod tutorials and examples can be found on the Yesod book, which is freely available online.

However, many of the examples given are self-contained (which means that handler functions, data models and templates are defined in a single module, where a Warp web server is started as well). There is a chapter that describes the scaffolded template, i.e. what you get after you start with Yesod, but it is rather a description of its advantages and structure, not so much a detailed how-to.

The purpose of this post is to explain to a new Yesod developer “What do I do after … ?” :

$ yesod init