2019-04-23 02:24:01 +02:00
|
|
|
/*
|
|
|
|
* snake_run.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <fstream>
|
2019-09-24 06:04:01 +02:00
|
|
|
#include <ixsnake/IXSnakeServer.h>
|
2019-12-25 06:55:34 +01:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-04-23 02:24:01 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> load(const std::string& path)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> memblock;
|
|
|
|
|
|
|
|
std::ifstream file(path);
|
|
|
|
if (!file.is_open()) return memblock;
|
|
|
|
|
|
|
|
file.seekg(0, file.end);
|
|
|
|
std::streamoff size = file.tellg();
|
|
|
|
file.seekg(0, file.beg);
|
|
|
|
|
2019-06-03 05:46:20 +02:00
|
|
|
memblock.resize((size_t) size);
|
2019-09-23 19:25:23 +02:00
|
|
|
file.read((char*) &memblock.front(), static_cast<std::streamsize>(size));
|
2019-04-23 02:24:01 +02:00
|
|
|
|
|
|
|
return memblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string readAsString(const std::string& path)
|
|
|
|
{
|
|
|
|
auto vec = load(path);
|
|
|
|
return std::string(vec.begin(), vec.end());
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace
|
2019-04-23 02:24:01 +02:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
int ws_snake_main(int port,
|
|
|
|
const std::string& hostname,
|
|
|
|
const std::string& redisHosts,
|
|
|
|
int redisPort,
|
|
|
|
const std::string& redisPassword,
|
|
|
|
bool verbose,
|
2019-12-20 05:49:28 +01:00
|
|
|
const std::string& appsConfigPath,
|
2020-02-01 01:55:54 +01:00
|
|
|
const SocketTLSOptions& socketTLSOptions,
|
|
|
|
bool disablePong)
|
2019-04-23 02:24:01 +02:00
|
|
|
{
|
|
|
|
snake::AppConfig appConfig;
|
|
|
|
appConfig.port = port;
|
|
|
|
appConfig.hostname = hostname;
|
|
|
|
appConfig.verbose = verbose;
|
|
|
|
appConfig.redisPort = redisPort;
|
|
|
|
appConfig.redisPassword = redisPassword;
|
2019-12-20 05:49:28 +01:00
|
|
|
appConfig.socketTLSOptions = socketTLSOptions;
|
2020-02-01 01:55:54 +01:00
|
|
|
appConfig.disablePong = disablePong;
|
2019-04-23 02:24:01 +02:00
|
|
|
|
|
|
|
// Parse config file
|
|
|
|
auto str = readAsString(appsConfigPath);
|
|
|
|
if (str.empty())
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::error("Cannot read content of {}", appsConfigPath);
|
2019-04-23 02:24:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::error(str);
|
2019-04-23 02:24:01 +02:00
|
|
|
auto apps = nlohmann::json::parse(str);
|
|
|
|
appConfig.apps = apps["apps"];
|
|
|
|
|
|
|
|
std::string token;
|
|
|
|
std::stringstream tokenStream(redisHosts);
|
|
|
|
while (std::getline(tokenStream, token, ';'))
|
|
|
|
{
|
|
|
|
appConfig.redisHosts.push_back(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display config on the terminal for debugging
|
|
|
|
dumpConfig(appConfig);
|
|
|
|
|
|
|
|
snake::SnakeServer snakeServer(appConfig);
|
2019-09-06 05:27:01 +02:00
|
|
|
snakeServer.runForever();
|
|
|
|
|
|
|
|
return 0; // should never reach this
|
2019-04-23 02:24:01 +02:00
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|