refactoring volgens skill

This commit is contained in:
2026-08-29 22:25:06 +02:00
parent 09121df0d7
commit b3a5a0b345
20 changed files with 1043 additions and 998 deletions
+79 -38
View File
@@ -1,6 +1,7 @@
#lang racket/base
(require file/sha1
racket/contract
racket/os
racket/path
racket/random
@@ -14,35 +15,75 @@
(struct player-agent-config (file ini app-id server-url name) #:transparent)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Supporting functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (fresh-app-id)
(bytes->hex-string (crypto-random-bytes 32)))
(define (valid-app-id? value)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Provided functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Recognize a playback-agent application identifier.
; pre : value is any Racket value.
; post : No state is changed.
; result : #t only for a 256-bit identifier encoded as 64 hexadecimal digits.
; internals:
; Identifiers are accepted case-insensitively and normalized while
; loading configuration.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define/contract (valid-app-id? value)
(-> any/c boolean?)
(and (string? value)
(regexp-match? #px"^[0-9a-fA-F]{64}$" value)))
(regexp-match? #px"^[0-9a-fA-F]{64}$" value)
#t))
(define (load-player-agent-config [file (get-ini-file 'rkt-web-player-agent)])
(define ini (file->ini file))
(define configured-id (ini-get ini 'agent 'app-id #f))
(define value
(player-agent-config
file
ini
(if (valid-app-id? configured-id)
(string-downcase configured-id)
(fresh-app-id))
(ini-get ini 'server 'url "http://127.0.0.1:8080")
(ini-get ini 'agent 'name
(format "~a playback" (gethostname)))))
(save-player-agent-config! value)
value)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Load the playback-agent INI configuration.
; pre : file is a writable path-string understood by simple-ini.
; post : Missing defaults and a generated application ID are persisted.
; result : A player-agent-config value containing normalized settings.
; internals:
; Reusing the stored application ID preserves the server allowlist;
; only a missing or malformed identifier is replaced.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define/contract (load-player-agent-config
[file (get-ini-file 'rkt-web-player-agent)])
(->* () (path-string?) player-agent-config?)
(let* ((ini (file->ini file))
(configured-id (ini-get ini 'agent 'app-id #f))
(value
(player-agent-config
file
ini
(if (valid-app-id? configured-id)
(string-downcase configured-id)
(fresh-app-id))
(ini-get ini 'server 'url "http://127.0.0.1:8080")
(ini-get ini 'agent 'name
(format "~a playback" (gethostname))))))
(save-player-agent-config! value)
value))
(define (save-player-agent-config! value)
(define ini (player-agent-config-ini value))
(ini-set! ini 'agent 'app-id (player-agent-config-app-id value))
(ini-set! ini 'agent 'name (player-agent-config-name value))
(ini-set! ini 'server 'url (player-agent-config-server-url value))
(ini->file ini (player-agent-config-file value) #:private? #t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Persist a playback-agent configuration.
; pre : value is a player-agent-config with a writable file path.
; post : Its ID, name and server URL are stored in a private INI file.
; result : The result returned by simple-ini's ini->file procedure.
; internals:
; The existing parsed INI value is updated directly so unrelated
; settings remain intact.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define/contract (save-player-agent-config! value)
(-> player-agent-config? void?)
(let ((ini (player-agent-config-ini value)))
(ini-set! ini 'agent 'app-id (player-agent-config-app-id value))
(ini-set! ini 'agent 'name (player-agent-config-name value))
(ini-set! ini 'server 'url (player-agent-config-server-url value))
(ini->file ini (player-agent-config-file value) #:private? #t)))
(module+ test
(require rackunit
@@ -52,18 +93,18 @@
(define test-file (build-path test-directory "agent.ini"))
(dynamic-wind
void
(lambda ()
(define first (load-player-agent-config test-file))
(check-true (valid-app-id? (player-agent-config-app-id first)))
(define changed
(struct-copy player-agent-config first
(server-url "https://music.example.test")
(name "Test output")))
(save-player-agent-config! changed)
(define second (load-player-agent-config test-file))
(check-equal? (player-agent-config-app-id second)
(player-agent-config-app-id first))
(check-equal? (player-agent-config-server-url second)
"https://music.example.test")
(check-equal? (player-agent-config-name second) "Test output"))
(lambda () (delete-directory/files test-directory))))
(λ ()
(let* ((first (load-player-agent-config test-file))
(changed
(struct-copy player-agent-config first
(server-url "https://music.example.test")
(name "Test output"))))
(check-true (valid-app-id? (player-agent-config-app-id first)))
(save-player-agent-config! changed)
(let ((second (load-player-agent-config test-file)))
(check-equal? (player-agent-config-app-id second)
(player-agent-config-app-id first))
(check-equal? (player-agent-config-server-url second)
"https://music.example.test")
(check-equal? (player-agent-config-name second) "Test output"))))
(λ () (delete-directory/files test-directory))))