refactoring

This commit is contained in:
2026-09-01 09:31:49 +02:00
parent b3a5a0b345
commit a5a53b7efc
20 changed files with 2082 additions and 1032 deletions
+56 -11
View File
@@ -3,7 +3,6 @@
(require racket/cmdline
racket/contract
racket/list
racket/mpair
racket/runtime-path
racket/string
simple-ini
@@ -24,15 +23,15 @@
(define-runtime-path default-playlist-keystore
"data/playlists.keystore")
(define-runtime-path default-log-file
"data/rkt-web-player.log")
;;; Returns the key/value pairs from one INI section in source order.
(define (ini-section-key-values config section-name)
(let ((section (assoc section-name (mcdr config))))
(if section
(for/list ((line (in-list (cdr section)))
#:when (and (pair? line)
(eq? (car line) 'keyval)))
(cons (symbol->string (cadr line))
(caddr line)))
'())))
(map (λ (key)
(cons (symbol->string key)
(ini-get config section-name key #f)))
(ini-keys config section-name)))
(define (configuration-list value defaults)
(cond
@@ -65,6 +64,12 @@
#:local-output? [local-output? #t]
#:playlist-keystore
[playlist-keystore default-playlist-keystore]
#:log-file
[log-file default-log-file]
#:log-retention-days
[log-retention-days 7]
#:log-level
[log-level 'debug]
#:launch-browser? [launch-browser? #t])
(->* ((listof library-spec/c))
(#:allowed-agent-ids (listof string?)
@@ -76,8 +81,15 @@
#:dlna-port exact-positive-integer?
#:local-output? boolean?
#:playlist-keystore (or/c path-string? #f)
#:log-file path-string?
#:log-retention-days exact-positive-integer?
#:log-level symbol?
#:launch-browser? boolean?)
any)
(sl-log-to-rotating-file log-file log-retention-days)
(sl-set-log-level log-level)
(let* ((libraries (make-music-libraries music-paths))
(player (make-player libraries
#:allowed-agent-ids allowed-agent-ids
@@ -104,6 +116,23 @@
(λ ()
(player-close! player)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests for module main.rkt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module+ test
(require rackunit)
(let ((config (make-ini)))
(ini-set! config 'libraries 'music "/srv/music")
(ini-set! config 'server 'port 8080)
(ini-set! config 'libraries 'archive "/srv/archive")
(check-equal?
(ini-section-key-values config 'libraries)
'(("music" . "/srv/music")
("archive" . "/srv/archive")))
(check-equal? (ini-section-key-values config 'missing) '())))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Command line
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -115,6 +144,7 @@
(define playlist-keystore #f)
(define launch-browser? #t)
(define config-file #f)
(define log-file #f)
(define music-paths
(command-line
@@ -138,6 +168,9 @@
[("--no-browser")
"Do not open the web interface automatically"
(set! launch-browser? #f)]
[("--log-file") path
"Log file path, retention is normally set to 7 days, but can be changed in the INI file"
(set! log-file path)]
#:args paths
paths))
@@ -189,7 +222,6 @@
music-paths
configured-paths))
(sl-log-to-display)
(run-web-player
all-libraries
#:allowed-agent-ids allowed-agent-ids
@@ -211,4 +243,17 @@
(not (string=? (string-trim (format "~a" configured)) ""))
configured))
default-playlist-keystore)
#:launch-browser? launch-browser?))
#:launch-browser? launch-browser?
#:log-file
(if (eq? log-file #f)
default-log-file
(if (eq? (ini-get config 'logging 'log-file #f) #f)
log-file
(ini-get config 'logging 'log-file default-log-file)))
#:log-retention-days
(ini-get config 'logging 'log-retention-days 7)
#:log-level
(string->symbol
(format "~a" (ini-get config 'logging 'log-level 'debug)))
)
)