Score
0
Watch 2 Star 1 Fork 0

nuty / velaRacketMIT

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
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.

Comments ( 1 )

Sign in for post a comment

Racket
1
https://gitee.com/nuty/vela.git
git@gitee.com:nuty/vela.git
nuty
vela
vela
master

Help Search