[ADD/WIP] Generate back a default configuration.

This commit is contained in:
lda 2023-12-02 19:07:02 +01:00
parent f6313101fd
commit 224eb644ee
Signed by: lda
GPG key ID: 6898757653ABE3E6
3 changed files with 62 additions and 12 deletions

View file

@ -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 },

1
config.json Normal file
View file

@ -0,0 +1 @@
{"log":{"output":"stdout"},"listen":[{"port":8008,"tls":{}}],"registration":false,"serverName":"localhost","baseUrl":"http:\/\/localhost:8008\/","federation":true}

View file

@ -28,14 +28,17 @@
#include <Cytoplasm/Array.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Db.h>
#include <Cytoplasm/HttpServer.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Int64.h>
#include <Cytoplasm/Util.h>
#include <sys/types.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <grp.h>
#include <pwd.h>
#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;
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 *