forked from cadence/breezewiki
		
	Use ini for configuration
This commit is contained in:
		
							parent
							
								
									73a5cb68c5
								
							
						
					
					
						commit
						b18a31df4a
					
				
					 5 changed files with 41 additions and 26 deletions
				
			
		
							
								
								
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -12,4 +12,4 @@ | |||
| compiled | ||||
| 
 | ||||
| # Personal | ||||
| /config.txt | ||||
| /config.ini | ||||
|  |  | |||
|  | @ -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)))) | ||||
|  |  | |||
							
								
								
									
										2
									
								
								info.rkt
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								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")) | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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)) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue