summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9a87d7c793fbd7546e8b588a3de7caa1b7285c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "app.h"
#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) {
		printf("%s PORT\n", argv[0]);
		return 1;
	}

	const char *admin_user = getenv("ADMIN_USER");
	const char *admin_pass = getenv("ADMIN_PASS");

	if (admin_user == NULL || admin_pass == NULL) {
		fprintf(stderr, "Error: ADMIN_USER and ADMIN_PASS environment variables must be set\n");
		return 1;
	}

	const char *local = getenv("SQLITE_PATH");
	if(local == NULL) {
		local = "/var/lib/borgflag/data.db";
	}

	struct App app = {
		.dbname = local,
	};
	struct Server server;

	server.app = &app;

	signal(SIGTERM, signal_shutdown);
	signal(SIGINT, signal_shutdown);

	app_startup(&app);
	web_begin(&server, atoi(argv[1]), admin_user, admin_pass);

	while (!shutdown_requested) {
		pause();
	}

	web_join(&server);
}