Skip to content

Commit

Permalink
Legacy config (#11)
Browse files Browse the repository at this point in the history
* legacy config

* wip

* try fix mac

* simple config

* fix

* fix git

* fix
  • Loading branch information
felixguendling authored Oct 10, 2024
1 parent bccc4b3 commit 4ebb3df
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ jobs:
run: ln -s /deps deps

- name: CMake
run: cmake -G Ninja -S . -B build --preset=${{ matrix.config.preset }}
run: |
git config --global --add safe.directory `pwd`
cmake -G Ninja -S . -B build --preset=${{ matrix.config.preset }}
# ==== BUILD ====
- name: Build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
fasta.json
config.yaml
config.yml
config.ini

5 changes: 3 additions & 2 deletions exe/import.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ int import(int ac, char** av) {

auto c = config{};
try {
c = config::read(config_path);
auto const silencer = utl::global_progress_bars{false};
c = config_path.extension() == ".ini" ? config::read_legacy(config_path)
: config::read(config_path);
auto const bars = utl::global_progress_bars{false};
import(c, std::move(data_path));
} catch (std::exception const& e) {
fmt::println("unable to import: {}", e.what());
Expand Down
24 changes: 19 additions & 5 deletions exe/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include <iostream>

#include "utl/progress_tracker.h"

#include "motis/config.h"
#include "motis/data.h"
#include "motis/import.h"

#if !defined(MOTIS_VERSION)
#define MOTIS_VERSION "unknown"
#endif
Expand All @@ -12,6 +18,7 @@ using namespace std::string_view_literals;
namespace motis {
int import(int, char**);
int server(int, char**);
int server(data d, config const& c);
} // namespace motis

using namespace motis;
Expand Down Expand Up @@ -56,11 +63,18 @@ int main(int ac, char** av) {
return server(ac, av);
} else if (cmd == "import") {
return import(ac, av);
}
} else {
auto const exit_code = import(ac, av);
if (exit_code == 0) {
server(ac, av);
} else {
try {
auto const bars = utl::global_progress_bars{false};
auto args = vm.count("subargs")
? vm.at("subargs").as<std::vector<std::string>>()
: std::vector<std::string>{};
args.insert(begin(args), cmd);
auto const c = config::read_simple(args);
server(import(c, "data"), c);
} catch (std::exception const& e) {
std::cerr << "error: " << e.what() << "\n";
}
}
}
}
49 changes: 28 additions & 21 deletions exe/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,7 @@ void POST(auto&& r, std::string target, From& from) {
}
}

int server(int ac, char** av) {
auto data_path = fs::path{"data"};

auto desc = bpo::options_description{"Options"};
desc.add_options() //
("help,h", "produce this help message") //
("data,d", bpo::value(&data_path)->default_value(data_path), "data path");

auto const pos_desc = bpo::positional_options_description{}.add("data", -1);

auto vm = bpo::variables_map{};
bpo::store(
bpo::command_line_parser(ac, av).options(desc).positional(pos_desc).run(),
vm);
bpo::notify(vm);

auto c = config::read(data_path / "config.yml");
auto d = data{std::move(data_path), c};

int server(data d, config const& c) {
auto ioc = asio::io_context{};
auto workers = asio::io_context{};
auto s = net::web_server{ioc};
Expand All @@ -91,11 +73,11 @@ int server(int ac, char** av) {
qr.route("GET", "/tiles/.*", ep::tiles{*d.tiles_});
}

qr.serve_files("ui/build");
auto const server_config = c.server_.value_or(config::server{});
qr.serve_files(server_config.web_folder_);
qr.enable_cors();
s.on_http_request(std::move(qr));

auto const server_config = c.server_.value_or(config::server{});
auto ec = boost::system::error_code{};
s.init(server_config.host_, server_config.port_, ec);
s.run();
Expand Down Expand Up @@ -137,4 +119,29 @@ int server(int ac, char** av) {
return 0;
}

int server(int ac, char** av) {
auto data_path = fs::path{"data"};

auto desc = bpo::options_description{"Options"};
desc.add_options() //
("help,h", "produce this help message") //
("data,d", bpo::value(&data_path)->default_value(data_path), "data path");

auto const pos_desc = bpo::positional_options_description{}.add("data", -1);

auto vm = bpo::variables_map{};
bpo::store(
bpo::command_line_parser(ac, av).options(desc).positional(pos_desc).run(),
vm);
bpo::notify(vm);

if (vm.count("help")) {
std::cout << desc << "\n";
return 0;
}

auto const c = config::read(data_path / "config.yml");
return server(data{std::move(data_path), c}, c);
}

} // namespace motis
3 changes: 3 additions & 0 deletions include/motis/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ using headers_t = std::map<std::string, std::string>;

struct config {
friend std::ostream& operator<<(std::ostream&, config const&);
static config read_simple(std::vector<std::string> const& args);
static config read_legacy(std::filesystem::path const&);
static config read(std::filesystem::path const&);
static config read(std::string const&);

Expand All @@ -32,6 +34,7 @@ struct config {
bool operator==(server const&) const = default;
std::string host_{"0.0.0.0"};
std::string port_{"8080"};
std::string web_folder_{"ui"};
unsigned n_threads_{std::thread::hardware_concurrency()};
};
std::optional<server> server_{};
Expand Down
Loading

0 comments on commit 4ebb3df

Please sign in to comment.