diff options
| author | Frederick Muriuki Muriithi | 2026-03-06 15:09:05 -0600 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-03-12 12:34:23 -0500 |
| commit | f0f0ba92544a150f08cf26c56e19988309d7135e (patch) | |
| tree | 971e645638718a9c0ebf4f898ae59c8d5db74b4f /web | |
| parent | c43f4317b59f3d2433817fa9e96ba35a14b78f99 (diff) | |
| download | gn-guile-f0f0ba92544a150f08cf26c56e19988309d7135e.tar.gz | |
Setup initial CLI configurations.
We want to move away from using global values for our configuration. In this respect, we provide a way to provide configurations at application startup via CLI arguments.
Diffstat (limited to 'web')
| -rw-r--r-- | web/config.scm | 86 | ||||
| -rw-r--r-- | web/webserver.scm | 7 |
2 files changed, 92 insertions, 1 deletions
diff --git a/web/config.scm b/web/config.scm new file mode 100644 index 0000000..9b3b9c2 --- /dev/null +++ b/web/config.scm @@ -0,0 +1,86 @@ +;;; Copyright © 2026 Frederick M Muriithi <fredmanglis@gmail.com> + +(define-module (web config) + #:use-module (srfi srfi-9 gnu) + + #:use-module (config) + #:use-module (config api) + #:use-module (config parser sexp) + + #:export (<gn-guile-config> + gn-guile-config-port + gn-guile-config-gn-docs-remote-url + gn-guile-config-gn-docs-local-checkout + gn-guile-config-gn-docs-working-branch + + parse-cli-options + cli-options->gn-guile-config)) + +(define-immutable-record-type <gn-guile-config> + (gn-guile-config port gn-docs-remote-url gn-docs-local-checkout + gn-docs-working-branch) + gn-guile-config? + (port gn-guile-config-port) + (gn-docs-remote-url gn-guile-config-gn-docs-remote-url) + (gn-docs-local-checkout gn-guile-config-gn-docs-local-checkout) + (gn-docs-working-branch gn-guile-config-gn-docs-working-branch)) + + +(define string->exact (compose inexact->exact string->number)) + + +(define (user-port? parsed) + (and (positive? parsed) (>= parsed 1024) (<= parsed 49151))) + + +(define (parse-cli-options cmd-line) + "Read configuration values from files and command-line options and convert them to appropriate data types." + (let ((config + (configuration (name 'gn-guile) + (synopsis "gn-guile web service: provide services + to main Genenetwork service.") + (description "gn-guile web service is a small +service, written in GNU Guile, that provides some functionality to the main +Genenetwork service in the background. This is not meant for direct user +interaction.") + (keywords + (list (switch (name 'write) + (default #f) + (test boolean?) + (character #f) + (synopsis "Write the settings to configuration file(s)") + (description "When this option is present, the configuration values, provided as command line option, will be written to the file path(s) that has/have been specified.")) + (setting (name 'port) + (default 8091) + (test user-port?) + (handler string->exact) + (character #\p) + (synopsis "Port number that the service will listen on")) + (setting (name 'gn-docs-remote-url) + (default "git@git.genenetwork.org:/home/git/public/gn-docs") + (test string?) + (character #\r) + (synopsis "Remote URI for gn-docs repository")) + (setting (name 'gn-docs-local-checkout) + (default (string-append (dirname (getcwd)) "/gn-guile-files/gn-docs")) + (test file-exists?) + (character #\c) + (synopsis "Path where gn-docs is checked out")) + (setting (name 'gn-docs-working-branch) + (default "non-existent") + (test string?) + (character #\b) + (synopsis "Branch to push/pull from")))) + (parser sexp-parser) + (directory (list (in-home ".config/gn-guile/") + (in-cwd ".config/")))))) + (getopt-config-auto cmd-line config))) + + +(define (cli-options->gn-guile-config cli-options) + "Extract specific values from guile-config's <codex> object into gn-guile's custom configuration object." + (gn-guile-config + (option-ref cli-options 'port) + (option-ref cli-options 'gn-docs-remote-url) + (option-ref cli-options 'gn-docs-local-checkout) + (option-ref cli-options 'gn-docs-working-branch))) diff --git a/web/webserver.scm b/web/webserver.scm index 4041f50..891f4f9 100644 --- a/web/webserver.scm +++ b/web/webserver.scm @@ -1,4 +1,5 @@ (use-modules (json) + (config) (ice-9 match) (ice-9 format) (ice-9 iconv) @@ -26,6 +27,7 @@ (gn data group) (gn runner gemma) (web sxml) + (web config) (web view view) (web view doc) (web view markdown)) @@ -397,6 +399,9 @@ otherwise search for set/group data" (write (string-append "Starting Guile REST API " get-version " server!")) (write args) (newline) - (let ((listen (inexact->exact (string->number (car (cdr args)))))) + (let* ((options (parse-cli-options args)) + (listen (option-ref options 'port))) + (when (option-ref options 'write) + (options-write options)) (display `("listening on" ,listen)) (start-web-server "127.0.0.1" listen))) |
