122 rindas
3.3 KiB
C++
122 rindas
3.3 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 <iostream>
|
|
#include <fstream>
|
|
#include <exception>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include "gateway/gatewayinterface.h"
|
|
#include "gateway/gatewayfactory.h"
|
|
#include "handlers/handlerfactory.h"
|
|
#include "database/databasefactory.h"
|
|
#include "config.h"
|
|
#include "template.h"
|
|
#include "session.h"
|
|
#include "logger.h"
|
|
#include "urlprovider.h"
|
|
#include "requestworker.h"
|
|
#include "cache/fscache.h"
|
|
void sigterm_handler(int arg)
|
|
{
|
|
// TODO: proper shutdown.
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
void setup_signal_handlers()
|
|
{
|
|
struct sigaction sigtermaction;
|
|
sigtermaction.sa_handler = &sigterm_handler;
|
|
|
|
int ret = sigaction(SIGTERM, &sigtermaction, NULL);
|
|
if(ret == -1)
|
|
{
|
|
perror("sigaction");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
std::unique_ptr<ICache> createCache(const Config &config)
|
|
{
|
|
|
|
std::string path = config.getConfig("cache_fs_dir");
|
|
|
|
return std::make_unique<FsCache>(config.getConfig("cache_fs_dir"));
|
|
}
|
|
int main(int argc, char **argv)
|
|
{
|
|
if(geteuid() == 0)
|
|
{
|
|
std::cerr << "Do not run this as root!" << std::endl;
|
|
return 1;
|
|
}
|
|
if(argc < 2)
|
|
{
|
|
std::cerr << "no path to config file provided" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
try
|
|
{
|
|
ConfigReader configreader(argv[1]);
|
|
Config config = configreader.readConfig();
|
|
|
|
setup_signal_handlers();
|
|
|
|
std::fstream logstream;
|
|
logstream.open(config.logfile, std::fstream::out | std::fstream::app);
|
|
Logger::setStream(&logstream);
|
|
|
|
auto database = createDatabase(config);
|
|
|
|
// TODO: quite ugly, anon-handling must be rethought
|
|
auto userdao = database->createUserDao();
|
|
std::optional<User> anon = userdao->find(config.anon_username);
|
|
if(!anon)
|
|
{
|
|
throw std::runtime_error("No such anon user in database");
|
|
}
|
|
if(!anon->enabled)
|
|
{
|
|
throw std::runtime_error("Anon user cannot be enabled");
|
|
}
|
|
anon->permissions = config.anon_permissions;
|
|
userdao->save(anon.value());
|
|
User::setAnon(anon.value());
|
|
|
|
Template siteTemplate{config};
|
|
UrlProvider urlprovider{config};
|
|
|
|
auto cache = createCache(config);
|
|
cache->clear();
|
|
RequestWorker requestWorker(*database, siteTemplate, urlprovider, *cache);
|
|
|
|
auto interface = createGateway(config);
|
|
interface->work(requestWorker);
|
|
}
|
|
catch(const std::exception &e)
|
|
{
|
|
Logger::error() << e.what();
|
|
std::cerr << e.what() << std::endl;
|
|
}
|
|
return 0;
|
|
}
|