2022-10-04 09:00:44 +00:00
|
|
|
#lang typed/racket/base
|
2022-09-17 10:51:42 +00:00
|
|
|
(require racket/function
|
|
|
|
racket/pretty
|
2022-08-30 10:13:26 +00:00
|
|
|
racket/runtime-path
|
2023-02-05 04:56:15 +00:00
|
|
|
racket/string
|
|
|
|
typed/ini)
|
2022-08-23 09:57:42 +00:00
|
|
|
|
|
|
|
(provide
|
2022-09-08 02:05:47 +00:00
|
|
|
config-parameter
|
2022-08-24 08:59:11 +00:00
|
|
|
config-true?
|
2022-08-23 09:57:42 +00:00
|
|
|
config-get)
|
|
|
|
|
2022-10-04 09:00:44 +00:00
|
|
|
(module+ test
|
2023-02-05 04:56:15 +00:00
|
|
|
(require "../lib/typed-rackunit.rkt"))
|
2022-10-04 09:00:44 +00:00
|
|
|
|
2022-08-24 08:59:11 +00:00
|
|
|
(define-runtime-path path-config "../config.ini")
|
|
|
|
|
2022-10-04 09:00:44 +00:00
|
|
|
(: config-parameter (Symbol -> (Parameterof String)))
|
2022-09-08 02:05:47 +00:00
|
|
|
(define (config-parameter key)
|
|
|
|
(hash-ref config key))
|
|
|
|
|
2022-10-04 09:00:44 +00:00
|
|
|
(: config-true? (Symbol -> Boolean))
|
2022-08-24 08:59:11 +00:00
|
|
|
(define (config-true? key)
|
2022-09-08 02:05:47 +00:00
|
|
|
(not (member ((config-parameter key)) '("" "false"))))
|
2022-08-24 01:23:46 +00:00
|
|
|
|
2022-10-04 09:00:44 +00:00
|
|
|
(: config-get (Symbol -> String))
|
2022-08-23 09:57:42 +00:00
|
|
|
(define (config-get key)
|
2022-09-08 02:05:47 +00:00
|
|
|
((config-parameter key)))
|
2022-08-23 09:57:42 +00:00
|
|
|
|
|
|
|
(define default-config
|
2022-09-04 01:38:30 +00:00
|
|
|
'((application_name . "BreezeWiki")
|
|
|
|
(canonical_origin . "")
|
2022-08-24 08:59:11 +00:00
|
|
|
(debug . "false")
|
2022-10-22 11:35:34 +00:00
|
|
|
(feature_search_suggestions . "true")
|
2022-09-04 01:38:30 +00:00
|
|
|
(instance_is_official . "false") ; please don't turn this on, or you will make me very upset
|
2022-10-04 08:13:07 +00:00
|
|
|
(log_outgoing . "true")
|
2022-09-07 12:16:24 +00:00
|
|
|
(port . "10416")
|
2023-02-05 04:56:15 +00:00
|
|
|
(strict_proxy . "false")
|
|
|
|
|
|
|
|
(feature_offline::enabled . "false")
|
|
|
|
(feature_offline::format . "json.gz")
|
2023-02-08 10:15:13 +00:00
|
|
|
(feature_offline::only . "false")
|
|
|
|
|
|
|
|
(access_log::enabled . "false")))
|
2022-08-23 09:57:42 +00:00
|
|
|
|
2022-09-08 02:05:47 +00:00
|
|
|
(define loaded-alist
|
|
|
|
(with-handlers
|
|
|
|
([exn:fail:filesystem:errno?
|
|
|
|
(λ (exn)
|
2023-02-05 04:56:15 +00:00
|
|
|
(displayln "note: config file not detected, using defaults")
|
|
|
|
'())]
|
2022-09-08 02:05:47 +00:00
|
|
|
[exn:fail:contract?
|
|
|
|
(λ (exn)
|
2023-02-05 04:56:15 +00:00
|
|
|
(displayln "note: config file empty or missing [] section, using defaults")
|
|
|
|
'())])
|
|
|
|
(define h (in-hash
|
|
|
|
(ini->hash
|
|
|
|
(call-with-input-file path-config
|
|
|
|
(λ (in)
|
|
|
|
(read-ini in))))))
|
2022-09-08 02:05:47 +00:00
|
|
|
(define l
|
2023-02-05 04:56:15 +00:00
|
|
|
(for*/list : (Listof (Pairof Symbol String))
|
|
|
|
([(section-key section) h]
|
|
|
|
[(key value) (in-hash section)])
|
|
|
|
(if (eq? section-key '||)
|
|
|
|
(cons key value)
|
|
|
|
(cons (string->symbol (string-append (symbol->string section-key)
|
|
|
|
"::"
|
|
|
|
(symbol->string key)))
|
|
|
|
value))))
|
|
|
|
(printf "note: ~a items loaded from config file~n" (length l))
|
|
|
|
l))
|
2022-09-08 02:05:47 +00:00
|
|
|
|
2022-09-17 10:51:42 +00:00
|
|
|
(define env-alist
|
2023-02-05 04:56:15 +00:00
|
|
|
(for/list : (Listof (Pairof Symbol String))
|
|
|
|
([name (environment-variables-names (current-environment-variables))]
|
|
|
|
#:when (string-prefix? (string-downcase (bytes->string/latin-1 name)) "bw_"))
|
|
|
|
(cons
|
|
|
|
;; key: convert to string, remove bw_ prefix, convert to symbol
|
|
|
|
(string->symbol (string-downcase (substring (bytes->string/latin-1 name) 3)))
|
|
|
|
;; value: convert to string
|
|
|
|
(bytes->string/latin-1
|
|
|
|
(cast (environment-variables-ref (current-environment-variables) name) Bytes)))))
|
2022-09-17 10:51:42 +00:00
|
|
|
(when (> (length env-alist) 0)
|
|
|
|
(printf "note: ~a items loaded from environment variables~n" (length env-alist)))
|
|
|
|
|
|
|
|
(define combined-alist (append default-config loaded-alist env-alist))
|
2022-09-08 02:05:47 +00:00
|
|
|
|
2022-08-23 09:57:42 +00:00
|
|
|
(define config
|
2023-02-05 04:56:15 +00:00
|
|
|
(for/hasheq ([pair combined-alist]) : (Immutable-HashTable Symbol (Parameter String))
|
|
|
|
(values (car pair) (make-parameter (cdr pair)))))
|
2022-08-24 08:59:11 +00:00
|
|
|
|
|
|
|
(when (config-true? 'debug)
|
2022-08-30 12:45:37 +00:00
|
|
|
; all values here are optimised for maximum prettiness
|
2022-08-30 10:13:26 +00:00
|
|
|
(parameterize ([pretty-print-columns 80])
|
|
|
|
(display "config: ")
|
2022-10-04 09:00:44 +00:00
|
|
|
(pretty-write ((inst sort (Pairof Symbol String))
|
|
|
|
(hash->list (make-immutable-hasheq combined-alist))
|
2022-09-08 02:05:47 +00:00
|
|
|
symbol<?
|
|
|
|
#:key car))))
|
2022-09-17 11:09:47 +00:00
|
|
|
|
|
|
|
(when (not (config-true? 'debug))
|
|
|
|
(when (not (config-true? 'canonical_origin))
|
|
|
|
(displayln
|
|
|
|
(string-append "warning: configuring canonical_origin is highly recommended for production!\n"
|
|
|
|
" see https://docs.breezewiki.com/Configuration.html"))))
|
2022-10-04 09:00:44 +00:00
|
|
|
|
|
|
|
(module+ test
|
|
|
|
; this is just a sanity check
|
|
|
|
(parameterize ([(config-parameter 'application_name) "JeffWiki"]
|
|
|
|
[(config-parameter 'strict_proxy) ""])
|
|
|
|
(check-equal? (config-get 'application_name) "JeffWiki")
|
2023-02-05 04:56:15 +00:00
|
|
|
(check-false (config-true? 'strict_proxy))
|
|
|
|
(check-equal? (string? (config-get 'feature_offline::format)) #t)))
|
|
|
|
|