Hello World
One annotation registers the route; one function returns the response.
Create a controller
Put the following function in controller/src/myhello.cpp. The header controller/include/myhello.h is generated at CMake configure time. During build, paozhu_pre scans annotations and writes routes into common/autocontrolmethod.hpp.
#include "httppeer.h"
#include "myhello.h"
namespace http
{
//@urlpath(null,myhello)
asio::awaitable<std::string> myhello(std::shared_ptr<httppeer> peer)
{
peer->output = "Hello from myhello!";
co_return "";
}
}Rebuild, start the server and open http://127.0.0.1/myhello; the page shows Hello from myhello!. The repository example is controller/src/testhello.cpp, exposed at /hello.
Annotation syntax
//@urlpath(pre_filter, /path/:param):maskcode(1234):ratelimit(20,5):title("Page title")
// pre_filter -> function name, or null for none
// /path/:param -> route; :param must be trailing
// maskcode(n) -> RBAC mask, 0 means the auth hook is skipped
// ratelimit(n,s) -> per-route rate limit
// title(...) -> shown in the permission catalogue| Part | Meaning | Example |
|---|---|---|
null | No pre-filter | //@urlpath(null,hello) |
| Filter name | Runs first; @ok continues | admin_islogin |
:maskcode | RBAC mask; 0 skips the hook | :maskcode(64) |
:ratelimit | Per-route rate limit | :ratelimit(20,5) |
:title | Display name in permission catalogue | :title("Article list") |
Coroutine and sync handlers
Returning asio::awaitable<std::string> is recommended when you need co_await for async ORM or HTTP calls. A sync handler returning std::string also works — the stress-test route in the repo uses that form.
//@urlpath(null,hello/sync)
std::string hello_sync(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
client << "Hello from a sync handler";
return "";
}Read a path parameter
//@urlpath(null,user/info/:userid)
std::string user_info(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
client << "userid: " << client.get["userid"].to_string();
return "";
}For /user/info/42, client.get["userid"] is 42. Query-string parameters also come from client.get; POST fields come from client.post.
Four common outputs
| Goal | Code |
|---|---|
| Plain text / HTML | peer->output = "..."; or client << "..." |
| JSON | client.val["code"] = 0; client.out_json(); |
| Template view | peer->view("cms/list"); |
| Redirect | client.goto_url("/admin/main"); |