zoukankan      html  css  js  c++  java
  • CINATRA发布第一个版本

    cinatra是什么?

    cinatra是C++开源社区–purecpp发起的一个开源项目,现在正式发布第一个版本cinatra0.9.0,cinatra是一个现代C++写的web framework,它的目的是给用户提供一个易用、灵活和高性能的web框架,让用户能完全专注于核心逻辑而无需关注http细节。它的灵感来源于sinatra,但又有自己的特色。

    如何使用

    1.从github上下载源码。

    2.安装boost,因为框架用到asio和coroutine,需要1.57及以上的版本。

    3.编译。已经提供vs2013的工程文件和Cmakelist,直接在win和linux平台下编译即可。

    示例

    #include <cinatra/cinatra.hpp>
    using namespace cinatra;
    int main()
    {
        SimpleApp app;
        app.route("/", [](Request&  req , Response& res)
        {
            res.end("Hello Cinatra");
        });
     
        app.listen("http").run();
        
        return 0;
    }

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

    #include <cinatra/cinatra.hpp>
    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<std::string>(b));
        });
     
        app.listen("http").run();
        
        return 0;
    }

    浏览器中输入: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, int b);

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

    struct MyStruct
    {
        void hello(Request& req, Response& res)
        {
            res.end("Hello " + req.session().get<std::string>("uid") + "!");
        }
    };
    MyStruct t;
    // 访问/hello
    app.route("/hello", &MyStruct::hello, &t);

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

    struct CheckLoginAspect
    {
        void before(Request& req, Response& res)
        {
            //如果session没有uid且访问的不是login和test_post页面
            if (!req.session().exists("uid")&&req.path()!="/login.html"&&
                req.path() != "/test_post"&&req.path().compare(0, 7, "/public"))
             {
                 // 跳转到登陆页面
                 res.redirect("/login.html");
             }
        }
     
        void after(Request&  req , Response&  res)
        {
     
        }
    };
     
    #include <cinatra/cinatra.hpp>
    using namespace cinatra;
    int main()
    {
        Cinatra<CheckLoginAspect> app;
        app.route("/", [](Request&  req , Response& res)
        {
            res.end("Hello Cinatra");
        });
        app.listen("http").run();
        
        return 0;
    }

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

    struct CheckLoginAspect
    {
        void before(Request& req, Response& res)
        {
            //如果session没有uid且访问的不是login和test_post页面
            if (!req.session().exists("uid")&&req.path()!="/login.html"&&
                req.path() != "/test_post"&&req.path().compare(0, 7, "/public"))
             {
                 // 跳转到登陆页面
                 res.redirect("/login.html");
             }
        }
     
        void after(Request&  req , Response&  res)
        {
     
        }
    };
     
    struct LogAspect
    {
        void before(cinatra::Request& req, cinatra::Response& res)
        {
            std::cout << "log before" << std::endl;
        }
     
        void after(cinatra::Request& /* req */, cinatra::Response& /* res */)
        {
            std::cout << "log after" << std::endl;
        }
    };
     
    #include <cinatra/cinatra.hpp>
    using namespace cinatra;
    int main()
    {
        Cinatra<CheckLoginAspect, LogAspect> app; //扩展了一个记录日志的切面
        app.route("/", [](Request&  req , Response& res)
        {
            res.end("Hello Cinatra");
        });
     
        app.listen("http").run();
     
        return 0;
    }

    性能

    用ab工具和另外一个c++ web框架crow做了一下性能对比:

    crow:

    crow test_看图王

    cinatra:

    cinatra test_看图王

    可以看到cinatra比crow的性能略高。现在第一个版本还没有专门去做优化,主要是完成了基本功能,后续会持续优化的,也欢迎大家帮忙做进一步的性能测试。

    cinatra的设计

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

    图片1

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

    roadmap

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

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

    开发和参与者

    C++开源社区:http://purecpp.org/,qicosmos(江南),网事如风,SIGSEGV,海盗,福尔摩斯喵。

    更多的例子请到社区github上看。

    如果你发现了问题请及时到社区反馈给我们,也欢迎提出宝贵意见。希望更多的人能参与进来。

    如果你觉得cinatra不错,请不要吝惜给一个star^_^。

  • 相关阅读:
    画图工具Graphviz安装配置
    转:完善eclipse+pdt作php开发中的代码提示能力
    转:SVN 出现This client is too old to work with working copy...错误
    Codeforces Round #260 (Div. 2)C. Boredom(dp)
    three.js 源代码凝视(十四)Math/Sphere.js
    android项目中刷新activity界面
    中科燕园GIS外包---地铁GIS项目
    华为HCNA教程(笔记)
    HapiJS开发手冊
    《Java并发编程实战》第十四章 构建自己定义的同步工具 读书笔记
  • 原文地址:https://www.cnblogs.com/qicosmos/p/4707921.html
Copyright © 2011-2022 走看看