本文整理汇总了C++中Router::on_get方法的典型用法代码示例。如果您正苦于以下问题:C++ Router::on_get方法的具体用法?C++ Router::on_get怎么用?C++ Router::on_get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Router
的用法示例。
在下文中一共展示了Router::on_get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: start
void Service::start(const std::string&)
{
/** IP STACK SETUP **/
// Bring up IPv4 stack on network interface 0
auto& stack = net::Inet4::ifconfig(5.0,
[] (bool timeout) {
printf("DHCP resolution %s\n", timeout ? "failed" : "succeeded");
if (timeout)
{
/**
* Default Manual config. Can only be done after timeout to work
* with DHCP offers going to unicast IP (e.g. in GCE)
**/
net::Inet4::stack().network_config({ 10,0,0,42 }, // IP
{ 255,255,255,0 }, // Netmask
{ 10,0,0,1 }, // Gateway
{ 8,8,8,8 }); // DNS
}
});
// Create a router
Router router;
// Setup a route on GET /
router.on_get("/plaintext", [](auto, auto res) {
res->source().set_status_code(http::OK);
res->header().add_field(http::header::Server, "IncludeOS/" + OS::version());
res->header().add_field(http::header::Content_Type, "text/plain");
res->header().add_field(http::header::Date, "Mon, 01 Jan 1970 00:00:01 GMT");
res->source().add_body("Hello, world!"s);
res->send();
});
INFO("Router", "Registered routes:\n%s", router.to_string().c_str());
// Create and setup the server
server_ = std::make_unique<Server>(stack.tcp());
server_->set_routes(router).listen(80);
}
示例2: start_acorn
static void start_acorn(net::Inet& inet)
{
/** SETUP LOGGER */
const int LOGBUFFER_LEN = 1024*16;
static gsl::span<char> spanerino{new char[LOGBUFFER_LEN], LOGBUFFER_LEN};
logger_ = std::make_unique<Logger>(spanerino);
logger_->flush();
logger_->log("LUL\n");
OS::add_stdout(
[] (const char* data, size_t len) {
// append timestamp
auto entry = "[" + isotime::now() + "]" + std::string{data, len};
logger_->log(entry);
});
disk = fs::shared_memdisk();
// init the first legit partition/filesystem
disk->init_fs(
[&inet] (fs::error_t err, auto& fs)
{
if (err) panic("Could not mount filesystem...\n");
// only works with synchronous disks (memdisk)
list_static_content(fs);
/** BUCKET SETUP */
// create squirrel bucket
squirrels = std::make_shared<SquirrelBucket>(10);
// set member name to be unique
squirrels->add_index<std::string>("name",
[](const Squirrel& s)->const auto&
{
return s.get_name();
}, SquirrelBucket::UNIQUE);
// seed squirrels
squirrels->spawn("Alfred"s, 1000U, "Wizard"s);
squirrels->spawn("Alf"s, 6U, "Script Kiddie"s);
squirrels->spawn("Andreas"s, 28U, "Code Monkey"s);
squirrels->spawn("AnnikaH"s, 20U, "Fairy"s);
squirrels->spawn("Ingve"s, 24U, "Integration Master"s);
squirrels->spawn("Martin"s, 16U, "Build Master"s);
squirrels->spawn("Rico"s, 28U, "Mad Scientist"s);
// setup users bucket
users = std::make_shared<UserBucket>();
users->spawn();
users->spawn();
/** ROUTES SETUP **/
using namespace mana;
Router router;
// setup Squirrel routes
router.use("/api/squirrels", routes::Squirrels{squirrels});
// setup User routes
router.use("/api/users", routes::Users{users});
// setup Language routes
router.use("/api/languages", routes::Languages{});
/** DASHBOARD SETUP **/
dashboard_ = std::make_unique<dashboard::Dashboard>(8192);
// Add singleton component
dashboard_->add(dashboard::Memmap::instance());
dashboard_->add(dashboard::StackSampler::instance());
dashboard_->add(dashboard::Status::instance());
// Construct component
dashboard_->construct<dashboard::Statman>(Statman::get());
dashboard_->construct<dashboard::TCP>(inet.tcp());
dashboard_->construct<dashboard::CPUsage>();
dashboard_->construct<dashboard::Logger>(*logger_, static_cast<size_t>(50));
// Add Dashboard routes to "/api/dashboard"
router.use("/api/dashboard", dashboard_->router());
// Fallback route for angular application - serve index.html if route is not found
router.on_get("/app/.*",
[&fs](auto, auto res) {
#ifdef VERBOSE_WEBSERVER
printf("[@GET:/app/*] Fallback route - try to serve index.html\n");
#endif
fs.cstat("/public/app/index.html", [res](auto err, const auto& entry) {
if(err) {
res->send_code(http::Not_Found);
} else {
// Serve index.html
#ifdef VERBOSE_WEBSERVER
printf("[@GET:/app/*] (Fallback) Responding with index.html. \n");
#endif
res->send_file({disk, entry});
}
});
});
INFO("Router", "Registered routes:\n%s", router.to_string().c_str());
//.........这里部分代码省略.........