diff --git a/.env.example b/.env.example index 5166f7b..742b580 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,6 @@ WEBSITE_ENV= WEBSITE_URL= MICROCMS_SERVICE_DOMAIN= MICROCMS_API_KEY= +MICROCMS_WEBHOOK_KEY= CLOUDFLARE_ZONE_ID= CLOUDFLARE_API_KEY= diff --git a/qlfile b/qlfile index 1b680f7..b4862b2 100644 --- a/qlfile +++ b/qlfile @@ -11,3 +11,4 @@ ql local-time ql function-cache ql jonathan ql access +ql flexi-streams diff --git a/qlfile.lock b/qlfile.lock index f3d33ac..6421bac 100644 --- a/qlfile.lock +++ b/qlfile.lock @@ -54,3 +54,7 @@ (:class qlot/source/ql:source-ql :initargs (:%version :latest) :version "ql-2024-10-12")) +("flexi-streams" . + (:class qlot/source/ql:source-ql + :initargs (:%version :latest) + :version "ql-2024-10-12")) diff --git a/src/components/header.lisp b/src/components/header.lisp index d4a2f9c..9546095 100644 --- a/src/components/header.lisp +++ b/src/components/header.lisp @@ -1,6 +1,7 @@ (defpackage #:website/components/header (:use #:cl - #:website/helper) + #:hsx + #:jingle) (:export #:~header)) (in-package #:website/components/header) diff --git a/src/components/metadata.lisp b/src/components/metadata.lisp index 780e2c8..f8fb04a 100644 --- a/src/components/metadata.lisp +++ b/src/components/metadata.lisp @@ -1,6 +1,7 @@ (defpackage #:website/components/metadata (:use #:cl - #:website/helper) + #:hsx + #:jingle) (:import-from #:website/lib/env #:website-url) (:export #:~metadata)) diff --git a/src/helper.lisp b/src/helper.lisp index a367676..c208395 100644 --- a/src/helper.lisp +++ b/src/helper.lisp @@ -1,15 +1,26 @@ (uiop:define-package #:website/helper - (:use #:cl) - (:use-reexport #:hsx - #:jingle - #:jonathan - #:access) - (:export #:api-p)) + (:use #:cl + #:jingle) + (:import-from #:flexi-streams + #:make-flexi-stream) + (:import-from #:jonathan + #:parse) + (:export #:api-request-p + #:get-request-body-plist)) (in-package #:website/helper) (defun starts-with-p (prefix string) (let ((pos (search prefix string :start1 0 :end1 (length prefix) :start2 0))) (and pos (= pos 0)))) -(defun api-p () +(defun api-request-p () (starts-with-p "/api/" (request-uri *request*))) + +(defun get-request-body-plist () + (parse + (let ((text-stream (make-flexi-stream (request-raw-body *request*) + :external-format :utf-8))) + (with-output-to-string (out) + (loop :for char := (read-char text-stream nil) + :while char + :do (write-char char out)))))) diff --git a/src/lib/cms.lisp b/src/lib/cms.lisp index 7f24118..7f75c80 100644 --- a/src/lib/cms.lisp +++ b/src/lib/cms.lisp @@ -17,21 +17,19 @@ (setf microcms:*service-domain* (microcms-service-domain)) (setf microcms:*api-key* (microcms-api-key)) -(defmacro memorize (name timeout) +(defmacro memorize (name) (let ((origin (gensym))) `(progn (setf (fdefinition ',origin) (fdefinition ',name)) - (defcached (,name :timeout ,timeout) (&key query) + (defcached ,name (&key query) (,origin :query query))))) -(defparameter *timeout* 60) - (define-object-client about) -(memorize get-about *timeout*) +(memorize get-about) (define-object-client work) -(memorize get-work *timeout*) +(memorize get-work) (define-list-client blog) -(memorize get-blog-list *timeout*) -(memorize get-blog-detail *timeout*) +(memorize get-blog-list) +(memorize get-blog-detail) diff --git a/src/lib/env.lisp b/src/lib/env.lisp index a6e1ffb..f0a4bb1 100644 --- a/src/lib/env.lisp +++ b/src/lib/env.lisp @@ -18,3 +18,4 @@ (env-var website-url "WEBSITE_URL") (env-var microcms-service-domain "MICROCMS_SERVICE_DOMAIN") (env-var microcms-api-key "MICROCMS_API_KEY") +(env-var microcms-webhook-key "MICROCMS_WEBHOOK_KEY") diff --git a/src/renderer.lisp b/src/renderer.lisp index 64c6724..7863e38 100644 --- a/src/renderer.lisp +++ b/src/renderer.lisp @@ -1,8 +1,11 @@ (defpackage #:website/renderer (:use #:cl - #:website/helper) - (:import-from #:hsx/element - #:element) + #:hsx + #:jingle) + (:import-from #:jonathan + #:to-json) + (:import-from #:website/helper + #:api-request-p) (:import-from #:website/components/metadata #:~metadata) (:import-from #:website/components/scripts @@ -14,7 +17,7 @@ (defmethod jingle:process-response :around ((app jingle:app) result) (when (eq (request-method *request*) :get) (set-response-header :cache-control "public, max-age=60")) - (cond ((api-p) + (cond ((api-request-p) (set-response-header :content-type "application/json; charset=utf-8") (call-next-method app (to-json result))) (t diff --git a/src/routes/about.lisp b/src/routes/about.lisp index fa84d69..1fd330c 100644 --- a/src/routes/about.lisp +++ b/src/routes/about.lisp @@ -1,6 +1,8 @@ (defpackage #:website/routes/about (:use #:cl - #:website/helper) + #:hsx + #:jingle + #:access) (:import-from #:website/lib/cms #:get-about) (:import-from #:website/lib/time diff --git a/src/routes/api/revalidate.lisp b/src/routes/api/revalidate.lisp new file mode 100644 index 0000000..7c0457e --- /dev/null +++ b/src/routes/api/revalidate.lisp @@ -0,0 +1,24 @@ +(defpackage #:website/routes/api/revalidate + (:use #:cl + #:jingle) + (:import-from #:function-cache + #:clear-cache) + (:import-from #:website/lib/env + #:microcms-webhook-key) + (:import-from #:website/helper + #:get-request-body-plist) + (:import-from #:website/lib/cms + #:get-about) + (:export #:handle-post)) +(in-package #:website/routes/api/revalidate) + +(defun handle-post (params) + (declare (ignore params)) + (unless (string= (car (get-request-header "X-MICROCMS-WEBHOOK-KEY")) + (microcms-webhook-key)) + (set-response-status :unauthorized) + (return-from handle-post '(:|message| "Invalid token"))) + (let* ((body (get-request-body-plist)) + (api (getf body :|api|))) + (cond ((string= api "about") (clear-cache 'get-about))) + '(:|message| "ok"))) diff --git a/src/routes/blog.lisp b/src/routes/blog.lisp index 051e7df..820b359 100644 --- a/src/routes/blog.lisp +++ b/src/routes/blog.lisp @@ -1,6 +1,7 @@ (defpackage #:website/routes/blog (:use #:cl - #:website/helper) + #:hsx + #:jingle) (:export #:handle-get)) (in-package #:website/routes/blog) diff --git a/src/routes/index.lisp b/src/routes/index.lisp index fe78adb..398abbc 100644 --- a/src/routes/index.lisp +++ b/src/routes/index.lisp @@ -1,6 +1,7 @@ (defpackage #:website/routes/index (:use #:cl - #:website/helper) + #:hsx + #:access) (:import-from #:website/lib/cms #:get-about) (:export #:handle-get diff --git a/src/routes/not-found.lisp b/src/routes/not-found.lisp index b5ca783..163ff70 100644 --- a/src/routes/not-found.lisp +++ b/src/routes/not-found.lisp @@ -1,6 +1,9 @@ (defpackage #:website/routes/not-found (:use #:cl - #:website/helper) + #:hsx + #:jingle) + (:import-from #:website/helper + #:api-request-p) (:export #:handle-not-found)) (in-package #:website/routes/not-found) @@ -11,7 +14,7 @@ (defun handle-not-found () (setf (context :metadata) *metadata*) - (if (api-p) + (if (api-request-p) '(:|message| "404 Not Found") (hsx (div :class "flex flex-col h-full items-center justify-center gap-y-6" diff --git a/src/routes/work.lisp b/src/routes/work.lisp index 97114cc..c1e6cf4 100644 --- a/src/routes/work.lisp +++ b/src/routes/work.lisp @@ -1,6 +1,7 @@ (defpackage #:website/routes/work (:use #:cl - #:website/helper) + #:hsx + #:jingle) (:import-from #:website/lib/cms #:get-work) (:import-from #:website/lib/time