Skip to content

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kaappi-web

Web framework for Kaappi Scheme. Ring/Compojure-inspired: handlers are functions, middleware are higher-order functions, routes are data.

Pure Scheme — no build step. Depends on kaappi-http and kaappi-json.

Quick Start

(import (kaappi web))

(define app
  (routes
    (GET "/" (lambda (req params)
              (text-response "Hello, World!")))

    (GET "/users/:id" (lambda (req params)
                        (json-response
                          `(("id" . ,(param/number params "id"))))))

    (POST "/users" (lambda (req params)
                     (let ((body (request-json req)))
                       (json-response body 201))))))

(serve
  (wrap app wrap-json-body wrap-logging wrap-errors)
  8080)
kaappi --lib-path /path/to/kaappi-http/lib \
       --lib-path /path/to/kaappi-json/lib \
       --lib-path /path/to/kaappi-web/lib \
       app.scm

Routing

(routes
  (GET    "/path"       handler)
  (POST   "/path"       handler)
  (PUT    "/path"       handler)
  (DELETE "/path"       handler)
  (PATCH  "/path"       handler)
  (HEAD   "/path"       handler))

Path parameters with :name:

(GET "/users/:id/posts/:pid"
  (lambda (req params)
    ;; params = (("id" . "42") ("pid" . "7"))
    (param params "id")           ; => "42"
    (param/number params "id")    ; => 42
    ...))

Automatic 404 when no route matches.

Response Helpers

Procedure Description
(json-response data [status]) JSON with application/json
(text-response text [status]) Plain text
(html-response html [status]) HTML
(redirect url [status]) 302 redirect (or custom)
(no-content) 204 No Content

Middleware

Middleware are (handler → handler) functions, composed with wrap:

(wrap app
  wrap-json-body          ; parse JSON request bodies
  wrap-logging            ; log requests to stdout
  (wrap-cors "*")         ; add CORS headers
  wrap-errors)            ; catch exceptions → 500

Built-in

Middleware Description
wrap-json-body Parses application/json bodies; access via (request-json req)
wrap-logging Prints METHOD /path to stdout
(wrap-cors origin) CORS headers + OPTIONS preflight handling
wrap-errors Catches exceptions, returns {"error":"Internal server error"}

Custom middleware

(define (wrap-auth handler)
  (lambda (request)
    (if (request-header request "authorization")
        (handler request)
        (json-response '(("error" . "Unauthorized")) 401))))

Tests

# Offline routing tests
kaappi --lib-path ... tests/test-routing.scm

# Integration tests (starts server, curls it)
bash tests/test-app.sh

License

MIT

About

Web framework for Kaappi Scheme — routing, middleware, JSON helpers

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages