aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: cfa08c62bc04ea8c512001252e8d7aa45535b641 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "ContentManager.hpp"
#include "content/blog/Blog.hpp"
#include "EventManager.hpp"
#include "Filesystem.hpp"

#include <event2/buffer.h>

#include <inja/inja.hpp>
#include <iostream>

static void example_inja_render(struct evhttp_request *const req,
                                EvUserData ud) {
  (void)ud;

  inja::Environment env;
  nlohmann::json data;
  data["name"] = "Peter";
  data["city"] = "Brunswick";
  data["age"] = 29;
  data["names"] = {"Jeff", "Seb"};
  data["brother"]["name"] = "Chris";
  data["brother"]["daughters"] = {"Maria", "Helen"};
  data["brother"]["daughter0"] = {{"name", "Maria"}};
  data["is_happy"] = true;
  data["is_sad"] = false;
  data["relatives"]["mother"] = "Maria";
  data["relatives"]["brother"] = "Chris";
  data["relatives"]["sister"] = "Jenny";
  data["vars"] = {2, 3, 4, 0, -1, -2, -3};

  auto reply = env.render(
      "<html><body>\n"
      "Hello {{ name }}! I come from {{ city }}.<br>\n"
      "Hello {{ names.1 }}!<br>\n"
      "Hello {{ brother.name }}!<br>\n"
      "Hello {{ brother.daughter0.name }}!<br>\n"
      "{{ \"{{ no_value }}\" }}<br>\n"
      "Hello{# This is a comment #}!<br>\n"
      "{# --- #Todo --- #}<br>\n"
      "{% for name in names %}a{% endfor %}<br>\n"
      "Hello {% for name in names %}{{ name }} {% endfor %}!<br>\n"
      "Hello {% for name in names %}{{ loop.index }}: {{ name }}, {% "
      "endfor %}!<br>\n"
      "{% for type, name in relatives %}{{ type }}: {{ name }}, {% endfor "
      "%}<br>\n"
      "{% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}<br>\n"
      "{% for name in names %}{{ loop.index }}: {{ name }}{% if not "
      "loop.is_last %}, {% endif %}{% endfor %}!<br>\n"
      "{% for name in names %}{{ loop.index }}: {{ name }}{% if "
      "loop.is_last == false %}, {% endif %}{% endfor %}!<br>\n"
      "{% for name in names %}a{% endfor %}<br>\n"
      "</body></html>\n",
      data);

  evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type",
                    "text/html");

  struct evbuffer *const output = evbuffer_new();
  if (output != nullptr) {
    evbuffer_add(output, reply.c_str(), reply.size());
    evhttp_send_reply(req, 200, "OK", output);
    evbuffer_free(output);
  }
}

int main(int argc, char **argv) {
  char const *host = "127.0.0.1";
  uint16_t port = 9000;

  if (argc <= 1) {
    std::cout << "usage: cpp-web [HOST] [PORT] [WWWROOTs..]" << std::endl;
  }

  if (argc > 1) {
    host = argv[1];
  }
  if (argc > 2) {
    port = atoi(argv[2]);
  }

  Filesystem fs;
  for (auto i = 3; i < argc; ++i) {
    if (fs.Scan(argv[i]) != true) {
      return 1;
    }
  }
  fs.Scan();

  ContentManager ctmgr;
  ctmgr.RegisterModule(std::make_shared<Blog>("/blog"));

  if (ctmgr.InitAll() == false)
  {
    std::cout << "InitAll() failed." << std::endl;
    return 1;
  }

  EventManager evmgr;
  //evmgr.SetDefaultCallback(example_inja_render, {});
  evmgr.AddCallback("/bla", example_inja_render, {});
  evmgr.AddContentManager(ctmgr);
  evmgr.Init(host, port);

  //ctmgr.ShutdownAll();
}