代码拉取完成,页面将自动刷新
该项目当时写的比较仓促,可用性并不高并且长期处于停止维护的状态。如果你感兴趣的话请访问https://github.com/Nodesheep/cweb 我最近重新开始了该项目的维护,并拓展了很多新能力,可用性上目前来看还是可以的,不过由于没有大范围实践,所以稳定性上不能够很好保证,如果使用中有任何问题,可以告知我让项目变得更加完善。我会在github上持续维护该项目,感谢各位支持。
cpp仿造gin写的一个简易版web框架
#include <iostream>
#include <string>
#include "json.h"
#include "cin.hpp"
using namespace std;
void handle1(context* c) {
c->STRING(Status_OK, "hi, have a good day");
}
void handle2(context* c) {
string resp = "I heard your name: " + c->req_.getQuery()["name"];
c->STRING(Status_OK, resp);
}
void handle3(context* c) {
string resp = "dynamic param: " + c->params_["param"];
c->STRING(Status_OK, resp);
}
void handle4(context* c) {
cout << "use group handle4" << endl;
c->next();
}
void handle5(context* c) {
Json::Value root;
root["name"] = "lemon";
root["sex"] = "man";
root["age"] = 23;
Json::Value hobby;
hobby["sport"] = "football";
hobby["else"] = "sing";
root["hobby"] = hobby;
c->JSON(Status_OK, root);
}
int main(int argc, const char * argv[]) {
Cweb c("127.0.0.1", 6666); //初始化
c.GET("/api/sayhi", handle1);
c.GET("/api/echo", handle2); //带参数 ?key=value
c.GET("/api/dynamic/:param", handle3); //动态路由
group* g1 = c.Group("/group"); //分组操作
g1->USE(handle4); //中间件
g1->GET("/sayhi", handle5);
c.run(4);//运行
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。