2024-11-01 19:05:20 +08:00
|
|
|
#include "Restful.h"
|
|
|
|
#include <Wt/Dbo/Impl.h>
|
|
|
|
#include <Wt/Dbo/Json.h>
|
|
|
|
#include <Wt/Dbo/backend/Sqlite3.h>
|
|
|
|
#include <Wt/Http/Response.h>
|
|
|
|
|
|
|
|
DBO_INSTANTIATE_TEMPLATES(MyMessage)
|
|
|
|
|
|
|
|
DbStruct *m_dbStruct;
|
|
|
|
|
2024-11-14 21:53:18 +08:00
|
|
|
void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
|
2024-11-01 19:05:20 +08:00
|
|
|
response.setMimeType("application/json");
|
|
|
|
response.addHeader("Server", "Wt");
|
|
|
|
|
|
|
|
MyMessage message;
|
|
|
|
message.message = "Hello, World!";
|
|
|
|
|
|
|
|
Wt::Dbo::JsonSerializer writer(response.out());
|
|
|
|
writer.serialize(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaintextResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
|
|
|
|
response.setMimeType("text/plain");
|
|
|
|
response.addHeader("Server", "Wt");
|
|
|
|
response.out() << "Hello, World!";
|
|
|
|
}
|
|
|
|
|
|
|
|
DbResource::DbResource(const std::string &db) {
|
|
|
|
if (!m_dbStruct) {
|
|
|
|
m_dbStruct = new DbStruct(db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
|
|
|
|
response.setMimeType("application/json");
|
|
|
|
response.addHeader("Server", "Wt");
|
|
|
|
|
|
|
|
Wt::Dbo::Transaction transaction(m_dbStruct->session);
|
|
|
|
Wt::Dbo::ptr<World> entry = m_dbStruct->session.load<World>(m_dbStruct->rand());
|
|
|
|
|
|
|
|
Wt::Dbo::JsonSerializer writer(response.out());
|
|
|
|
writer.serialize(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DbStruct::rand() {
|
|
|
|
return distribution(rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
DbStruct::DbStruct(const std::string &db) : rng(clock()), distribution(1, 10000) {
|
|
|
|
session.setConnection(std::make_unique<Wt::Dbo::backend::Sqlite3>(db));
|
|
|
|
session.mapClass<World>("world");
|
|
|
|
session.mapClass<Fortune>("fortune");
|
|
|
|
}
|