From 224eb644ee119d5915aef50d4b3b5aacce8ed0f4 Mon Sep 17 00:00:00 2001 From: lda Date: Sat, 2 Dec 2023 19:07:02 +0100 Subject: [PATCH] [ADD/WIP] Generate back a default configuration. --- Schema/Config.json | 6 ----- config.json | 1 + src/Config.c | 67 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 config.json diff --git a/Schema/Config.json b/Schema/Config.json index 976c7ef..9bdc593 100644 --- a/Schema/Config.json +++ b/Schema/Config.json @@ -12,14 +12,8 @@ "type": "struct" }, - "HttpHandler *": { "type": "extern" }, - "void *": { "type": "extern" }, - "ConfigListener": { "fields": { - "handler": { "type": "HttpHandler *", "ignore": true }, - "handlerArgs": { "type": "void *", "ignore": true }, - "port": { "type": "integer", "required": true }, "threads": { "type": "integer", "required": false }, "maxConnections": { "type": "integer", "required": false }, diff --git a/config.json b/config.json new file mode 100644 index 0000000..759e0a3 --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +{"log":{"output":"stdout"},"listen":[{"port":8008,"tls":{}}],"registration":false,"serverName":"localhost","baseUrl":"http:\/\/localhost:8008\/","federation":true} \ No newline at end of file diff --git a/src/Config.c b/src/Config.c index d699c97..210c5dc 100644 --- a/src/Config.c +++ b/src/Config.c @@ -28,14 +28,17 @@ #include #include #include -#include #include #include +#include +#include #include #include #include #include +#include +#include #ifndef HOST_NAME_MAX #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX @@ -45,7 +48,6 @@ Config * ConfigParse(HashMap * config) { Config *tConfig; - JsonValue *value; size_t i; @@ -74,6 +76,7 @@ ConfigParse(HashMap * config) tConfig->err = "Couldn't allocate enough memory for 'baseUrl'."; goto error; } + snprintf(tConfig->baseUrl, len, "https://%s/", tConfig->serverName); } if (!tConfig->log.timestampFormat) { @@ -113,11 +116,63 @@ ConfigExists(Db * db) int ConfigCreateDefault(Db * db) { - DbRef *ref; + Config config; + ConfigListener *listener; + HashMap *json; - Array *listeners; - HashMap *listen; - return 0; + + DbRef *ref; + + size_t len; + + memset(&config, 0, sizeof(Config)); + + + config.log.output = CONFIG_LOG_OUTPUT_FILE; + + config.runAs.gid = StrDuplicate(getgrgid(getgid())->gr_name); + config.runAs.uid = StrDuplicate(getpwuid(getuid())->pw_name); + + config.registration = 0; + config.federation = 1; + + gethostname(config.serverName, HOST_NAME_MAX); + len = strlen(config.serverName) + 10; + config.baseUrl = Malloc(len); + snprintf(config.baseUrl, len, "https://%s/", config.serverName); + + config.listen = ArrayCreate(); + listener = Malloc(sizeof(ConfigListener)); + listener->maxConnections = Int64Create(0, 0); + listener->port = Int64Create(0, 8008); + listener->threads = Int64Create(0, 0); + + listener->tls.key = NULL; + listener->tls.cert = NULL; + ArrayAdd(config.listen, listener); + + config.serverName = Malloc(HOST_NAME_MAX + 1); + + /* TODO: Don't set that field(it has to because otherwise j2s will add + * a NULL byte it seems.) */ + config.identityServer = StrDuplicate(""); + + + json = ConfigToJson(&config); + + ref = DbCreate(db, 1, "config"); + if (!ref) + { + ConfigFree(&config); + return 0; + } + DbJsonSet(ref, json); + DbUnlock(db, ref); + + ConfigFree(&config); + JsonFree(json); + + return 1; } Config *