about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-12-26T10·05+0100
committerVincent Ambo <tazjin@gmail.com>2017-12-26T10·13+0100
commit4536e65471eb9d844eb811cfe643ea687a62dbae (patch)
tree8b39e9a834a43433b17738406436b00cdde7f1a8 /src
parentdb0e74fbd05d10675ae2dd64da9a285789595613 (diff)
feat(lisp): Implement configuration loading
Adds configuration loading from a file located at either
"/etc/gemma/config.lisp" or a path determined via the `GEMMA_CONFIG`
environment variable.

The configuration file can contain any number of deftask forms and a
single config form which determines the location at which Gemma stores
its data and also the port on which it should listen.
Diffstat (limited to 'src')
-rw-r--r--src/gemma.lisp51
1 files changed, 33 insertions, 18 deletions
diff --git a/src/gemma.lisp b/src/gemma.lisp
index ad096ec57080..2ead8f9ab2cf 100644
--- a/src/gemma.lisp
+++ b/src/gemma.lisp
@@ -10,16 +10,44 @@
 (defpackage gemma
   (:use :cl
         :local-time
-        :cl-json))
+        :cl-json)
+  (:import-from :sb-posix :getenv)
+  (:shadowing-import-from :sb-posix :getcwd))
 (in-package :gemma)
 
 ;; TODO: Store an average of how many days it was between task
 ;; completions. Some of the current numbers are just guesses
 ;; anyways.
 
+(defmacro in-case-of (x &body body)
+  "Evaluate BODY if X is non-nil, binding the value of X to IT."
+  `(let ((it ,x))
+     (when it ,@body)))
+
+;; Set default configuration parameters
+(defvar *gemma-port* 4242
+  "Port on which the Gemma web server listens.")
+
+(defun initialise-persistence (data-dir)
+  (defvar *p-tasks*
+    (cl-prevalence:make-prevalence-system data-dir)
+    "All tasks registered in this Gemma instance.")
+
+  ;; Initialise database ID counter
+  (or (> (length (cl-prevalence:find-all-objects *p-tasks* 'task)) 0)
+      (cl-prevalence:tx-create-id-counter *p-tasks*)))
+
+(defun config (&key port data-dir)
+  "Configuration function for use in the Gemma configuration file."
+
+  (in-package :gemma)
+  (in-case-of port (defparameter *gemma-port* it))
+  (initialise-persistence (or data-dir "data/")))
+
 ;;
 ;; Define task management system
 ;;
+
 (defclass task ()
   ((id :reader id
        :initarg :id)
@@ -44,23 +72,6 @@
             :initarg :done-at
             :accessor last-done-at)))
 
-(defvar *gemma-port*
-  (parse-integer (or (sb-posix:getenv "GEMMA_PORT") "4242"))
-  "Port on which the Gemma web server should listen.")
-
-(defvar *gemma-data-dir*
-  (pathname (or (sb-posix:getenv "GEMMA_DATA_DIR")
-                (sb-posix:getcwd)))
-  "Directory in which to store Gemma data.")
-
-(defvar *p-tasks*
-  (cl-prevalence:make-prevalence-system *gemma-data-dir*)
-  "All tasks registered in this Gemma instance.")
-
-;; Initialise database ID counter
-(or (> (length (cl-prevalence:find-all-objects *p-tasks* 'task)) 0)
-    (cl-prevalence:tx-create-id-counter *p-tasks*))
-
 (defmacro deftask (task-name days &optional description)
   (unless (get-task task-name)
     `(progn (cl-prevalence:tx-create-object
@@ -110,6 +121,10 @@ maximum interval."
     (:remaining . ,(days-remaining task))))
 
 (defun start-gemma ()
+  ;; Load configuration
+  (load (pathname (or (getenv "GEMMA_CONFIG")
+                      "/etc/gemma/config.lisp")))
+
   ;; Set up web server
   (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port *gemma-port*))