Skip to content

Instantly share code, notes, and snippets.

@slankdev
Created November 6, 2017 13:57
Show Gist options
  • Save slankdev/02cc287b2f5183d6c1e07a5e5aa7aa25 to your computer and use it in GitHub Desktop.
Save slankdev/02cc287b2f5183d6c1e07a5e5aa7aa25 to your computer and use it in GitHub Desktop.
crow テスト
#include <crow.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <slankdev/string.h>
struct S {
int a;
int b;
int c;
void show() const
{
printf("a: %d \n", a);
printf("b: %d \n", b);
printf("c: %d \n", c);
}
};
struct MiddleWare {
S* ptr;
struct context {};
void before_handle(crow::request&, crow::response&, context&) {}
void after_handle(crow::request&, crow::response&, context&) {}
};
int main()
{
crow::App<MiddleWare> app;
S s;
app.get_middleware<MiddleWare>().ptr = &s;
CROW_ROUTE(app,"/print") ( [&]() {
crow::json::wvalue x;
S* s = app.get_middleware<MiddleWare>().ptr;
x["a"] = slankdev::format("%d", s->a);
x["b"] = slankdev::format("%d", s->b);
x["c"] = slankdev::format("%d", s->c);
return x;
});
CROW_ROUTE(app, "/<str>").methods(crow::HTTPMethod::GET) (
[&](std::string str)
{
S* s = app.get_middleware<MiddleWare>().ptr;
if (str == "a") return std::to_string(s->a).c_str();
else if (str == "b") return std::to_string(s->b).c_str();
else if (str == "c") return std::to_string(s->c).c_str();
else return "";
}
);
CROW_ROUTE(app, "/valset/<str>").methods(crow::HTTPMethod::POST) (
[&](const crow::request& req, std::string str)
{
auto x = crow::json::load(req.body);
if (!x) {
printf("json error: \n%s\n\n", req.body.c_str());
return "JSON ERR\n";
}
S* s = app.get_middleware<MiddleWare>().ptr;
if (str == "a") s->a = x["a"].i();
else if (str == "b") s->a = x["b"].i();
else if (str == "c") s->a = x["c"].i();
else return "error\n";
if (str == "a") return std::to_string(s->a).c_str();
else if (str == "b") return std::to_string(s->b).c_str();
else if (str == "c") return std::to_string(s->c).c_str();
else return "error\n";
}
);
app.loglevel(crow::LogLevel::Critical);
app.port(8888).run();
}
LIBSLANKDEV := $(HOME)/git/libslankdev
CXXFLAGS += -I$(LIBSLANKDEV) -std=c++11 -I$(HOME)
LDFLAGS += -lboost_system
all:
$(CXX) $(CXXFLAGS) main.cc $(LDFLAGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment