diff --git a/qlfile.lock b/qlfile.lock index 8a3b068..1a9052c 100644 --- a/qlfile.lock +++ b/qlfile.lock @@ -33,7 +33,7 @@ ("piccolo" . (:class qlot/source/git:source-git :initargs (:remote-url "https://github.com/skyizwhite/piccolo.git") - :version "git-8f4a7c4907f2115f98ceb6dd111ac77f75d1be04")) + :version "git-1b617f75393df820cb1ba0546ced07d2fb1d02c3")) ("cl-ppcre" . (:class qlot/source/ql:source-ql :initargs (:%version :latest) diff --git a/src/app.lisp b/src/app.lisp index 153e517..f319d9f 100644 --- a/src/app.lisp +++ b/src/app.lisp @@ -3,19 +3,25 @@ (:local-nicknames (#:jg #:jingle)) (:import-from #:lack) (:local-nicknames (#:utils #:hp/utils/*)) - (:export #:*app*)) + (:export #:*app* + #:update-routes)) (in-package #:hp/app) +(defparameter *raw-app* (jg:make-app)) + +(defun update-routes () + (utils:enable-file-based-routing *raw-app* + :dir "src/routes" + :system "hp" + :system-pathname "src")) + +(update-routes) + (defparameter *app* - (let ((app (jg:make-app))) - (utils:enable-file-based-routing app - :dir "src/routes" - :system "hp" - :system-pathname "src") - (lack:builder (:static - :path "/static/" - :root (asdf:system-relative-pathname :hp "static/")) - app))) + (lack:builder (:static + :path "/static/" + :root (asdf:system-relative-pathname :hp "static/")) + *raw-app*)) ; for clackup cmd *app* diff --git a/src/main.lisp b/src/main.lisp index 22e8a42..d083199 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -1,11 +1,13 @@ -(defpackage :hp +(uiop:define-package :hp (:nicknames #:hp/main) (:use #:cl) (:import-from #:clack) (:import-from #:hp/app - #:*app*) + #:*app* + #:update-routes) (:export #:start-server - #:stop-server)) + #:stop-server + #:update-routes)) (in-package :hp) (defparameter *server* nil) diff --git a/src/routes/index.lisp b/src/routes/index.lisp index b013f4e..9373951 100644 --- a/src/routes/index.lisp +++ b/src/routes/index.lisp @@ -1,4 +1,4 @@ -(defpackage #:hp/routes/index +(uiop:define-package #:hp/routes/index (:use #:cl) (:local-nicknames (#:pi #:piccolo)) (:local-nicknames (#:jg #:jingle)) diff --git a/src/routes/user/=name.lisp b/src/routes/user/=name.lisp new file mode 100644 index 0000000..ddd95ba --- /dev/null +++ b/src/routes/user/=name.lisp @@ -0,0 +1,23 @@ +(uiop:define-package #:hp/routes/user/=name + (:use #:cl) + (:local-nicknames (#:pi #:piccolo)) + (:local-nicknames (#:jg #:jingle)) + (:local-nicknames (#:ui #:hp/ui/*)) + (:export #:on-get)) +(in-package #:hp/routes/user/=name) + +;;; View + +(pi:define-element page (name) + (pi:h + (ui:layout + (section :class "h-full flex justify-center items-center" + (p :class "text-primary text-4xl" + "Hello, " name "!"))))) + +;;; Controller + +(defun on-get (params) + (jg:with-html-response + (jg:with-request-params ((name :name)) params + (pi:element-string (page :name name))))) diff --git a/src/ui/layout.lisp b/src/ui/layout.lisp index 4888963..db3442e 100644 --- a/src/ui/layout.lisp +++ b/src/ui/layout.lisp @@ -1,4 +1,4 @@ -(defpackage #:hp/ui/layout +(uiop:define-package #:hp/ui/layout (:use #:cl) (:local-nicknames (#:pi #:piccolo)) (:export #:layout)) diff --git a/src/utils/routes.lisp b/src/utils/routes.lisp index 772a040..54cb32e 100644 --- a/src/utils/routes.lisp +++ b/src/utils/routes.lisp @@ -1,4 +1,4 @@ -(defpackage #:hp/utils/routes +(uiop:define-package #:hp/utils/routes (:use #:cl) (:local-nicknames (#:alx #:alexandria)) (:local-nicknames (#:re #:cl-ppcre)) @@ -14,8 +14,14 @@ "/" (re:regex-replace "/index" url ""))) +(defun replace-dynamic-annotation (url) + (re:regex-replace "=" url ":")) + +(defun format-url (url) + (replace-dynamic-annotation (remove-index url))) + (defun pathname->url (pathname dir) - (remove-index + (format-url (re:regex-replace (concatenate 'string (namestring (uiop/os:getcwd)) dir) @@ -57,7 +63,7 @@ :for url :in urls :for pkg :in packages :do (loop - :for method in *http-request-methods* + :for method :in *http-request-methods* :do (let ((handler (find-symbol (string (alx:symbolicate 'on- method)) pkg))) (when handler (setf (jg:route app url :method method) handler))))))) diff --git a/tests/example.lisp b/tests/example.lisp index faf4574..90aae3e 100644 --- a/tests/example.lisp +++ b/tests/example.lisp @@ -1,4 +1,4 @@ -(defpackage #:hp-tests/example +(uiop:define-package #:hp-tests/example (:use #:cl #:fiveam)) (in-package #:hp-tests/example)