复现已有算法

This commit is contained in:
cloud
2026-07-14 15:43:18 +08:00
parent abebd2a683
commit 50b8111fd9
860 changed files with 182250 additions and 18 deletions
@@ -0,0 +1,40 @@
#include <drogon/HttpController.h>
using namespace drogon;
// HttpControllers are automatically added to Drogon upon Drogon initializing.
class SayHello : public HttpController<SayHello>
{
public:
METHOD_LIST_BEGIN
// Drogon automatically appends the namespace and name of the controller to
// the handlers of the controller. In this example, although we are adding
// a handler to /. But because it is a part of the SayHello controller. It
// ends up in path /SayHello/ (IMPORTANT! It is /SayHello/ not /SayHello
// as they are different paths).
METHOD_ADD(SayHello::genericHello, "/", Get);
// Same for /hello. It ends up at /SayHello/hello
METHOD_ADD(SayHello::personalizedHello, "/hello", Get);
METHOD_LIST_END
protected:
void genericHello(const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback)
{
auto resp = HttpResponse::newHttpResponse();
resp->setBody(
"Hello, this is a generic hello message from the SayHello "
"controller");
callback(resp);
}
void personalizedHello(
const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback)
{
auto resp = HttpResponse::newHttpResponse();
resp->setBody(
"Hi there, this is another hello from the SayHello Controller");
callback(resp);
}
};
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<%c++
auto name=@@.get<std::string>("name");
bool nameIsEmpty = name == "";
if (nameIsEmpty)
name = "anonymous";
auto message = "Hello, " + name + " from a CSP template";
%>
<head>
<meta charset="UTF-8">
<title>[[ name ]]</title>
</head>
<body>
<%c++ $$<<message; %>
<%c++
if (nameIsEmpty)
{
$$ << "<br>"
<< "You can revisit the same page and append ?name=<i>your_name</i> to change the name";
}
%>
</body>
</html>
@@ -0,0 +1,29 @@
#include <drogon/HttpSimpleController.h>
#include <drogon/HttpResponse.h>
using namespace drogon;
// HttpSimpleController does not allow registration of multiple handlers.
// Instead, it has one handler - asyncHandleHttpRequest. The
// HttpSimpleController is a lightweight class designed to handle really simple
// cases.
class HelloViewController : public HttpSimpleController<HelloViewController>
{
public:
PATH_LIST_BEGIN
// Also unlike HttpContoller, HttpSimpleController does not automatically
// prepend the namespace and class name to the path. Thus the path of this
// controller is at "/view".
PATH_ADD("/view");
PATH_LIST_END
void asyncHandleHttpRequest(
const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) override
{
HttpViewData data;
data["name"] = req->getParameter("name");
auto resp = HttpResponse::newHttpViewResponse("HelloView", data);
callback(resp);
}
};
+97
View File
@@ -0,0 +1,97 @@
#include <trantor/utils/Logger.h>
#ifdef _WIN32
#include <ws2tcpip.h>
#else
#include <netinet/tcp.h>
#include <sys/socket.h>
#endif
#include <drogon/drogon.h>
using namespace drogon;
int main()
{
// `registerHandler()` adds a handler to the desired path. The handler is
// responsible for generating a HTTP response upon an HTTP request being
// sent to Drogon
app().registerHandler(
"/",
[](const HttpRequestPtr &request,
std::function<void(const HttpResponsePtr &)> &&callback) {
LOG_INFO << "connected:"
<< (request->connected() ? "true" : "false");
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello, World!");
callback(resp);
},
{Get});
// `registerHandler()` also supports parsing and passing the path as
// parameters to the handler. Parameters are specified using {}. The text
// inside the {} does not correspond to the index of parameter passed to the
// handler (nor it has any meaning). Instead, it is only to make it easier
// for users to recognize the function of each parameter.
app().registerHandler(
"/user/{user-name}",
[](const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &name) {
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello, " + name + "!");
callback(resp);
},
{Get});
// You can also specify that the parameter is in the query section of the
// URL!
app().registerHandler(
"/hello?user={user-name}",
[](const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &name) {
auto resp = HttpResponse::newHttpResponse();
resp->setBody("Hello, " + name + "!");
callback(resp);
},
{Get});
// Or, if you want to, instead of asking drogon to parse it for you. You can
// parse the request yourselves.
app().registerHandler(
"/hello_user",
[](const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = HttpResponse::newHttpResponse();
auto name = req->getOptionalParameter<std::string>("user");
if (!name)
resp->setBody("Please tell me your name");
else
resp->setBody("Hello, " + name.value() + "!");
callback(resp);
},
{Get});
app()
.setBeforeListenSockOptCallback([](int fd) {
LOG_INFO << "setBeforeListenSockOptCallback:" << fd;
#ifdef _WIN32
#elif __linux__
int enable = 1;
if (setsockopt(
fd, IPPROTO_TCP, TCP_FASTOPEN, &enable, sizeof(enable)) ==
-1)
{
LOG_INFO << "setsockopt TCP_FASTOPEN failed";
}
#else
#endif
})
.setAfterAcceptSockOptCallback([](int) {});
// Ask Drogon to listen on 127.0.0.1 port 8848. Drogon supports listening
// on multiple IP addresses by adding multiple listeners. For example, if
// you want the server also listen on 127.0.0.1 port 5555. Just add another
// line of addListener("127.0.0.1", 5555)
LOG_INFO << "Server running on 127.0.0.1:8848";
app().addListener("127.0.0.1", 8848).run();
}