diff options
Diffstat (limited to 'snac.c')
| -rw-r--r-- | snac.c | 73 |
1 files changed, 73 insertions, 0 deletions
| @@ -1,3 +1,4 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 1 | /* copyright (c) 2022 grunfink - MIT license */ | 2 | /* copyright (c) 2022 grunfink - MIT license */ |
| 2 | 3 | ||
| 3 | #define XS_IMPLEMENTATION | 4 | #define XS_IMPLEMENTATION |
| @@ -13,8 +14,80 @@ | |||
| 13 | 14 | ||
| 14 | #include "snac.h" | 15 | #include "snac.h" |
| 15 | 16 | ||
| 17 | d_char *srv_basedir = NULL; | ||
| 18 | d_char *srv_config = NULL; | ||
| 19 | d_char *srv_baseurl = NULL; | ||
| 20 | |||
| 21 | int dbglevel = 0; | ||
| 22 | |||
| 23 | |||
| 24 | void srv_log(d_char *str) | ||
| 25 | /* logs a message */ | ||
| 26 | { | ||
| 27 | char tm[16] = "00:00:00"; | ||
| 28 | xs *msg = str; | ||
| 29 | |||
| 30 | fprintf(stderr, "%s %s\n", tm, msg); | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | int srv_open(char *basedir) | ||
| 35 | /* opens a server */ | ||
| 36 | { | ||
| 37 | int ret = 0; | ||
| 38 | xs *cfg_file = NULL; | ||
| 39 | FILE *f; | ||
| 40 | |||
| 41 | srv_basedir = xs_str_new(basedir); | ||
| 42 | |||
| 43 | cfg_file = xs_fmt("%s/server.json", basedir); | ||
| 44 | |||
| 45 | if ((f = fopen(cfg_file, "r")) == NULL) | ||
| 46 | srv_log(xs_fmt("cannot open %s", cfg_file)); | ||
| 47 | else { | ||
| 48 | xs *cfg_data; | ||
| 49 | |||
| 50 | /* read full config file */ | ||
| 51 | cfg_data = xs_readall(f); | ||
| 52 | |||
| 53 | /* parse */ | ||
| 54 | srv_config = xs_json_loads(cfg_data); | ||
| 55 | |||
| 56 | if (srv_config == NULL) | ||
| 57 | srv_log(xs_fmt("cannot parse %s", cfg_file)); | ||
| 58 | else { | ||
| 59 | char *host; | ||
| 60 | char *prefix; | ||
| 61 | char *dbglvl; | ||
| 62 | |||
| 63 | host = xs_dict_get(srv_config, "host"); | ||
| 64 | prefix = xs_dict_get(srv_config, "prefix"); | ||
| 65 | dbglvl = xs_dict_get(srv_config, "dbglevel"); | ||
| 66 | |||
| 67 | if (host == NULL || prefix == NULL) | ||
| 68 | srv_log(xs_str_new("cannot get server data")); | ||
| 69 | else { | ||
| 70 | srv_baseurl = xs_fmt("https://%s%s", host, prefix); | ||
| 71 | |||
| 72 | dbglevel = (int) xs_number_get(dbglvl); | ||
| 73 | |||
| 74 | if ((dbglvl = getenv("DEBUG")) != NULL) { | ||
| 75 | dbglevel = atoi(dbglvl); | ||
| 76 | srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel)); | ||
| 77 | } | ||
| 78 | |||
| 79 | ret = 1; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | return ret; | ||
| 85 | } | ||
| 86 | |||
| 16 | 87 | ||
| 17 | int main(int argc, char *argv[]) | 88 | int main(int argc, char *argv[]) |
| 18 | { | 89 | { |
| 90 | srv_open("/home/angel/lib/snac/comam.es"); | ||
| 91 | |||
| 19 | return 0; | 92 | return 0; |
| 20 | } | 93 | } |