diff --git a/app/demo/etc/config.lisp b/app/demo/etc/config.lisp index 81e23cf..96320c4 100644 --- a/app/demo/etc/config.lisp +++ b/app/demo/etc/config.lisp @@ -9,11 +9,11 @@ :console nil) (config:add :server :class 'server:config - :port "8800" - :address "0.0.0.0" + :port (config:from-env :port "8800") + :address (config:from-env :address "0.0.0.0") :routes `((("hx") server:message-handler :html-responder cs-hx:response) (() server:fileserver :doc-root - ,(config:path "/var/www/html/" :env-key :docroot)))) + ,(util:path-from-string (config:from-env :docroot "/var/www/html/"))))) (config:add-action '(:test :data) #'core:echo) (config:add-action '(:auth :login) #'core:echo) diff --git a/config.lisp b/config.lisp index 000ff29..e88922b 100644 --- a/config.lisp +++ b/config.lisp @@ -6,8 +6,8 @@ (:use :common-lisp) (:local-nicknames (:util :scopes/util)) (:export #:base #:root #:*root* #:*current* - #:env-data #:env-keys #:env-prefix #:env-path #:from-env #:path - #:actions #:add #:add-action #:add-actions #:children #:env-slots + #:env-data #:env-keys #:env-prefix #:env-path #:from-env + #:actions #:add #:add-action #:add-actions #:children #:name #:setup #:parent #:shutdown)) (in-package :scopes/config) @@ -28,17 +28,10 @@ (defvar *current* nil) (defclass root (common) - ((env-keys :reader env-keys - :initarg :env-keys - :initform nil) - (env-prefix :reader env-prefix - :initarg :env-prefix - :initform "SCOPES_") - (env-path :reader env-path - :initarg :env-path - :initform nil) - (env-data :accessor env-data - :initform (make-hash-table)))) + ((env-keys :reader env-keys :initarg :env-keys :initform nil) + (env-prefix :reader env-prefix :initarg :env-prefix :initform "SCOPES_") + (env-path :reader env-path :initarg :env-path :initform nil) + (env-data :accessor env-data :initform (make-hash-table)))) (defmethod initialize-instance :after ((cfg root) &key &allow-other-keys) (let* ((data (env-data cfg)) @@ -61,8 +54,7 @@ ;;;; config base class (defclass base (common) - ((env-slots :reader env-slots :initform nil :allocation :class) - (name :reader name :initarg :name) + ((name :reader name :initarg :name) (parent :accessor parent :initarg :parent) (setup :reader setup :initarg :setup :initform #'(lambda (cfg))) (shutdown :reader shutdown :initarg :shutdown :initform #'(lambda (ctx))) @@ -70,8 +62,7 @@ (defmethod initialize-instance :after ((cfg base) &key parent &allow-other-keys) (if parent - (push cfg (children parent))) - (env-override cfg)) + (push cfg (children parent)))) (defmethod (setf parent) ((cfg base) (parent common)) (setf (parent cfg) parent) @@ -96,9 +87,6 @@ (defun from-env (key &optional (default "")) (or (gethash key (env-data *root*)) default)) -(defun path (s &key env-key) - (util:path-from-string (from-env env-key s))) - (defun hash-to-slots (ht obj slots) (if ht (dolist (sl slots) @@ -106,6 +94,3 @@ (val (gethash key ht))) (if val (setf (slot-value obj sl) val)))))) - -(defun env-override (cfg) - (hash-to-slots (env-data *root*) cfg (env-slots cfg))) diff --git a/test/.test.env b/test/.test.env index 1bd864f..cfd4a7e 100644 --- a/test/.test.env +++ b/test/.test.env @@ -1,4 +1,6 @@ # test/.test.env - environment file for overriding config settings SCOPES_USER=user-from-env-file -SCOPES_PASSWORD=secret-file +SCOPES_PASSWORD=secret-from-env-file + +SCOPES_DOCROOT=/var/www/html diff --git a/test/etc/config-dummy.lisp b/test/etc/config-dummy.lisp index 322a9e5..aa5d54e 100644 --- a/test/etc/config-dummy.lisp +++ b/test/etc/config-dummy.lisp @@ -5,9 +5,12 @@ (in-package :scopes/test-config) (config:root - :env-keys '(:user :password) + :env-keys '(:user :password :address :port) :env-path (t:test-path ".test.env")) (config:add :child :class 'child-config :user (config:from-env :user) - :password (config:from-env :password)) + :password (config:from-env :password) + :address (config:from-env :address) + :port (config:from-env :port "8199") + :docroot (config:from-env :docroot)) diff --git a/test/etc/config-web.lisp b/test/etc/config-web.lisp index a3f28d2..94a28f6 100644 --- a/test/etc/config-web.lisp +++ b/test/etc/config-web.lisp @@ -12,7 +12,7 @@ (config:add :server :class 'server:config - :port "8899" + :port (config:from-env :port "8899") :routes `((("hx") server:message-handler :html-responder cs-hx:response) (() server:fileserver diff --git a/test/test-config.lisp b/test/test-config.lisp index c0b8833..4795bab 100644 --- a/test/test-config.lisp +++ b/test/test-config.lisp @@ -14,7 +14,10 @@ (defclass child-config (config:base) ((user :accessor user :initarg :user) - (password :accessor password :initarg :password))) + (password :accessor password :initarg :password) + (address :accessor address :initarg :address) + (port :accessor port :initarg :port) + (docroot :accessor docroot :initarg :docroot))) (defun run () (let ((t:*test-suite* (t:test-suite "config"))) @@ -34,5 +37,10 @@ (child (car (config:children config:*root*)))) (== (gethash :user data) "user-from-env-file") (== (gethash :password data) "very_secret") + (== (gethash :address data) nil) + (== (gethash :docroot data) nil) (== (user child) "user-from-env-file") - (== (password child) "very_secret"))) + (== (password child) "very_secret") + (== (address child) "") + (== (port child) "8199") + (== (docroot child) ""))) diff --git a/web/server.lisp b/web/server.lisp index a5738d2..2318337 100644 --- a/web/server.lisp +++ b/web/server.lisp @@ -17,8 +17,7 @@ (in-package :scopes/web/server) (defclass config (config:base) - ((config:env-slots :initform '(address port)) - (config:setup :initform #'setup) + ((config:setup :initform #'setup) (config:shutdown :initform #'stop) (address :reader address :initarg :address :initform "localhost") (port :reader port :initarg :port :initform "8888")