summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Dockerfile11
-rw-r--r--src/main.c24
-rw-r--r--src/web.c37
-rw-r--r--src/web.h2
4 files changed, 64 insertions, 10 deletions
diff --git a/Dockerfile b/Dockerfile
index 57cc35c..027b2a1 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,22 +1,19 @@
FROM alpine:3.23.2@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff6da4eb62 AS build
-RUN apk add --no-cache gcc make musl-dev sqlite-dev libmicrohttpd-dev cjson-dev
+RUN apk add --no-cache gcc make linux-headers musl-dev sqlite-dev libmicrohttpd-dev cjson-dev
COPY . /app
WORKDIR /app
RUN make app
-
FROM alpine:3.23.2@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff6da4eb62
-RUN apk add --no-cache libgcc sqlite-libs libmicrohttpd cjson \
- && mkdir /config
+RUN apk add --no-cache libgcc sqlite-libs libmicrohttpd cjson
COPY --from=build /app/app /app/app
-VOLUME /workdir
-VOLUME /data
-EXPOSE 9090
+VOLUME /var/lib/borgflag/
+EXPOSE 8080
CMD [ "/app/app", "8080" ]
diff --git a/src/main.c b/src/main.c
index 2cd5ceb..e5d872d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,15 @@
#include "web.h"
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+
+static volatile sig_atomic_t shutdown_requested = 0;
+
+static void signal_shutdown(int sig) {
+ shutdown_requested = 1;
+}
int main(int argc, char ** argv) {
if (argc != 2) {
@@ -17,15 +25,27 @@ int main(int argc, char ** argv) {
return 1;
}
+ const char *local = getenv("SQLITE_PATH");
+ if(local == NULL) {
+ local = "/var/lib/borgflag/data.db";
+ }
+
struct App app = {
- .dbname = "borgflag.db"
+ .dbname = local,
};
struct Server server;
server.app = &app;
+ signal(SIGTERM, signal_shutdown);
+ signal(SIGINT, signal_shutdown);
+
prepare_database(&app);
web_begin(&server, atoi(argv[1]), admin_user, admin_pass);
- getchar();
+
+ while (!shutdown_requested) {
+ pause();
+ }
+
web_join(&server);
}
diff --git a/src/web.c b/src/web.c
index c55278a..3967af9 100644
--- a/src/web.c
+++ b/src/web.c
@@ -2,6 +2,9 @@
#include "mytime.h"
#include <assert.h>
+#include <errno.h>
+#include <linux/limits.h>
+#include <libgen.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/stat.h>
@@ -60,6 +63,33 @@ static int is_authenticated(struct App *app, struct MHD_Connection *connection)
return strcmp(cookie, expected) == 0;
}
+// https://stackoverflow.com/questions/2336242/recursive-mkdir-system-call-on-unix
+static void _mkdir(const char *dir) {
+ char tmp[PATH_MAX];
+ char *p = NULL;
+ size_t len;
+ int rc;
+
+ snprintf(tmp, sizeof(tmp),"%s",dir);
+ len = strlen(tmp);
+ if (tmp[len - 1] == '/')
+ tmp[len - 1] = 0;
+ for (p = tmp + 1; *p; p++)
+ if (*p == '/') {
+ *p = 0;
+ rc = mkdir(tmp, S_IRWXU);
+ if(rc != 0 && errno != EEXIST) {
+ abort();
+ }
+ *p = '/';
+ }
+ rc = mkdir(tmp, S_IRWXU);
+ if(rc != 0 && errno != EEXIST) {
+ abort();
+ }
+
+}
+
void prepare_database(struct App *app) {
// @CLEANUP: Move this to main? doesn't belong here at least
sqlite3_config(SQLITE_CONFIG_LOG, sqliteError, NULL);
@@ -69,6 +99,13 @@ void prepare_database(struct App *app) {
printf("Running migrations against %s\n", app->dbname);
+ if(memcmp("file::memory", app->dbname, 12) != 0) {
+ char *full = strdup(app->dbname);
+ char *dbdir = dirname(full);
+ _mkdir(dbdir);
+ free(full);
+ }
+
sqlite3 *conn;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
fprintf(stderr, "(%d) %s\n", err, sqlite3_errmsg(conn));
diff --git a/src/web.h b/src/web.h
index 3a6e315..0e33018 100644
--- a/src/web.h
+++ b/src/web.h
@@ -9,7 +9,7 @@ struct MHD_PostProcessor;
#define CLIENT_SECRET_LEN 33
struct App {
- char* dbname;
+ const char* dbname;
char session_secret[CLIENT_SECRET_LEN];
const char *admin_user;
const char *admin_pass;