Files
pip/main.cpp
2024-11-18 11:11:19 +03:00

73 lines
2.3 KiB
C++

#include "pip.h"
using namespace PICoutManipulators;
PIKbdListener kbd;
const char * pageTitle = "<!DOCTYPE html>"
"<html>"
"<body>"
"<h1>Title</h1>"
"</body>"
"</html>";
int main(int argc, char * argv[]) {
kbd.enableExitCapture();
PIHTTPServer server;
server.setFavicon(PIFile::readAll("logo.png", false));
// server.setOption(MicrohttpdServer::Option::HTTPSEnabled, true);
server.listen({"127.0.0.1", 7777});
// server.listen({"192.168.1.10", 7778});
server.registerPath("/", MicrohttpdServer::Method::Get, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody(PIByteArray::fromAscii(pageTitle));
return ret;
});
server.registerPath("/html", MicrohttpdServer::Method::Get, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody("<!DOCTYPE html><html><body><p>arg=%1</p></body></html>"_a.arg(r.args.value("a0")).toUTF8());
return ret;
});
server.registerPath("/api", MicrohttpdServer::Method::Put, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody(PIByteArray::fromAscii("<!DOCTYPE html><html><body>API</body></html>"));
return ret;
});
server.registerPath("/api/*", MicrohttpdServer::Method::Post, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody("<!DOCTYPE html><html><body>API etry %1</body></html>"_a.arg(r.path).toUTF8());
ret.setCode(405);
return ret;
});
server.registerUnhandled([](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody("<!DOCTYPE html><html><body>Unknown</body></html>"_a.arg(r.path).toUTF8());
ret.setCode(404);
return ret;
});
/*server.setRequestCallback([](MicrohttpdServer::Request r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply rep;
piCout << "request" << r.path;
piCout << " header" << r.headers;
piCout << " args" << r.args;
piCout << " body" << r.body;
piCout << "";
rep.setBody(PIByteArray::fromAscii("[{\"value1\": true, \"value2\": \"ыекштп\"}]"));
return rep;
});*/
piCout << "start" << server.isListen();
WAIT_FOR_EXIT
server.stop();
return 0;
}