Skip to content
Web framework for Racket.
Racket
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples bug fix and will use middleware rewirte cros Feb 26, 2020
vela-doc middleware Feb 22, 2020
vela-lib Fix bug Apr 9, 2020
vela handle file Feb 23, 2020
.gitignore .gitignore May 10, 2019
CNAME Create CNAME Feb 24, 2020
README.md update readme Mar 5, 2020
_config.yml Set theme jekyll-theme-hacker Feb 24, 2020

README.md

Vela

Simple web framework to build restful app in Racket.

Features

  • Web handlers with Function or Class.
  • Friendly way to define url routers.
  • Pluggable middlewares when request and response.
  • Request params check and collector.
  • Entirely on the racket webserver lib.

Installation

raco pkg install vela

Quickstart

  #lang racket
  (require vela)

  (define index
    (lambda (req)
      (jsonify (hash 'msg "hello world!" ))))

  (define routers
    (urls
      (url "/" index "index")))

  (app-run routers #:port 8000)

Define Handler

There two ways to define a handler.

Use handler%

(define hello-handler
  (class handler%

    (define/public (get [id null])
      (displayln id)
      (jsonify (hash 'code 200 'msg "handle get" )))

    (define/public (post)
      (jsonify (hash 'code 200 'msg "handle post" )))

    (define/public (put id)
      (jsonify (hash 'code 200 'msg "handle put" )))

    (define/public (delete id)
      (jsonify (hash 'code 200 'msg "handle delete" )))

    (super-new)))

Use simple function:

(define index-handler
  (lambda (req)
    (jsonify (hash 'code 200 'msg "hello api" ))))

or

(define (index-handler req)
  (jsonify (hash 'code 200 'msg "hello api" )))

Response

Use jsonify to return JSON content.

(jsonify (hash 'name "rosso" 'msg "hello world!"))

Use render to render TEXT or HTML template.

plain text

(render "hello world!")

HTML template

(require web-server/templates)
  ...
(render (include-template "index.html"))

URL routes

Use urls and url function to define route.

(define routers
  (urls
    (url "/" index-handler "handler with function")))

Use url-group grouping routes.

(define api-v1 (url-group "/api/v1"))

(define routers
  (urls
	 ...
    (api-v1
      (url "/hellos" hello-handler "hello-list/post")
      (url "/hello/:id" hello-handler "hello-put/delete/get"))))

Use middleware

Use middleware in url or url-group.

(require
  vela
  web-server/http/request-structs)


(define (index-handler req)
  (jsonify "hello!"))


(define (login-required req)
  (jsonify "user not login"))

(define (print-current-time req)
  (displayln (current-seconds)))

(define (say-hi req resp)
  (jsonify "hi"))


(define api-v1 
  (url-group "/cc" #:on-request (list login-required) #:on-response (list say-hi)))

(define routers
  (urls
    (url "/" index-handler  #:on-request (list print-current-time) "index")

    (api-v1
      (url "/index" index-handler)
      (url "/index1" index-handler #:on-request (list login-required) #:on-response (list say-hi) "index1"))))

request middleware must set a request argument. response middleware must set request and response argument.

Customize Headers

You need require make-header from request-structs package.

(require
   (only-in web-server/http/request-structs
      make-header))

(define my-headers
  (list
  	 ...
    (make-header #"Access-Control-Allow-Origin" #"*")
    (make-header #"Access-Control-Allow-Methods" #"PUT, POST, GET, DELETE, PATCH, OPTIONS")))

Customize Response

Use responsefuction

(define (my-custom-rsp xml-doc)
  (response
    #:headers my-headers
    #:mime #"application/xml"
    #:body xml-doc))

examples

Very simple apps build with Vela in the examples folder.

License

Licensed under the MIT License.

You can’t perform that action at this time.