Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Nicer configuration management.
  • Loading branch information
zkat committed Aug 3, 2011
1 parent bca3cd4 commit b14f90d
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions shortening.lisp
Expand Up @@ -8,25 +8,23 @@
(defparameter *max-db-size* 100)
(defparameter *url-length* 6)

(defparameter *config-locations* (list #p"/etc/shortening.conf"
(merge-pathnames ".shortening.conf" (user-homedir-pathname))))
(defun load-config ()
(when-let* ((config-path (probe-file (merge-pathnames ".shortening.conf" (user-homedir-pathname))))
(when-let* ((config-path (find-if #'probe-file *config-locations*))
(config (handler-case (read-files (make-config) (list config-path))
(configparser-error () nil))))
(flet ((conf (name)
(ignore-errors (parse-integer (get-option config "conf" name)))))
(setf *port* (or (conf "port") *port*)
*max-db-size* (or (conf "max-db-size") *max-db-size*)
*url-length* (or (conf "url-length") *url-length*)))
(configparser-error ()
(format *error-output*
"~&Error while loading config file ~A. Using defaults.~%"
config-path)))))
(flet ((conf (id &aux (symbol (intern (format nil "*~:(~A~)*" id)))
(name (string-upcase (string id))))
(setf (symbol-value symbol)
(or (ignore-errors (parse-integer (get-option config "shortening" name)))
(when (boundp symbol) (symbol-value symbol))))))
(mapcar #'conf '(port max-db-size url-length)))
t))

(defun ensure-config ()
(let ((path (merge-pathnames ".shortening.conf" (user-homedir-pathname))))
(unless (probe-file path)
(with-open-file (fd path :direction :output :if-exists :supersede)
(format fd "[default]~%port = ~A~%max-db-size = ~A~%url-length = ~A"
*port* *max-db-size* *url-length*))
t)))

;; Util
(defparameter *random-alphabet* "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
(defun random-string (&optional (length 6))
Expand Down Expand Up @@ -95,7 +93,6 @@

(defun init ()
(exit-on-error
(ensure-config)
(load-config)
(push (lambda (*request*)
(unless (string= (script-name*) "/api")
Expand Down

0 comments on commit b14f90d

Please sign in to comment.