From 096d2bbf5267c6ffb9f8d6637587187be2af952f Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Tue, 24 May 2022 01:24:18 -0500 Subject: Standalone binary - starts --- conf/build.conf | 6 ++-- conf/clog.conf | 2 +- src/clog.c | 77 ------------------------------------------------ src/init.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 src/init.c diff --git a/conf/build.conf b/conf/build.conf index 32bbf21..57d6329 100644 --- a/conf/build.conf +++ b/conf/build.conf @@ -4,9 +4,9 @@ # Set to yes if you wish to produce a single binary instead # of a dynamic library. If you set this to yes you must also # set kore_source together with kore_flavor. -# single_binary=yes -# kore_source=../kore-4.2.2/ -# kore_flavor=PGSQL=1 TLS_BACKEND=none +single_binary=yes +kore_source=../kore-4.2.2/ +kore_flavor=PGSQL=1 TLS_BACKEND=none # The flags below are shared between flavors cflags=-Wall -Wmissing-declarations -Wshadow diff --git a/conf/clog.conf b/conf/clog.conf index ef50a78..23ad8d0 100644 --- a/conf/clog.conf +++ b/conf/clog.conf @@ -5,7 +5,7 @@ server notls { tls no } -load ./clog.so init +# load ./clog.so init workers 4 diff --git a/src/clog.c b/src/clog.c index c4f44e3..10a453c 100644 --- a/src/clog.c +++ b/src/clog.c @@ -19,7 +19,6 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) -int init(int); int post(struct http_request *); int posts(struct http_request *); int render_posts_html(struct http_request *, const char *); @@ -29,82 +28,6 @@ static int render_md(char *, struct kore_buf *); static const char *database = "db"; -// Called when this module is loaded (see config) -int -init(int state) -{ - 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; -} - int post(struct http_request *req) { 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 +#include + +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; +} + -- cgit v1.2.3