-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget.cpp
94 lines (86 loc) · 2.59 KB
/
get.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 <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
#include <fastcgi++/http.hpp>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include "connector.hpp"
void error_log(const char* msg)
{
using namespace std;
using namespace boost;
static ofstream error;
if(!error.is_open())
{
error.open("/tmp/errlog", ios_base::out | ios_base::app);
error.imbue(locale(error.getloc(), new posix_time::time_facet()));
}
error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
}
class GetBook : public Connector
{
inline void sendError(const std::string& errorMsg)
{
out << "{ \"success\" : 0, \"message\" : \"" + errorMsg + "\" }" << std::endl;
}
bool response()
{
out << "Content-Type: application/json; charset=ISO-8859-1\r\n\r\n";
std::map<std::string, std::string> parameters;
for (Fastcgipp::Http::Environment<char>::Gets::const_iterator it = environment().gets.begin(); it != environment().gets.end(); ++it)
{
parameters[it->first] = it->second;
}
if (parameters.find("id") == parameters.end())
{
sendError("Missing id");
}
else
{
sql::Statement* stmt = con->createStatement();
try
{
sql::ResultSet* res = stmt->executeQuery("SELECT name, publisher, UNIX_TIMESTAMP(date) as date, edition FROM book WHERE id = " + parameters["id"]);
if (!res->next())
{
sendError("Could not found book with id = " + parameters["id"]);
}
else
{
std::string result = "{ \"success\" : 1, ";
result += "\"name\": \"" + res->getString("name") + "\",";
result += "\"publisher\": \"" + res->getString("publisher") + "\",";
result += "\"date\": " + res->getString("date") + ",";
result += "\"edition\": " + res->getString("edition");
result += "}";
delete res;
out << result << std::endl;
}
} catch (sql::SQLException& e)
{
sendError(e.what());
}
delete stmt;
}
return true;
}
};
int main()
{
try
{
Fastcgipp::Manager<GetBook> fcgi;
fcgi.handler();
}
catch (std::exception& e)
{
error_log(e.what());
}
return 0;
}