Cinatra:高性能现代CWeb框架.docxVIP

  • 126
  • 0
  • 约3.18千字
  • 约 6页
  • 2021-04-13 发布于天津
  • 举报
Cinatra :高性能现代 C Web 框架 Cinatra 是由 C++ 开源社区 purecpp 发起的一个开源项 目,是一个现代 C++ 写的 Web 框架,旨在给用户提供一个 易用、灵活和高性能的 Web 框架,让用户能完全专注于核 心逻辑而无需关注 http 细节。它的灵感来源于 Sinatra ,但 又有自己的特色。目前正式发布第一个版本 Cinatra0.9.0 。 开发者包括:江南、网事如风、 SIGSEGV 、海盗、福尔摩 斯喵。Cinatra 斯喵。 Cinatra 的设计非常简单,只有几个组件,下 用户仅用面是 Cinatra 的逻辑视图。 Cinatra 逻辑视图 用户仅用 Cinatra 即可,其它的事情框架已经帮用户做好了,用户只 中处理,用关注核心逻辑即可,这些核心逻辑都在 handler 中处理, 而这些 handler 而这些 handler 完全由用户自定义和扩展。 参考示例: #include usingnamespace cinatra; int main() { SimpleApp app; app.route(/, [](Request req , Response res) { res.end(Hello Cinatra); }); app.listen(http).run(); return 0; } 到返回的”运行起来之后, 在浏览器中输入: 就可以看 到返回的” Hello Cinatra 用起”来, 是不是很简单, Cinatra 框 架帮你把很多事情都做好了,你只需要关注你的核心业务逻 辑即可。让我们继续看一个稍微复杂一点的例子。#include 辑即可。让我们继续看一个稍微复杂一点的例子。 #include using namespace cinatra; int main() { SimpleApp app; app.route(/hello, [](Request req , Response res) { res.end(Hello + req.query().get_val(name)); }); app.route(/hello/:name/:age, [](Request req, Response res, const std::string a, int b) { res.end(Name: + a + Age: + boost::lexical_cast(b)); }); app.listen(http).run(); return 0; } 浏览器中输入: /hello?name=testage=12 页面将输出” Hello test ”为。了让用户用起来更方便,我们 还支持下面这种 url 请求方式: /hello/test/12 ,这个 url 将会被路由到 /hello/:name/:age 对应的 handler : [](Request req, Response res, const std::string a, intb); Router 不仅仅支持 Lambda 表达式还支持类的成员函数, 如 struct果你想把 handler 放到类对象中,你可以这样做。 struct MyStruct { void hello(Request req, Response res) { res.end(Hello + req.session().get(uid) + !); } }; MyStruct t; // 访问 /hello app.route(/hello, MyStruct::hello, t); Cinatra 不仅仅使用简单,还很灵活,它支持 AOP , 我们可以很方便的将非核心逻辑和核心逻辑分离,比如下面 的例子。 structCheckLoginAspect { void before(Request req, Response res) { // 如果 session 没有 uid 且访问的不是 login 和 test_post 页面 if (!req.session().exists(uid)req.path()!=/login.html req.path() != /test_postreq.path().compare(0, 7, /public)) { // 跳转到登陆页面 res.redirect(/login.html); } } void after(Request req , Response res) { } }; #include usingnamespace cinatra; int main() { Cinatra app; app.route(/, [](Request req

文档评论(0)

1亿VIP精品文档

相关文档