qswiki/config.cpp

136 行
4.2 KiB
C++

/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "config.h"
#include "permissions.h"
#include <exception>
#include <iostream>
std::string Config::required(const std::string &key)
{
auto it = this->configmap.find(key);
if(it != this->configmap.end())
{
return it->second;
}
throw std::runtime_error("Required config key " + key + " not found");
}
std::string Config::optional(const std::string &key, std::string defaultvalue)
{
auto it = this->configmap.find(key);
if(it != this->configmap.end())
{
return it->second;
}
return defaultvalue;
}
int Config::optional(const std::string &key, int defaultvalue)
{
auto it = this->configmap.find(key);
if(it != this->configmap.end())
{
std::string str = it->second;
return std::stoi(str);
}
return defaultvalue;
}
uint64_t Config::optional(const std::string &key, uint64_t defaultvalue)
{
auto it = this->configmap.find(key);
if(it != this->configmap.end())
{
std::string str = it->second;
return static_cast<uint64_t>(std::stoull(str));
}
return defaultvalue;
}
Config::Config(const std::map<std::string, std::string> &map)
{
this->configmap = map;
this->wikipath = optional("wikipath", "/");
this->anon_username = optional("anon_username", "anonymouse");
this->wikiname = required("wikiname");
this->logfile = required("logfile");
this->templatepath = required("templatepath");
this->linkallcats = required("linkallcats");
this->linkallpages = required("linkallpages");
this->linkcategory = required("linkcategory");
this->linkdelete = required("linkdelete");
this->linkedit = required("linkedit");
this->linkhistory = required("linkhistory");
this->linkindex = required("linkindex");
this->linklogout = required("linklogout");
this->linkpage = required("linkpage");
this->linkrecent = required("linkrecent");
this->linkrevision = required("linkrevision");
this->linksettings = required("linksettings");
this->linkshere = required("linkshere");
this->loginurl = required("loginurl");
this->linkrecentsort = required("linkrecentsort");
this->linkhistorysort = required("linkhistorysort");
this->actionurl = required("actionurl");
this->settingsurl = required("settingsurl");
this->deletionurl = required("deletionurl");
this->adminregisterurl = required("adminregisterurl");
this->userchangepwurl = required("userchangepwurl");
this->connectionstring = required("connectionstring");
this->max_pagename_length = optional("max_pagename_length", 256);
this->session_max_lifetime = optional("session_max_lifetime", 3600);
this->query_limit = optional("query_limit", 200);
this->threadscount = optional("threadscount", 1);
this->anon_permissions = Permissions(required("anon_permissions"));
this->templateprefix = "{qswiki:";
this->max_payload_length = optional("max_payload_length", 10 * 1024 * 1024);
}
ConfigReader::ConfigReader(const std::string &file)
{
this->path = file;
}
Config ConfigReader::readConfig()
{
std::fstream f1(path, std::fstream::in);
std::string line;
std::map<std::string, std::string> configmap;
while(getline(f1, line))
{
if(isspace(line[0]) || line[0] == '#')
{
continue;
}
std::stringstream s(line);
std::string key;
std::string value;
s >> key >> value;
configmap.insert(std::make_pair(std::move(key), std::move(value)));
}
return Config(configmap);
}