summaryrefslogtreecommitdiffhomepage
path: root/webserver.cpp
blob: bfa3b8ca350af97f9dabb26b9f3fb3f3e3a35806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "auth.h"
#include "config.h"
#include "server.h"
#include "plugin.h"

#include <filesystem>
#include <exception>
#include <iostream>
#include <string>

using namespace std::string_literals;

void usage()
{
 std::cout << "usage: webserver [-c <configuration-filename> | -p <password> ] " << std::endl;
 std::cout << "       webserver [-c <configuration-filename>]             - start server with specified configuration\n"
              "                                                             file or default /etc/webserver.conf" << std::endl;
 std::cout << "       webserver -p <password>                             - encrypt password for basic HTTP AUTH,\n"
              "                                                             suitable for config file, and exit" << std::endl;
}

void initlocale() {
 if (setenv("LC_ALL", "UTF-8", 1)) {
  exit(1);
 }
}

int webserver(int argc, char* argv[])
{
 try {
  //initlocale(); // TODO: breaks plugins

  std::filesystem::path config_filename;

  if (!(argc == 1 || argc == 3)) {
   usage();
   return 1;
  }

  if (argc == 3) {

   // normal run with configuration file specified
   if (argv[1] == "-c"s) {
    config_filename = argv[2];
   } else if (argv[1] == "-p"s) { // generate crypted password
    std::cout << Auth::generate(argv[2]) << std::endl;
    return 0;
   } else {
    usage();
    return 1;
   }
  }

  Config config{config_filename};
  
  PluginLoader plugin_loader(config);
  plugin_loader.load_plugins();

  if (!plugin_loader.validate_config())
   throw std::runtime_error("Config validation error.");

  return run_server(config, plugin_loader.get_plugins());
 } catch (const std::exception& ex) {
  std::cout << "Error: " << ex.what() << std::endl;
  return 1;
 }
}