This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathservice.cpp
192 lines (152 loc) · 6.16 KB
/
service.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sstream>
#include <os>
#include <rtc>
#include <acorn>
#include <profile>
using namespace std;
using namespace acorn;
using UserBucket = bucket::Bucket<User>;
using SquirrelBucket = bucket::Bucket<Squirrel>;
std::shared_ptr<UserBucket> users;
std::shared_ptr<SquirrelBucket> squirrels;
std::unique_ptr<mana::Server> server_;
std::unique_ptr<dashboard::Dashboard> dashboard_;
std::unique_ptr<Logger> logger_;
Disk_ptr disk;
#include <time.h>
// Get current date/time, format is [YYYY-MM-DD.HH:mm:ss]
const std::string timestamp() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "[%Y-%m-%d.%X] ", &tstruct);
return buf;
}
void Service::start(const std::string&) {
/** SETUP LOGGER */
char* buffer = (char*)malloc(1024*16);
static gsl::span<char> spanerino{buffer, 1024*16};
logger_ = std::make_unique<Logger>(spanerino);
logger_->flush();
logger_->log("LUL\n");
OS::add_stdout([] (const char* data, size_t len) {
// append timestamp
auto entry = timestamp() + std::string{data, len};
logger_->log(entry);
});
disk = fs::new_shared_memdisk();
// mount the main partition in the Master Boot Record
disk->mount([](fs::error_t err) {
if (err) panic("Could not mount filesystem, retreating...\n");
/** IP STACK SETUP **/
// Bring up IPv4 stack on network interface 0
auto& stack = net::Inet4::ifconfig(5.0,
[](bool timeout) {
printf("DHCP Resolution %s.\n", timeout?"failed":"succeeded");
});
// config
stack.network_config({ 10,0,0,42 }, // IP
{ 255,255,255,0 }, // Netmask
{ 10,0,0,1 }, // Gateway
{ 8,8,8,8 }); // DNS
// only works with synchronous disks (memdisk)
list_static_content(disk);
/** BUCKET SETUP */
// create squirrel bucket
squirrels = std::make_shared<SquirrelBucket>(10);
// set member name to be unique
squirrels->add_index<std::string>("name",
[](const Squirrel& s)->const auto&
{
return s.get_name();
}, SquirrelBucket::UNIQUE);
// seed squirrels
squirrels->spawn("Alfred"s, 1000U, "Wizard"s);
squirrels->spawn("Alf"s, 6U, "Script Kiddie"s);
squirrels->spawn("Andreas"s, 28U, "Code Monkey"s);
squirrels->spawn("AnnikaH"s, 20U, "Fairy"s);
squirrels->spawn("Ingve"s, 24U, "Integration Master"s);
squirrels->spawn("Martin"s, 16U, "Build Master"s);
squirrels->spawn("Rico"s, 28U, "Mad Scientist"s);
// setup users bucket
users = std::make_shared<UserBucket>();
users->spawn();
users->spawn();
/** ROUTES SETUP **/
using namespace mana;
Router router;
// setup Squirrel routes
router.use("/api/squirrels", routes::Squirrels{squirrels});
// setup User routes
router.use("/api/users", routes::Users{users});
// setup Language routes
router.use("/api/languages", routes::Languages{});
/** DASHBOARD SETUP **/
dashboard_ = std::make_unique<dashboard::Dashboard>(8192);
// Add singleton component
dashboard_->add(dashboard::Memmap::instance());
dashboard_->add(dashboard::StackSampler::instance());
dashboard_->add(dashboard::Status::instance());
// Construct component
dashboard_->construct<dashboard::Statman>(Statman::get());
dashboard_->construct<dashboard::TCP>(stack.tcp());
dashboard_->construct<dashboard::CPUsage>(0ms, 500ms);
dashboard_->construct<dashboard::Logger>(*logger_, static_cast<size_t>(50));
// Add Dashboard routes to "/api/dashboard"
router.use("/api/dashboard", dashboard_->router());
// Fallback route for angular application - serve index.html if route is not found
router.on_get("/app/.*", [](auto, auto res) {
#ifdef VERBOSE_WEBSERVER
printf("[@GET:/app/*] Fallback route - try to serve index.html\n");
#endif
disk->fs().cstat("/public/app/index.html", [res](auto err, const auto& entry) {
if(err) {
res->send_code(http::Not_Found);
} else {
// Serve index.html
#ifdef VERBOSE_WEBSERVER
printf("[@GET:/app/*] (Fallback) Responding with index.html. \n");
#endif
res->send_file({disk, entry});
}
});
});
INFO("Router", "Registered routes:\n%s", router.to_string().c_str());
/** SERVER SETUP **/
// initialize server
server_ = std::make_unique<Server>(stack);
// set routes and start listening
server_->set_routes(router).listen(80);
/** MIDDLEWARE SETUP **/
// custom middleware to serve static files
auto opt = {"index.html"};
Middleware_ptr butler = std::make_shared<butler::Butler>(disk, "/public", opt);
server_->use(butler);
// custom middleware to serve a webpage for a directory
Middleware_ptr director = std::make_shared<director::Director>(disk, "/public/static");
server_->use("/static", director);
Middleware_ptr parsley = std::make_shared<json::Parsley>();
server_->use(parsley);
Middleware_ptr cookie_parser = std::make_shared<cookie::CookieParser>();
server_->use(cookie_parser);
}); // < disk
}