-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
94 lines (73 loc) · 2.9 KB
/
server.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
#include <iostream>
#include <string>
#include <vector>
#include <cpprest/http_listener.h>
#include <pqxx/pqxx>
#include "./src/Domain/DomainException.h"
#include "./src/Infrastructure/Persistence/SqlStudentRepository.h"
#include "./src/Infrastructure/Persistence/SqlSubjectRepository.h"
#include "./src/Infrastructure/HTTP/Router.h"
#include "./src/Infrastructure/HTTP/StudentHttpController.h"
#include "./src/Infrastructure/HTTP/SubjectHttpController.h"
using namespace std;
using namespace web::http;
using namespace web::json;
void handleRequest(http_request request)
{
string route = request.request_uri().path();
vector<string> path = uri::split_path(route);
string method = request.method();
cout << "[" << method << "] " << route << endl;
http_response response;
try {
string connectionString = getenv("DATABASE_URL");
pqxx::connection connection(connectionString.c_str());
SqlStudentRepository studentRepository(connection);
SqlSubjectRepository subjectRepository(connection);
StudentHttpController studentController(studentRepository, subjectRepository);
SubjectHttpController subjectController(subjectRepository);
response = Router::map(request, studentController, subjectController);
} catch (json_exception& e) {
value body = value::object();
body["error"] = value::object();
body["error"]["message"] = value::string("Invalid request body.");
response.set_body(body);
response.set_status_code(status_codes::BadRequest);
} catch(DomainException& e) {
value body = value::object();
body["error"] = value::object();
body["error"]["message"] = value::string(e.what());
response.set_body(body);
response.set_status_code(status_codes::BadRequest);
} catch(std::exception& e) {
cerr << e.what() << endl;
value body = value::object();
body["error"] = value::object();
body["error"]["message"] = value::string("Internal error.");
response.set_body(body);
response.set_status_code(status_codes::InternalError);
}
response.headers().add("Access-Control-Allow-Origin", "*");
response.headers().add("Access-Control-Allow-Headers", "*");
response.headers().add("Access-Control-Allow-Methods", "*");
request.reply(response);
}
int main(int argc, char const *argv[])
{
if (argc < 3) {
cerr << "Missing arguments. Usage: server <address> <port>" << endl;
return 1;
}
string address = string(argv[1]);
string port = string(argv[2]);
address.append(":");
address.append(port);
uri uri(address);
experimental::listener::http_listener httpListener(uri);
httpListener.support(methods::OPTIONS, handleRequest);
httpListener.support(handleRequest);
httpListener.open().wait();
cout << "Listening on " << address << endl;
while (true);
return 0;
}