From b18a31df4afd296540ed733698b996f2c3979212 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 24 Aug 2022 20:59:11 +1200 Subject: [PATCH] Use ini for configuration --- .gitignore | 2 +- breezewiki.rkt | 5 ++-- info.rkt | 2 +- src/application-globals.rkt | 5 ++-- src/config.rkt | 53 +++++++++++++++++++++++-------------- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 7da65b0..caf5d54 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ compiled # Personal -/config.txt +/config.ini diff --git a/breezewiki.rkt b/breezewiki.rkt index e6f537f..1f39521 100644 --- a/breezewiki.rkt +++ b/breezewiki.rkt @@ -23,7 +23,8 @@ #".svg" #"image/svg+xml")) (serve/launch/wait - #:port (config-get 'port) + #:listen-ip (if (config-true? 'debug) "127.0.0.1" #f) + #:port (string->number (config-get 'port)) (λ (quit) (sequencer:make (pathprocedure:make "/proxy" page-proxy) @@ -38,5 +39,5 @@ #:path->mime-type (lambda (u) (hash-ref mime-types (path-get-extension u))) - #:cache-no-cache (config-get 'debug) #;"browser applies heuristics if unset")) + #:cache-no-cache (config-true? 'debug) #;"browser applies heuristics if unset")) (lift:make page-not-found)))) diff --git a/info.rkt b/info.rkt index 2dcfedb..74152ef 100644 --- a/info.rkt +++ b/info.rkt @@ -1,3 +1,3 @@ #lang info -(define build-deps '("rackunit-lib" "web-server-lib" "http-easy-lib" "html-parsing" "html-writing" "json-pointer")) +(define build-deps '("rackunit-lib" "web-server-lib" "http-easy-lib" "html-parsing" "html-writing" "json-pointer" "ini-lib")) diff --git a/src/application-globals.rkt b/src/application-globals.rkt index cb875f4..49ea436 100644 --- a/src/application-globals.rkt +++ b/src/application-globals.rkt @@ -47,9 +47,10 @@ (div (@ (id "mw-content-text")) ,content)) (footer (@ (class "custom-footer")) - (img (@ (class "my-logo") (src "/static/breezewiki.svg"))) (div (@ (class "custom-footer__cols")) (div + (p + (img (@ (class "my-logo") (src "/static/breezewiki.svg")))) (p (a (@ (href "https://gitdab.com/cadence/breezewiki")) ,(format "~a source code" (config-get 'application-name)))) @@ -61,7 +62,7 @@ (a (@ (href "https://cadence.moe/contact")) "Cadence.")) `(p - ,(format "This unofficial instance is based off the ~a source code, but is not administered by its developer." (config-get 'application-name))))) + ,(format "This unofficial instance is based off the ~a source code, but is not controlled by the code developer." (config-get 'application-name))))) (div (p "This page displays proxied content from " (a (@ (href ,source-url) (rel "noreferrer")) ,source-url) diff --git a/src/config.rkt b/src/config.rkt index 7618cd7..d2bcf7a 100644 --- a/src/config.rkt +++ b/src/config.rkt @@ -1,36 +1,49 @@ #lang racket/base -(require racket/runtime-path) +(require racket/runtime-path + ini) (provide + config-true? config-get) -(define-runtime-path path-config "../config.txt") +(define-runtime-path path-config "../config.ini") + +(define (config-true? key) + (not (member (hash-ref config key) '("" "false")))) (define (config-get key) (hash-ref config key)) (define default-config - '((port . 10416) - (debug . #f) - (instance-is-official . #f) ; please don't turn this on, or you will make me very upset + '((port . "10416") + (debug . "false") + (instance-is-official . "false") ; please don't turn this on, or you will make me very upset (application-name . "BreezeWiki"))) (define config (make-hasheq (append default-config - (with-handlers ([exn:fail:filesystem:errno? (λ (exn) - '())]) - (call-with-input-file path-config - (λ (in) - (let loop ([alist '()]) - (let ([key (read in)] - [value (read in)]) - (if (eq? value eof) - alist - (loop (cons (cons key - (cond - [(eq? value 'true) #t] - [(eq? value 'false) #f] - [#t value])) - alist))))))))))) + (with-handlers ([exn:fail:filesystem:errno? + (λ (exn) + (begin0 + '() + (displayln "note: config file not detected, using defaults")))] + [exn:fail:contract? + (λ (exn) + (begin0 + '() + (displayln "note: config file empty or missing [] section, using defaults")))]) + (let ([l (hash->list + (hash-ref + (ini->hash + (call-with-input-file path-config + (λ (in) + (read-ini in)))) + '||))]) + (begin0 + l + (printf "note: ~a items loaded from config file~n" (length l)))))))) + +(when (config-true? 'debug) + (printf "config: ~v~n" config))