refactor: Implement Lispy improvements - templates, strings, and error handling
This commit implements three major refactorings to make the codebase more
idiomatic and maintainable:
1. Template Path Centralization
- Add *template-directory* parameter and helper functions
- Replace 11+ instances of repetitive template loading boilerplate
- New functions: template-path, load-template in template-utils.lisp
2. String Construction with FORMAT
- Replace concatenate with format for external URLs (Icecast, static files)
- Maintain Radiance URI handling for internal routes
- Applied to stream URLs, status endpoints, and API responses
3. Error Handling with Custom Conditions
- NEW FILE: conditions.lisp with comprehensive error hierarchy
- Custom conditions: not-found-error, authentication-error,
authorization-error, validation-error, database-error, asteroid-stream-error
- Helper macros: with-error-handling, with-db-error-handling
- Helper functions: signal-not-found, signal-validation-error, etc.
- Refactored 19 API endpoints and page routes
- Proper HTTP status codes: 404, 401, 403, 400, 500
Changes:
- conditions.lisp: NEW (180+ lines of error handling infrastructure)
- asteroid.asd: Add conditions.lisp to system components
- asteroid.lisp: Refactor 30+ endpoints, eliminate 200+ lines of boilerplate
- template-utils.lisp: Add centralized template loading helpers
- frontend-partials.lisp: Update template loading and string construction
Net result: -97 lines of code, significantly improved error handling,
more maintainable and idiomatic Common Lisp.
All changes tested and verified:
- Clean build
- All endpoints functional
- Error handling returns proper HTTP codes
- No regressions
This commit is contained in:
parent
85881b8fb6
commit
5882141cfa
|
|
@ -33,6 +33,8 @@
|
||||||
:pathname "./"
|
:pathname "./"
|
||||||
:components ((:file "app-utils")
|
:components ((:file "app-utils")
|
||||||
(:file "module")
|
(:file "module")
|
||||||
|
(:module :config
|
||||||
|
:components ((:file radiance-postgres)))
|
||||||
(:file "conditions")
|
(:file "conditions")
|
||||||
(:file "database")
|
(:file "database")
|
||||||
(:file "template-utils")
|
(:file "template-utils")
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
(in-package :asteroid)
|
^(in-package :asteroid)
|
||||||
|
|
||||||
(defun icecast-now-playing (icecast-base-url)
|
(defun icecast-now-playing (icecast-base-url)
|
||||||
"Fetch now-playing information from Icecast server.
|
|
||||||
|
|
||||||
ICECAST-BASE-URL - Base URL of the Icecast server (e.g. http://localhost:8000)
|
|
||||||
|
|
||||||
Returns a plist with :listenurl, :title, and :listeners, or NIL on error."
|
|
||||||
(let* ((icecast-url (format nil "~a/admin/stats.xml" icecast-base-url))
|
(let* ((icecast-url (format nil "~a/admin/stats.xml" icecast-base-url))
|
||||||
(response (drakma:http-request icecast-url
|
(response (drakma:http-request icecast-url
|
||||||
:want-stream nil
|
:want-stream nil
|
||||||
|
|
@ -14,32 +9,29 @@
|
||||||
(let ((xml-string (if (stringp response)
|
(let ((xml-string (if (stringp response)
|
||||||
response
|
response
|
||||||
(babel:octets-to-string response :encoding :utf-8))))
|
(babel:octets-to-string response :encoding :utf-8))))
|
||||||
;; Extract total listener count from root <listeners> tag (sums all mount points)
|
;; Simple XML parsing to extract source information
|
||||||
;; Extract title from asteroid.mp3 mount point
|
;; Look for <source mount="/asteroid.mp3"> sections and extract title, listeners, etc.
|
||||||
(let* ((total-listeners (multiple-value-bind (match groups)
|
(multiple-value-bind (match-start match-end)
|
||||||
(cl-ppcre:scan-to-strings "<listeners>(\\d+)</listeners>" xml-string)
|
(cl-ppcre:scan "<source mount=\"/asteroid\\.mp3\">" xml-string)
|
||||||
(if (and match groups)
|
|
||||||
(parse-integer (aref groups 0) :junk-allowed t)
|
(if match-start
|
||||||
0)))
|
(let* ((source-section (subseq xml-string match-start
|
||||||
;; Get title from asteroid.mp3 mount point
|
(or (cl-ppcre:scan "</source>" xml-string :start match-start)
|
||||||
(mount-start (cl-ppcre:scan "<source mount=\"/asteroid\\.mp3\">" xml-string))
|
(length xml-string))))
|
||||||
(title (if mount-start
|
(titlep (cl-ppcre:all-matches "<title>" source-section))
|
||||||
(let* ((source-section (subseq xml-string mount-start
|
(listenersp (cl-ppcre:all-matches "<listeners>" source-section))
|
||||||
(or (cl-ppcre:scan "</source>" xml-string :start mount-start)
|
(title (if titlep (cl-ppcre:regex-replace-all ".*<title>(.*?)</title>.*" source-section "\\1") "Unknown"))
|
||||||
(length xml-string)))))
|
(listeners (if listenersp (cl-ppcre:regex-replace-all ".*<listeners>(.*?)</listeners>.*" source-section "\\1") "0")))
|
||||||
(multiple-value-bind (match groups)
|
`((:listenurl . ,(format nil "~a/asteroid.mp3" *stream-base-url*))
|
||||||
(cl-ppcre:scan-to-strings "<title>(.*?)</title>" source-section)
|
(:title . ,title)
|
||||||
(if (and match groups)
|
(:listeners . ,(parse-integer listeners :junk-allowed t))))
|
||||||
(aref groups 0)
|
`((:listenurl . ,(format nil "~a/asteroid.mp3" *stream-base-url*))
|
||||||
"Unknown")))
|
(:title . "Unknown")
|
||||||
"Unknown")))
|
(:listeners . "Unknown"))))))))
|
||||||
`((:listenurl . ,(format nil "~a/asteroid.mp3" *stream-base-url*))
|
|
||||||
(:title . ,title)
|
|
||||||
(:listeners . ,total-listeners)))))))
|
|
||||||
|
|
||||||
(define-api asteroid/partial/now-playing () ()
|
(define-api asteroid/partial/now-playing () ()
|
||||||
"Get Partial HTML with live status from Icecast server"
|
"Get Partial HTML with live status from Icecast server"
|
||||||
(with-error-handling
|
(handler-case
|
||||||
(let ((now-playing-stats (icecast-now-playing *stream-base-url*)))
|
(let ((now-playing-stats (icecast-now-playing *stream-base-url*)))
|
||||||
(if now-playing-stats
|
(if now-playing-stats
|
||||||
(progn
|
(progn
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue