Integral-Rest

稚拙のIntegral-Restを紹介.

  1. Integral-Rest
  2. See Also

Integral-Rest

Integral-RestはIntegralのDAO tableからREST APIを簡単に作るLibrary.

(defpackage sample
  (:use :cl
        :integral
        :integral-rest))
(in-package :sample)

(connect-toplevel :sqlite3 :database-name ":memory:")

(defclass user ()
  ((id :initarg :id
       :type integer
       :primary-key t
       :accessor user-id)
   (name :initarg :name
         :type string
         :accessor user-name))
  (:metaclass integral:<dao-table-class>))

(ensure-table-exists (find-class 'user))

:metaclassとしてintegral:<dao-table-class>を指定し、userdefclassする.
後は(set-rest-app)をすると*rest-app*がREST API appに束縛されので、
*rest-app*ningle:<app>instanceなので、これをそのままclack:clackupできる.

(set-rest-app)

(clack:clackup *rest-app*)
;; => Listening on localhost:5000.

これだけでREST API ServerがPort 5000で立ち上がる.

(create-dao 'user :name "Rudolph")
;; => #<USER id: 1>

(dex:get "http://localhost:5000/api/users")
;; => "[{\"id\":1,\"name\":\"Rudolph\"}]"

(dex:get "http://localhost:5000/api/users/1")
;; => "{\"id\":1,\"name\":\"Rudolph\"}"

(dex:post "http://localhost:5000/api/users" :contest '(("name" . "Miller")))
;; => "{\"id\":2,\"name\":\"Miller\"}"

(find-dao 'user 2)
;; => #<USER id: 2 name: "Miller">

(dex:put "http://localhost:5000/api/users/2" :contest '(("name" . "Tom")))
;; => "{\"id\":2,\"name\":\"Tom\"}"

(find-dao 'user 2)
;; => #<USER id: 2 name: "Tom">

(dex:delete "http://localhost:5000/api/users/2")
;; => "{\"id\":2,\"name\":\"Tom\"}"

(find-dao 'user 2)
;; => NIL

ちょっとしたAPI Serverだとapp.lispに十数行で書ける.

See Also