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)
|
(:nicknames #:hp/main)
|
||||||
(:use #:cl)
|
(:use #:cl)
|
||||||
(:import-from #:clack)
|
(:import-from #:clack)
|
||||||
(:import-from #:lack)
|
(:import-from #:hp/app
|
||||||
(:local-nicknames (#:pages #:hp/pages/**/*))
|
#:*app*)
|
||||||
(:export #:start-app
|
(:export #:start-server
|
||||||
#:stop-app))
|
#:stop-server))
|
||||||
(in-package :hp)
|
(in-package :hp)
|
||||||
|
|
||||||
(defparameter *handler* nil)
|
(defparameter *server* nil)
|
||||||
|
|
||||||
(defun start-app ()
|
(defun start-server ()
|
||||||
(unless *handler*
|
(if *server*
|
||||||
(setf *handler*
|
(format t "Server is already running.~%")
|
||||||
(clack:clackup (lack:builder
|
(setf *server* (clack:clackup *app*
|
||||||
(:static
|
:address "localhost"
|
||||||
:path "/static/"
|
:port 3000))))
|
||||||
:root (asdf:system-relative-pathname
|
|
||||||
:hp
|
|
||||||
"static/"))
|
|
||||||
pages:*index-app*)
|
|
||||||
:address "localhost"
|
|
||||||
:port 3000))))
|
|
||||||
|
|
||||||
(defun stop-app ()
|
(defun stop-server ()
|
||||||
(when *handler*
|
(if *server*
|
||||||
(clack:stop *handler*)
|
(prog1
|
||||||
(setf *handler* nil)
|
(clack:stop *server*)
|
||||||
t))
|
(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