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