初始化及基本流程
int goahdStart(void)
{
char *argp, *auth, *home, *documents, *endpoints, *endpoint, *route, *tok, *lspec;
int argind, duration;
route = "route.txt";
auth = "auth.txt";
duration = 0;
logSetPath("stdout:2"); // 打印
documents = ME_GOAHEAD_DOCUMENTS;
//initPlatform();
if (websOpen(documents, route) < 0)
{
error("初始化WEB系统失败. Exiting.");
return -1;
}
logHeader();
if (websLoad(auth) < 0)
{
error("Cannot load %s", auth);
return -1;
}
if (websListen("http://*:80") < 0)
{
return -1;
}
websDefineHandler("test", testHandler, nullptr, nullptr, 0);
websAddRoute("/test", "test", 0);
websDefineAction("exampleAction", (void*)exampleAction);
if (websGetBackground()) {
if (daemon(0, 0) < 0) {
error("Cannot run as daemon");
return -1;
}
}
websServiceEvents(&finished);
return 0;
}
goActions
goActions定义
static void exampleAction(Webs *wp)
{
const char *io_val;
io_val = websGetVar(wp, "val_led", nullptr); // 获取POST过来的数据
qDebug() << "on_led_set: " << io_val;
websSetStatus(wp, 200);
websWriteHeaders(wp, -1, nullptr);
websWriteEndHeaders(wp);
websWrite(wp, "%s", "<h4>hello GoAction!</h4>"); // 返回数据
websDone(wp);
return nullptr;
/****这句注册一个可以通过action/exampleGetAction访问的GoAction函数****/
websDefineAction("exampleAction", exampleGetAction);
正文完