Cinatra:高性能现代C++ Web框架

Cinatra是由C++开源社区purecpp发起的一个开源项目,是一个现代C++写的Web框架,旨在给用户提供一个易用、灵活和高性能的Web框架,让用户能完全专注于核心逻辑而无需关注http细节。它的灵感来源于Sinatra,但又有自己的特色。目前正式发布第一个版本Cinatra0.9.0。开发者包括:江南、网事如风、SIGSEGV、海盗、福尔摩斯喵。

Cinatra的设计非常简单,只有几个组件,下面是Cinatra的逻辑视图。

Cinatra逻辑视图

用户仅用Cinatra即可,其它的事情框架已经帮用户做好了,用户只用关注核心逻辑即可,这些核心逻辑都在handler中处理,而这些handler完全由用户自定义和扩展。

参考示例:

[js]  view plain copy
  1. #include<cinatra/cinatra.hpp>  
  2.   
  3. usingnamespace cinatra;  
  4.   
  5. int main()  
  6.   
  7. {  
  8.   
  9.   SimpleApp app;  
  10.   
  11.   app.route("/", [](Request&  req , Response& res)  
  12.   
  13.   {  
  14.   
  15.     res.end("Hello Cinatra");  
  16.   
  17.   });  
  18.   
  19.   app.listen("http").run();  
  20.   
  21.    
  22.   
  23.   return 0;  
  24.   
  25. }  

运行起来之后,在浏览器中输入:127.0.0.1就可以看到返回的” Hello Cinatra”, 用起来是不是很简单,Cinatra框架帮你把很多事情都做好了,你只需要关注你的核心业务逻辑即可。让我们继续看一个稍微复杂一点的例子。

[js]  view plain copy
  1. #include <cinatra/cinatra.hpp>  
  2. using namespace cinatra;  
  3. int main()  
  4. {  
  5.     SimpleApp app;  
  6.     app.route("/hello", [](Request&  req , Response& res)  
  7.     {  
  8. res.end("Hello " + req.query().get_val("name"));      
  9.      });  
  10.     app.route("/hello/:name/:age", [](Request& req, Response& res, const std::string& a, int b)  
  11. {  
  12.         res.end("Name: " + a + " Age: " + boost::lexical_cast<std::string>(b));  
  13. });  
  14.   
  15.     app.listen("http").run();  
  16.       
  17.     return 0;  
  18. }  

浏览器中输入:127.0.0.1/hello?name=test&age=12,页面将输出” Hello test”。  为了让用户用起来更方便,我们还支持下面这种url请求方式:127.0.0.1/hello/test/12,这个url将会被路由到"/hello/:name/:age"对应的handler:[](Request& req, Response& res, const std::string& a, intb);

Router不仅仅支持Lambda表达式还支持类的成员函数,如果你想把handler放到类对象中,你可以这样做。

[js]  view plain copy
  1. struct MyStruct  
  2. {  
  3.     void hello(Request& req, Response& res)  
  4.     {  
  5.         res.end("Hello " + req.session().get<std::string>("uid") + "!");  
  6.     }  
  7. };  
  8. MyStruct t;  
  9. // 访问/hello  
  10. app.route("/hello", &MyStruct::hello, &t);  

Cinatra不仅仅使用简单,还很灵活,它支持AOP,我们可以很方便的将非核心逻辑和核心逻辑分离,比如下面的例子。

[js]  view plain copy
  1. structCheckLoginAspect  
  2.   
  3. {  
  4.   
  5.   void before(Request& req, Response& res)  
  6.   
  7.   {  
  8.   
  9.     //如果session没有uid且访问的不是login和test_post页面  
  10.   
  11.     if (!req.session().exists("uid")&&req.path()!="/login.html"&&  
  12.   
  13.       req.path() != "/test_post"&&req.path().compare(0, 7, "/public"))  
  14.   
  15.     {  
  16.   
  17.       // 跳转到登陆页面  
  18.   
  19.       res.redirect("/login.html");  
  20.   
  21.     }  
  22.   
  23.   }  
  24.   
  25.   void after(Request&  req , Response&  res)  
  26.   
  27.   {  
  28.   
  29.   }  
  30.   
  31. };  
  32.  
  33. #include<cinatra/cinatra.hpp>  
  34.   
  35. usingnamespace cinatra;  
  36.   
  37. int main()  
  38.   
  39. {  
  40.   
  41.   Cinatra<CheckLoginAspect> app;  
  42.   
  43.   app.route("/", [](Request&  req , Response& res)  
  44.   
  45.   {  
  46.   
  47.     res.end("Hello Cinatra");  
  48.   
  49.   });  
  50.   
  51. app.listen("http").run();  
  52.   
  53.   return 0;  
  54.   
  55. }  

在上面的例子中增加了一个检查是否登录的切面,如果用户没登录将会重定向到登录页面。此外,还可以自由组合多个切面,Cinatra可以很方便地扩展任意多个切面。你可以这样扩展切面。

[js]  view plain copy
  1. structCheckLoginAspect  
  2.   
  3. {  
  4.   
  5.   void before(Request& req, Response& res)  
  6.   
  7.   {  
  8.   
  9.     //如果session没有uid且访问的不是login和test_post页面  
  10.   
  11.     if (!req.session().exists("uid")&&req.path()!="/login.html"&&  
  12.   
  13.       req.path() != "/test_post"&&req.path().compare(0, 7, "/public"))  
  14.   
  15.     {  
  16.   
  17.       // 跳转到登陆页面  
  18.   
  19.       res.redirect("/login.html");  
  20.   
  21.     }  
  22.   
  23.   }  
  24.   
  25.   void after(Request&  req , Response&  res)  
  26.   
  27.   {  
  28.   
  29.   }  
  30.   
  31. };  
  32.   
  33. structLogAspect  
  34.   
  35. {  
  36.   
  37.   void before(cinatra::Request& req, cinatra::Response& res)  
  38.   
  39.   {  
  40.   
  41.     std::cout << "log before" << std::endl;  
  42.   
  43.   }  
  44.   
  45.   void after(cinatra::Request& /* req */, cinatra::Response& /* res */)  
  46.   
  47.   {  
  48.   
  49.     std::cout << "log after" << std::endl;  
  50.   
  51.   }  
  52.   
  53. };  
  54.  
  55. #include<cinatra/cinatra.hpp>  
  56.   
  57. usingnamespace cinatra;  
  58.   
  59. int main()  
  60.   
  61. {  
  62.   
  63.   Cinatra<CheckLoginAspect, LogAspect> app; //扩展了一个记录日志的切面  
  64.   
  65.   app.route("/", [](Request&  req , Response& res)  
  66.   
  67.   {  
  68.   
  69.     res.end("Hello Cinatra");  
  70.   
  71.   });  
  72.   
  73. app.listen("http").run();  
  74.   
  75.   return 0;  
  76.   
  77. }  

目前支持http1.0和1.1,支持session和cookie,后续计划:

  • https
  • html模板
  • websocket
  • cinatra打造purecpp社区

更多参考示例,请移步社区Github

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值