acl-dev / acl

C/C++ server and network library, including coroutine,redis client,http/https/websocket,mqtt, mysql/postgresql/sqlite client with C/C++ for Linux, Android, iOS, MacOS, Windows, etc..

Home Page:https://acl-dev.cn

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

httpserver如何绑定path处理类?

venjin opened this issue · comments

如题,这个库的httpserver使用貌似与其他库不同,其他库大多是server.setRouting一个path绑定一个处理函数或类对象,请问这个库如何做到类似功能?

acl 中的 httpserver 目前的 url path 绑定方式类似于 Go 语言方式,在绑定函数里的调用方式类似于 Java,在例子 https://github.com/acl-dev/acl/blob/master/app/wizard_demo/ht/main.cpp 中的代码:

bool http_get_root(HttpRequest&, HttpResponse& res)
{
	acl::string buf("hello world!\r\n");
	res.setContentLength(buf.size());
	return res.write(buf);
}

service.Get("/", http_get_root)
		.Get("/test1", [](HttpRequest&, HttpResponse& res) {
			acl::string buf("test1: hello world!\r\n");
			res.setContentLength(buf.size());
			return res.write(buf);
		}).Get("/test2", [&](HttpRequest&, HttpResponse& res) {
			acl::string buf("test2: hello world!\r\n");
			res.setContentLength(buf.size());
			return res.write(buf);
		}).Default([](const char* path, HttpRequest&, HttpResponse& res) {
			acl::string buf;
			buf.format("Default(%s): hello world!\r\n", path);
			res.setContentLength(buf.size());
			return res.write(buf);
		});

还有一个简单的例子参考:https://github.com/acl-dev/demo/blob/master/c%2B%2B1x/httpd/fiber_httpd.cpp

好的,非常感谢