From 0d86568346d116f96abfe81e5b0ae05ba17b17c2 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 19 Sep 2022 21:13:40 +0200 Subject: [PATCH] Start of work. --- snac.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ snac.h | 10 ++++++++ xs.h | 4 +-- xs_json.h | 2 +- 4 files changed, 86 insertions(+), 3 deletions(-) diff --git a/snac.c b/snac.c index c28c607..1957271 100644 --- a/snac.c +++ b/snac.c @@ -1,3 +1,4 @@ +/* snac - A simple, minimalistic ActivityPub instance */ /* copyright (c) 2022 grunfink - MIT license */ #define XS_IMPLEMENTATION @@ -13,8 +14,80 @@ #include "snac.h" +d_char *srv_basedir = NULL; +d_char *srv_config = NULL; +d_char *srv_baseurl = NULL; + +int dbglevel = 0; + + +void srv_log(d_char *str) +/* logs a message */ +{ + char tm[16] = "00:00:00"; + xs *msg = str; + + fprintf(stderr, "%s %s\n", tm, msg); +} + + +int srv_open(char *basedir) +/* opens a server */ +{ + int ret = 0; + xs *cfg_file = NULL; + FILE *f; + + srv_basedir = xs_str_new(basedir); + + cfg_file = xs_fmt("%s/server.json", basedir); + + if ((f = fopen(cfg_file, "r")) == NULL) + srv_log(xs_fmt("cannot open %s", cfg_file)); + else { + xs *cfg_data; + + /* read full config file */ + cfg_data = xs_readall(f); + + /* parse */ + srv_config = xs_json_loads(cfg_data); + + if (srv_config == NULL) + srv_log(xs_fmt("cannot parse %s", cfg_file)); + else { + char *host; + char *prefix; + char *dbglvl; + + host = xs_dict_get(srv_config, "host"); + prefix = xs_dict_get(srv_config, "prefix"); + dbglvl = xs_dict_get(srv_config, "dbglevel"); + + if (host == NULL || prefix == NULL) + srv_log(xs_str_new("cannot get server data")); + else { + srv_baseurl = xs_fmt("https://%s%s", host, prefix); + + dbglevel = (int) xs_number_get(dbglvl); + + if ((dbglvl = getenv("DEBUG")) != NULL) { + dbglevel = atoi(dbglvl); + srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel)); + } + + ret = 1; + } + } + } + + return ret; +} + int main(int argc, char *argv[]) { + srv_open("/home/angel/lib/snac/comam.es"); + return 0; } diff --git a/snac.h b/snac.h index 2038c9d..609be40 100644 --- a/snac.h +++ b/snac.h @@ -1,2 +1,12 @@ +/* snac - A simple, minimalistic ActivityPub instance */ /* copyright (c) 2022 grunfink - MIT license */ +extern d_char *srv_basedir; +extern d_char *srv_config; +extern d_char *srv_baseurl; + +extern int dbglevel; + +void srv_log(d_char *str); +int srv_open(char *basedir); + diff --git a/xs.h b/xs.h index 40df89f..9195bc0 100644 --- a/xs.h +++ b/xs.h @@ -75,7 +75,7 @@ d_char *xs_dict_del(d_char *dict, const char *key); d_char *xs_dict_set(d_char *dict, const char *key, const char *data); d_char *xs_val_new(xstype t); d_char *xs_number_new(float f); -float xs_get_number(char *v); +float xs_number_get(char *v); extern int _xs_debug; @@ -697,7 +697,7 @@ d_char *xs_number_new(float f) } -float xs_get_number(char *v) +float xs_number_get(char *v) /* gets the number as a float */ { float f = 0.0; diff --git a/xs_json.h b/xs_json.h index 4bf73d5..f6eaa2f 100644 --- a/xs_json.h +++ b/xs_json.h @@ -93,7 +93,7 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent) case XSTYPE_NUMBER: { char tmp[32]; - snprintf(tmp, sizeof(tmp), "%g", xs_get_number(data)); + snprintf(tmp, sizeof(tmp), "%g", xs_number_get(data)); s = xs_str_cat(s, tmp); } break;