From e3deae01133a951562f8495b6fd9910745c01f00 Mon Sep 17 00:00:00 2001 From: Jessie Hildebrandt Date: Tue, 21 Dec 2021 00:50:51 -0500 Subject: [PATCH] Add user-readable error for missing DB mount dir --- src/modules/fs_utils.fnl | 22 ++++++++++++++++++++++ src/modules/links_db.fnl | 15 +++++++++++++++ src/modules/sessions_db.fnl | 11 +++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/modules/fs_utils.fnl diff --git a/src/modules/fs_utils.fnl b/src/modules/fs_utils.fnl new file mode 100644 index 0000000..adb3535 --- /dev/null +++ b/src/modules/fs_utils.fnl @@ -0,0 +1,22 @@ +;; modules/fs_utils.fnl +;; Provides a collection of utilities for performing some filesystem operations + +;; -------------------------------------------------------------------------- ;; +;; Module definition + +(local fs_utils {}) + +(fn fs_utils.path_exists? [path] + "Return true if `path` exists and is accessible. +Note that this misuses os.rename in order to check `path`-- mightn't want to touch anything important with this..." + (let [(ok? _) (os.rename path path)] + ok?)) + +(fn fs_utils.dir_exists? [path] + "Return true if `path` is a directory and is accessible." + (fs_utils.path_exists? (.. path "/"))) + +;; -------------------------------------------------------------------------- ;; +;; Return module + +fs_utils diff --git a/src/modules/links_db.fnl b/src/modules/links_db.fnl index 1e84b06..cbdce1f 100644 --- a/src/modules/links_db.fnl +++ b/src/modules/links_db.fnl @@ -8,6 +8,11 @@ (local lume (require :lume)) (local sqlite (require :sqlite)) +;; -------------------------------------------------------------------------- ;; +;; Local modules + +(local fs_utils (require :modules.fs_utils)) + ;; -------------------------------------------------------------------------- ;; ;; Constants @@ -39,6 +44,16 @@ ORDER BY created DESC") ;; -------------------------------------------------------------------------- ;; ;; Initialize database +;; ---------------------------------- ;; +;; Ensure 'db/' folder exists + +(when (not (fs_utils.dir_exists? "db")) + (print "🚨 ERROR: No 'db/' folder to place SQLite database files in.\n❓ Did you forget to mount a volume at '/usr/simple-shortener/db?'") + (os.exit -1)) + +;; ---------------------------------- ;; +;; Connect to SQLite database + ;; Tables: ;; links | Stores information about shortened links ;; link_id - The unique base62 identifying string for the shortened link diff --git a/src/modules/sessions_db.fnl b/src/modules/sessions_db.fnl index 504a6fb..5068cb0 100644 --- a/src/modules/sessions_db.fnl +++ b/src/modules/sessions_db.fnl @@ -11,6 +11,7 @@ ;; Local modules (local config (require :modules.config)) +(local fs_utils (require :modules.fs_utils)) ;; -------------------------------------------------------------------------- ;; ;; Constants @@ -38,6 +39,16 @@ OR last_active <= strftime('%s', 'now', '-" config.session_max_idle_length_hours ;; -------------------------------------------------------------------------- ;; ;; Initialize database +;; ---------------------------------- ;; +;; Ensure 'db/' folder exists + +(when (not (fs_utils.dir_exists? "db")) + (print "🚨 ERROR: No 'db/' folder to place SQLite database files in.\n❓ Did you forget to mount a volume at '/usr/simple-shortener/db?'") + (os.exit -1)) + +;; ---------------------------------- ;; +;; Connect to SQLite database + ;; Tables: ;; sessions | Stores information about current (authorized) client sessions ;; session_id - The unique base62 identifying string for the client session