src/relpipe-tr-httpd.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 09 Apr 2022 17:50:46 +0200
branchv_0
changeset 4 37a86904145c
parent 2 4b05b16b97e6
permissions -rw-r--r--
partial implementation of response and header templates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <cstdio>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <cstdlib>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/Factory.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/RelationalReader.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/RelpipeReaderException.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/RelationalWriter.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/RelpipeWriterException.h>
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/writer/Factory.h>
4
37a86904145c partial implementation of response and header templates
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    32
#include <unistd.h>
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "HTTPDHandler.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "CLIParser.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "Configuration.h"
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::cli;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::reader;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::writer;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
using namespace relpipe::tr::httpd;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
int main(int argc, char**argv) {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	setlocale(LC_ALL, "");
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	CLI::untieStdIO();
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	CLI cli(argc, argv);
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	try {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		CLIParser cliParser;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		Configuration configuration = cliParser.parse(cli.arguments());
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		std::shared_ptr<RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
2
4b05b16b97e6 configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
		HTTPServer::Options serverOptions;
4b05b16b97e6 configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
		serverOptions.tcpPort = configuration.tcpPort;
4b05b16b97e6 configurable TCP listener port + RequestHandler interface
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
		std::shared_ptr<HTTPServer> httpServer(HTTPServer::create(serverOptions));
1
23c516259cc5 HTTP server demo
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
		HttpdHandler handler(writer, configuration, httpServer);
0
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		reader->addHandler(&handler);
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		reader->process();
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		resultCode = CLI::EXIT_CODE_SUCCESS;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	} catch (RelpipeCLIException& e) {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		resultCode = e.getExitCode();
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	} catch (RelpipeReaderException& e) {
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str());
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	return resultCode;
7b70918c30af establish new project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
}