Improve routing design
This commit is contained in:
parent
897dd291c7
commit
cef077e986
4 changed files with 62 additions and 65 deletions
src
15
src/app.lisp
Normal file
15
src/app.lisp
Normal file
|
@ -0,0 +1,15 @@
|
|||
(defpackage #:hp/app
|
||||
(:use #:cl)
|
||||
(:local-nicknames (#:routes #:hp/routes/*))
|
||||
(:import-from #:lack)
|
||||
(:export #:*app*))
|
||||
(in-package #:hp/app)
|
||||
|
||||
(defparameter *app*
|
||||
(lack:builder (:static
|
||||
:path "/static/"
|
||||
:root (asdf:system-relative-pathname :hp "static/"))
|
||||
routes:*index-app*))
|
||||
|
||||
; for clackup cmd
|
||||
*app*
|
|
@ -2,29 +2,24 @@
|
|||
(:nicknames #:hp/main)
|
||||
(:use #:cl)
|
||||
(:import-from #:clack)
|
||||
(:import-from #:lack)
|
||||
(:local-nicknames (#:pages #:hp/pages/**/*))
|
||||
(:export #:start-app
|
||||
#:stop-app))
|
||||
(:import-from #:hp/app
|
||||
#:*app*)
|
||||
(:export #:start-server
|
||||
#:stop-server))
|
||||
(in-package :hp)
|
||||
|
||||
(defparameter *handler* nil)
|
||||
(defparameter *server* nil)
|
||||
|
||||
(defun start-app ()
|
||||
(unless *handler*
|
||||
(setf *handler*
|
||||
(clack:clackup (lack:builder
|
||||
(:static
|
||||
:path "/static/"
|
||||
:root (asdf:system-relative-pathname
|
||||
:hp
|
||||
"static/"))
|
||||
pages:*index-app*)
|
||||
:address "localhost"
|
||||
:port 3000))))
|
||||
(defun start-server ()
|
||||
(if *server*
|
||||
(format t "Server is already running.~%")
|
||||
(setf *server* (clack:clackup *app*
|
||||
:address "localhost"
|
||||
:port 3000))))
|
||||
|
||||
(defun stop-app ()
|
||||
(when *handler*
|
||||
(clack:stop *handler*)
|
||||
(setf *handler* nil)
|
||||
t))
|
||||
(defun stop-server ()
|
||||
(if *server*
|
||||
(prog1
|
||||
(clack:stop *server*)
|
||||
(setf *server* nil))
|
||||
(format t "No servers running.~%")))
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
(defpackage #:hp/pages/index
|
||||
(:use #:cl)
|
||||
(:local-nicknames (#:jg #:jingle))
|
||||
(:local-nicknames (#:mk #:markup))
|
||||
(:local-nicknames (#:ui #:hp/ui/*))
|
||||
(:local-nicknames (#:utils #:hp/utils/*))
|
||||
(:export #:*index-app*))
|
||||
(in-package #:hp/pages/index)
|
||||
|
||||
(mk:enable-reader)
|
||||
|
||||
(defparameter *counter* 0)
|
||||
|
||||
(mk:deftag counter (&key id value)
|
||||
<div id=id >,(progn value)</div>)
|
||||
|
||||
(defun index-page (params)
|
||||
(declare (ignore params))
|
||||
(jg:with-html-response
|
||||
(mk:write-html
|
||||
<ui:layout>
|
||||
<counter id="counter" value=*counter* />
|
||||
<button hx-target="#counter" hx-post="/decrease"> - </button>
|
||||
<button hx-target="#counter" hx-post="/increase"> + </button>
|
||||
</ui:layout>)))
|
||||
|
||||
(defun increase (params)
|
||||
(declare (ignore params))
|
||||
(jg:with-html-response
|
||||
(mk:write-html <counter id="counter" value=(incf *counter*) />)))
|
||||
|
||||
(defun decrease (params)
|
||||
(declare (ignore params))
|
||||
(jg:with-html-response
|
||||
(mk:write-html <counter id= "counter" value=(decf *counter*) />)))
|
||||
|
||||
(defparameter *index-app* (jg:make-app))
|
||||
|
||||
(utils:register-routes
|
||||
*index-app*
|
||||
`((:method :GET :path "/" :handler ,#'index-page)
|
||||
(:method :POST :path "/increase" :handler ,#'increase)
|
||||
(:method :POST :path "/decrease" :handler ,#'decrease)))
|
30
src/routes/index.lisp
Normal file
30
src/routes/index.lisp
Normal file
|
@ -0,0 +1,30 @@
|
|||
(defpackage #:hp/routes/index
|
||||
(:use #:cl)
|
||||
(:local-nicknames (#:mk #:markup))
|
||||
(:local-nicknames (#:jg #:jingle))
|
||||
(:local-nicknames (#:ui #:hp/ui/*))
|
||||
(:local-nicknames (#:utils #:hp/utils/*))
|
||||
(:export #:*index-app*))
|
||||
(in-package #:hp/routes/index)
|
||||
|
||||
(mk:enable-reader)
|
||||
|
||||
;;; View
|
||||
|
||||
(mk:deftag page ()
|
||||
<ui:layout>
|
||||
<h1>Hello HTMX from Common Lisp!</h1>
|
||||
</ui:layout>)
|
||||
|
||||
;;; Controller
|
||||
|
||||
(defun index (params)
|
||||
(declare (ignore params))
|
||||
(jg:with-html-response
|
||||
(mk:write-html <page />)))
|
||||
|
||||
(defparameter *index-app* (jg:make-app))
|
||||
|
||||
(utils:register-routes
|
||||
*index-app*
|
||||
`((:method :GET :path "/" :handler ,#'index)))
|
Loading…
Add table
Add a link
Reference in a new issue