aboutsummaryrefslogtreecommitdiff
path: root/src/init.c
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2022-05-24 01:24:18 -0500
committerMichael McVady <femtonaut@gmail.com>2022-05-24 01:24:18 -0500
commit096d2bbf5267c6ffb9f8d6637587187be2af952f (patch)
tree68e97dbd320ef6c395655044d6ef16dee5aa15d5 /src/init.c
parentd05ab70231eccf7db88d69d6cd91eab4a5d54b7b (diff)
Standalone binary - starts
Diffstat (limited to 'src/init.c')
-rw-r--r--src/init.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/init.c b/src/init.c
new file mode 100644
index 0000000..3898a1c
--- /dev/null
+++ b/src/init.c
@@ -0,0 +1,90 @@
+#include <kore/kore.h>
+#include <kore/pgsql.h>
+
+int init_db(void);
+
+static const char *database = "db";
+
+/* Let kore handle the default option parsing. */
+void
+kore_parent_configure(int argc, char **argv)
+{
+ init_db();
+ kore_default_getopt(argc, argv);
+}
+
+int
+init_db(void)
+{
+ int err = 0;
+ const char *env = NULL;
+ struct kore_buf *pg_config_buf = kore_buf_alloc(0);
+
+ // Parse env vars and build PostgreSQL config string
+ env = getenv("POSTGRES_HOST");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "host=%s ", env);
+ }
+ else
+ {
+ const char *host = "host=localhost ";
+ kore_buf_append(pg_config_buf, host, strlen(host));
+ }
+
+ env = getenv("POSTGRES_PORT");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "port=%s ", env);
+ }
+ else
+ {
+ const char *port = "port=5432 ";
+ kore_buf_append(pg_config_buf, port, strlen(port));
+ }
+
+ env = getenv("POSTGRES_USER");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "user=%s ", env);
+ }
+ else
+ {
+ const char *user = "user=clog ";
+ kore_buf_append(pg_config_buf, user, strlen(user));
+ }
+
+ env = getenv("POSTGRES_PASSWORD");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "password=%s ", env);
+ }
+
+ env = getenv("POSTGRES_DBNAME");
+ if (env)
+ {
+ kore_buf_appendf(pg_config_buf, "user=%s ", env);
+ }
+ else
+ {
+ const char *dbname = "dbname=clog ";
+ kore_buf_append(pg_config_buf, dbname, strlen(dbname));
+ }
+
+ // FIXME: Should be configurable.
+ const char *sslmode = "sslmode=disable ";
+ kore_buf_append(pg_config_buf, sslmode, strlen(sslmode));
+
+ // Register the database
+ const char *pg_config = kore_buf_stringify(pg_config_buf, NULL);
+ kore_log(LOG_INFO, "%s", pg_config);
+ err = kore_pgsql_register(
+ database,
+ pg_config
+ );
+
+ kore_buf_free(pg_config_buf);
+
+ return err;
+}
+