You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.7 KiB

/*
Copyright 2018 Denes Matetelki <denes@matetelki.com>
This file is part of webfish.
webfish is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
webfish is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License v3 for
more details.
You should have received a copy of the GNU General Public License v3 along
with webfish. If not, see
https://www.gnu.org/licenses/gpl-3.0.html.
*/
6 years ago
#include <iostream>
#include <cpp_utils/ArgParse.hpp>
#include <cpp_utils/TcpConnection.hpp>
#include <cpp_utils/Logger.hpp>
#include <cpp_utils/Common.hpp>
#include <cpp_utils/TcpConnection.hpp>
#include <cpp_utils/SocketServer.hpp>
#include "WebhookMessage.hpp"
#include <iostream>
#include <string>
#include <unistd.h> // sleep
#include <sys/stat.h>
6 years ago
int main(int argc, char* argv[])
{
ArgParse a("A simple HTTP server that bites on Gitea webhooks.",
"Homepage: http://matetelki.eu:3000/denes/webfish\n" \
"Author: Denes Matetelki <denes@matetelki.com>"
);
a.addArgument("-p", "Listenning port (default is 5050)",
ArgParse::ValueType::INT);
a.addArgument("-f", "File to execute on receiving a POST message",
ArgParse::ValueType::STRING,
ArgParse::Required::REQUIRED,
ArgParse::Required::REQUIRED);
/// @todo Add log verbosity as option
6 years ago
try {
a.parseArgs(argc, argv);
} catch(...) {
std::cerr << a.usage() << std::endl;
6 years ago
return EXIT_FAILURE;
}
if (a.foundArg("-h"))
std::cout << a.usage() << std::endl;
6 years ago
Logger::createInstance();
Logger::init(std::cout);
Logger::setLogLevel(Logger::DEBUG);
std::string script_path;
a.argAsString("-f", script_path);
// std::filesystem fpath(script_path);
struct stat st;
if (stat(script_path.c_str(), &st) < 0) {
LOG_STATIC( Logger::ERR, "Parameter file not found.");
Logger::destroy();
return EXIT_FAILURE;
}
if ((st.st_mode & S_IEXEC) == 0) {
LOG_STATIC( Logger::ERR, "Parameter file is not executable.");
Logger::destroy();
return EXIT_FAILURE;
}
WebhookMessage msg(reinterpret_cast<void*>(
const_cast<char*>(script_path.c_str())));
const std::string host("127.0.0.1");
std::string port;
if (a.foundArg("-p"))
a.argAsString("-p", port);
else
port = std::string("5050");
TcpConnection conn(host, port, &msg);
SocketServer socketServer(&conn);
if ( !socketServer.start() ) {
LOG_STATIC( Logger::ERR, "Failed to start TCP server, exiting...");
Logger::destroy();
return EXIT_FAILURE;
}
// never reached
sleep(1);
socketServer.stop();
Logger::destroy();
6 years ago
return EXIT_SUCCESS;
}