113 linhas
3.0 KiB
C++
113 linhas
3.0 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);
|
|
|
|
User anon;
|
|
anon.login = config.anon_username;
|
|
anon.permissions = config.anon_permissions;
|
|
User::setAnon(anon);
|
|
|
|
auto database = createDatabase(config);
|
|
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;
|
|
|
|
}
|