-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAngularServer.cpp
More file actions
161 lines (145 loc) · 7.16 KB
/
Copy pathAngularServer.cpp
File metadata and controls
161 lines (145 loc) · 7.16 KB
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
#include "AngularServer.hpp"
#include "Version.h"
#include "Simple-Web-Server/server_http.hpp"
#include "Simple-Web-Server/status_code.hpp"
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/log/trivial.hpp>
#include <sstream>
#include "AngularResources.h"
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
using namespace boost::property_tree;
//------------------------------------- AngularServer
AngularServer::AngularServer(RouterClient &routerClient, const int port,const std::function<void()>& afterStart) : rc(routerClient){
server.config.port = port;
//out_header.emplace("Access-Control-Allow-Origin","*");
server.resource["^/ping"]["GET"] = servePingGet;
server.resource["^/api/(.+)"]["GET"] = std::bind(&AngularServer::serveApiGet, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/api/(.+)"]["PUT"] = std::bind(&AngularServer::serveApiGet, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/api/(.+)"]["POST"] = std::bind(&AngularServer::serveApiGet, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/api/(.+)"]["DELETE"] = std::bind(&AngularServer::serveApiGet, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/config"]["GET"] = std::bind(&AngularServer::serveConfigGet, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/config"]["PUT"] = std::bind(&AngularServer::serveConfigPut, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/config"]["OPTIONS"] = std::bind(&AngularServer::serveConfigPut, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/status"]["GET"] = std::bind(&AngularServer::serveStatusGet, this, std::placeholders::_1, std::placeholders::_2);
server.resource["^/stop"]["GET"] = std::bind(&AngularServer::serveStopGet, this, std::placeholders::_1, std::placeholders::_2);
server.default_resource["GET"] = std::bind(&AngularServer::serveResources, this, std::placeholders::_1, std::placeholders::_2);
thr=std::thread([this]() {
server.start();
});
std::this_thread::sleep_for(std::chrono::microseconds(100));
if(afterStart != NULL){
afterStart();
}
}
void AngularServer::setSingleInHeader(const std::string &key, const std::string &val){
auto it = out_header.find(key);
if(it != out_header.end()) it->second = val;
else out_header.emplace(key, val);
}
void AngularServer::stop(){
server.stop();
}
void AngularServer::serveResources(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> req ){
BOOST_LOG_TRIVIAL(info) << "sending resources [" << req->path << "] method " << req->method ;
std::string url = req->path.erase(0,1);
BOOST_LOG_TRIVIAL(info) << "url [" << url << "]";
if(url == "") url = "index.html";
if(url == "router") url = "index.html";
if(url == "console") url = "index.html";
if(url == "config") url = "index.html";
if(url == "signal") url = "index.html";
BOOST_LOG_TRIVIAL(info) << "url [" << url << "]";
auto found = ResourcesMap.find(url);
if(found != ResourcesMap.end()){
/*
if(endsWith(req->path, ".js")) out_header.emplace("Content-Type","application/javascript; charset=UTF-8");
if(endsWith(req->path, ".png")) out_header.emplace("Content-Type","image/png; charset=UTF-8");
if(endsWith(req->path, ".css")) out_header.emplace("Content-Type","text/css");
if(endsWith(url, ".html")) out_header.emplace("Content-Type","text/html");
*/
if(endsWith(req->path, ".js")) setSingleInHeader("Content-Type","application/javascript; charset=UTF-8");
if(endsWith(req->path, ".png")) setSingleInHeader("Content-Type","image/png; charset=UTF-8");
if(endsWith(req->path, ".css")) setSingleInHeader("Content-Type","text/css");
if(endsWith(url, ".html")) setSingleInHeader("Content-Type","text/html");
BOOST_LOG_TRIVIAL(info) << "found:" << req->path;
std::string mystr(found->second.second, found->second.second + found->second.first);
res->write(SimpleWeb::StatusCode::success_ok,mystr,out_header);
} else {
BOOST_LOG_TRIVIAL(info) << "not found:" << req->path;
res->write(SimpleWeb::StatusCode::success_ok,out_header);
}
}
void AngularServer::serveStatusGet(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> ){
BOOST_LOG_TRIVIAL(info) << "sending status ";
ptree tree;
tree.put("status.connected", rc.isConnected());
tree.put("status.loggedin", rc.isLoggedIn());
std::stringstream ss;
write_json(ss,tree);
setSingleInHeader("Content-Type","application/json; charset=UTF-8");
res->write(ss,out_header);
}
void AngularServer::serveConfigGet(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> ){
BOOST_LOG_TRIVIAL(info) << "sending config ";
ptree tree;
tree.put("config.server", rc.getServer());
tree.put("config.user", rc.getUserName());
tree.put("config.password", rc.getPassword());
std::ostringstream av, bv;
av << lte_monitor_VERSION_MAJOR << "." << lte_monitor_VERSION_MINOR;
bv << BOOST_VERSION / 100000 << "." << BOOST_VERSION / 100 % 1000 << "." << BOOST_VERSION % 100;
tree.put("config.appVersion", av.str());
tree.put("config.boostVersion", bv.str());
std::stringstream ss;
write_json(ss,tree);
setSingleInHeader("Content-Type","application/json; charset=UTF-8");
res->write(ss,out_header);
}
void AngularServer::serveConfigPut(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> req){
BOOST_LOG_TRIVIAL(info) << "setting new config";
if(req->method == "OPTIONS"){
SimpleWeb::CaseInsensitiveMultimap head;
head.emplace("Access-Control-Allow-Methods","POST, GET, OPTIONS, PUT");
head.emplace("Access-Control-Allow-Origin","*");
head.emplace("Access-Control-Allow-Headers","Content-Type, Authorization, X-Requested-With");
res->write("",head);
return;
}
ptree tree, pt;
read_json(req->content, pt);
rc.login(pt.get<std::string>("config.server"),pt.get<std::string>("config.user"),pt.get<std::string>("config.password"));
tree.put("config.server", rc.getServer());
tree.put("config.user", rc.getUserName());
tree.put("config.password", rc.getPassword());
std::stringstream ss;
write_json(ss,tree);
setSingleInHeader("Content-Type","application/json; charset=UTF-8");
res->write(ss,out_header);
}
void AngularServer::servePingGet(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> ){
BOOST_LOG_TRIVIAL(info) << "sending ping";
ptree pt;
pt.put("ping", "ok");
std::stringstream ss;
write_json(ss,pt);
res->write(ss);
}
void AngularServer::serveApiGet(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> req){
//std::string mypath=req->path_match[1].str();
BOOST_LOG_TRIVIAL(info) << "sending api " << req->path ;
std::string r = rc.Query(req->method, req->path, req->content.string());
setSingleInHeader("Content-Type","application/json; charset=UTF-8");
res->write(r,out_header);
}
void AngularServer::serveStopGet(std::shared_ptr<HttpServer::Response> res, std::shared_ptr<HttpServer::Request> ){
BOOST_LOG_TRIVIAL(info) << "stopping" ;
res->write("stopping");
std::this_thread::sleep_for(std::chrono::microseconds(1000));
stop();
}
AngularServer::~AngularServer(){
thr.join();
}