Paozhu logoPaozhu Docs
Documentation
DEV

Controller guide

Annotation routing, parameters, sessions/cookies, responses and pre-filters.

Minimal controller

Controllers live in controller/src/ and functions belong to namespace http. Headers are generated by the build; do not write them by hand.

controller/src/myhello.cppcpp
#include "httppeer.h"
#include "myhello.h"

namespace http
{
//@urlpath(null,myhello)
asio::awaitable<std::string> myhello(std::shared_ptr<httppeer> peer)
{
    httppeer &client = peer->get_peer();
    std::string name = client.get["name"].as_string("world");
    client.output = "Hello, " + name;
    co_return "";
}
}

Parameters and defaults

Query and POST fields come from client.get and client.post. to_string() and to_int() take no arguments; use as_string(default) / as_int(default) for defaults.

Sessions and cookies

controller/src/session_demo.cppcpp
client.session["adminid"] = 10001;
client.save_session();
std::string sid = client.get_session_id();

client.set_cookie("token", "abc", 7200, "/", client.host, true, true, "Lax");
int uid = client.session["adminid"].to_int();
client.clear_session();

Call save_session() explicitly after changing the session. The full cookie signature is set_cookie(name, value, expire, path, domain, httpOnly, secure, sameSite).

Pre-filters

controller/src/admin_panel.cppcpp
//@urlpath(admin_islogin,admin/panel)
std::string admin_panel(std::shared_ptr<httppeer> peer)
{
    httppeer &client = peer->get_peer();
    client << "Admin panel";
    return "";
}

std::string admin_islogin(std::shared_ptr<httppeer> peer)
{
    httppeer &client = peer->get_peer();
    if (client.session["adminid"].to_int() == 0) return "admin/login";
    return "@ok";   // not "ok"
}