changeset 54:85c59fbf1407

scid: add commands to retrieve API key
author David Demelier <markand@malikania.fr>
date Wed, 17 Aug 2022 11:07:08 +0200
parents 319979427566
children 38901547a76c
files man/scid.8 scid/main.c scid/scid.c scid/scid.h
diffstat 4 files changed, 133 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/man/scid.8	Wed Aug 17 09:52:00 2022 +0200
+++ b/man/scid.8	Wed Aug 17 11:07:08 2022 +0200
@@ -26,6 +26,10 @@
 .Op Fl f
 .Op Fl d Ar database-file
 .Op Fl t Ar theme-directory
+.Nm
+.Cm api-get
+.Nm
+.Cm api-reset
 .\" DESCRIPTION
 .Sh DESCRIPTION
 The
@@ -52,7 +56,7 @@
 Available options:
 .Bl -tag
 .It Fl f
-Runs as FastCGI process.
+Runs as FastCGI process (recommended).
 .It Fl d Ar database-file
 Use path specified
 .Pa database-file
@@ -62,6 +66,19 @@
 .Ar theme-directory
 as theme to use.
 .El
+.Pp
+Available commands:
+.Bl -tag
+.It Cm api-get
+Print the configured API key required for
+.Xr scictl 8
+and
+.Xr sciworkerd 8 .
+.It Cm api-reset
+Reset or create the API key if not already existing in database and print it.
+.El
+.Pp
+Otherwise, if no command is specified run either as CGI or FastCGI service.
 .\" SEE ALSO
 .Sh SEE ALSO
 .Xr sci 7 ,
--- a/scid/main.c	Wed Aug 17 09:52:00 2022 +0200
+++ b/scid/main.c	Wed Aug 17 11:07:08 2022 +0200
@@ -18,19 +18,59 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <time.h>
 #include <unistd.h>
 
+#include "db.h"
 #include "http.h"
+#include "log.h"
 #include "scid.h"
 #include "util.h"
 
 static void
 usage(void)
 {
-	fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir]\n");
+	fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir] [command]\n");
+	exit(1);
+}
+
+static void
+help(void)
+{
+	fprintf(stderr, "usage: scid\n");
+	fprintf(stderr, "       scid api-get\n");
+	fprintf(stderr, "       scid api-reset\n");
 	exit(1);
 }
 
+static void
+cmd_api_get(int argc, char **argv)
+{
+	char key[SCID_API_KEY_MAX] = {0};
+
+	if (db_key_get(key, sizeof (key)) == 1)
+		puts(key);
+}
+
+static void
+cmd_api_reset(int argc, char **argv)
+{
+	char key[SCID_API_KEY_MAX] = {0};
+
+	if (db_key_init(key, sizeof (key)) == 0)
+		printf("new key: %s\n", key);
+}
+
+static const struct {
+	const char *name;
+	void (*exec)(int, char **);
+} commands[] = {
+	{ "api-get",    cmd_api_get     },
+	{ "api-reset",  cmd_api_reset   },
+	{ NULL, NULL }
+};
+
 int
 main(int argc, char **argv)
 {
@@ -57,7 +97,30 @@
 	argc -= optind;
 	argv += optind;
 
-	scid_init();
-	run();
+	/* Always logs first. */
+	scid_init(SCID_INIT_LOG);
+	srand(time(NULL));
+
+	/* Check if there is a command provided, otherwise run as HTTP. */
+	if (argc == 0) {
+		scid_init(SCID_INIT_DATABASE | SCID_INIT_THEME);
+		log_info("scid: " VERSION);
+		run();
+	} else {
+		if (strcmp(argv[0], "help") == 0)
+			help();
+
+		for (size_t i = 0; commands[i].name; ++i) {
+			if (strcmp(commands[i].name, argv[0]) == 0) {
+				scid_init(SCID_INIT_DATABASE);
+				commands[i].exec(argc, argv);
+				goto over;
+			}
+		}
+
+		usage();
+	}
+
+over:
 	scid_finish();
 }
--- a/scid/scid.c	Wed Aug 17 09:52:00 2022 +0200
+++ b/scid/scid.c	Wed Aug 17 11:07:08 2022 +0200
@@ -28,13 +28,10 @@
 	.dbpath = VARDIR "/db/sci/sci.db"
 };
 
-static void
-init_misc(void)
+static inline void
+init_log(void)
 {
-	log_open("scid: version " VERSION);
-	log_info("scid: opening database %s", scid.dbpath);
-
-	theme_open(scid.themedir);
+	log_open("scid");
 }
 
 static void
@@ -60,16 +57,27 @@
 	}
 }
 
-void
-scid_init(void)
+static inline void
+init_theme(void)
 {
-	init_misc();
-	init_database();
+	theme_open(scid.themedir);
+}
+
+void
+scid_init(enum scid_init init)
+{
+	if (init & SCID_INIT_LOG)
+		init_log();
+	if (init & SCID_INIT_DATABASE)
+		init_database();
+	if (init & SCID_INIT_THEME)
+		init_theme();
 }
 
 void
 scid_finish(void)
 {
 	db_finish();
+	theme_finish();
 	log_finish();
 }
--- a/scid/scid.h	Wed Aug 17 09:52:00 2022 +0200
+++ b/scid/scid.h	Wed Aug 17 11:07:08 2022 +0200
@@ -41,10 +41,39 @@
 } scid /*! Global variable. */;
 
 /**
- * Initialize scid.
+ * \enum scid_init
+ * \brief Bitmask of scid components to initialize
+ */
+enum scid_init {
+	/**
+	 * Init logs.
+	 */
+	SCID_INIT_LOG           = (1 << 0),
+
+	/**
+	 * Open database read/write.
+	 */
+	SCID_INIT_DATABASE      = (1 << 1),
+
+	/**
+	 * Open the theme.
+	 */
+	SCID_INIT_THEME         = (1 << 2)
+};
+
+/**
+ * Initialize scid components.
+ *
+ * This function can be called multiple times as long as the component differ
+ * between each call.
+ *
+ * It's recommended to init logs first to get proper logging if the underlying
+ * component fails.
+ *
+ * \param components the components (as a bitmask)
  */
 void
-scid_init(void);
+scid_init(enum scid_init);
 
 /**
  * Cleanup scid.