PaoZhu C++ Web Framework比美Java SpringBoot 的注解

PaoZhu C++ Web Framework 内置HTTP/2 ORM 可以完全开发Web生态,提供注解功能。

C++ 注解机制更加简单

复制代码
//@urlpath(admin_islogin,admin/add_article)

一句话可以把一个函数变成注解函数,admin_islogin是拦截函数,访问admin/add_article之前执行认证,如果没有认证,不能执行注解函数。

详细代码,在controller/src目录下创建文件article

复制代码
//@urlpath(admin_islogin,admin/add_article)
std::string admin_add_article(std::shared_ptr<httppeer> peer)
{
    httppeer &client = peer->get_peer();
    client<<"Hello World!";
    return "";
}

这样可以在浏览器http://localhost/admin/add_article 这样访问了,非常简单地输出了Hello World! 到浏览器,是不是很惊讶,C++ 怎么做到的,这就是PaoZhu C++ Web Framework。

PaoZhu ORM提供协程和同步模式支持。

协程模式

复制代码
        //@urlpath(null,queries)
        asio::awaitable<std::string> techempowerqueries(std::shared_ptr<httppeer> peer)
        {
            peer->type("application/json; charset=UTF-8");
            peer->set_header("Date", get_gmttime());
        
            unsigned int get_num = peer->get["queries"].to_int();
            if (get_num == 0)
            {
                get_num = 1;
            }
            else if (get_num > 500)
            {
                get_num = 500;
            }
            auto myworld = orm::World();
            myworld.record.reserve(get_num);
            myworld.lock_conn();
            for (unsigned int i = 0; i < get_num; i++)
            {
                myworld.wheresql.clear();
                unsigned int rd_num = rand_range(1, 10000);
                myworld.where("id", rd_num);
                co_await myworld.async_fetch_append();
            }
            myworld.unlock_conn();
            peer->output = myworld.to_json();
            co_return "";
        }

同步模式

复制代码
        //@urlpath(admin_islogin,admin/addtopic)
        std::string admin_addtopic(std::shared_ptr<httppeer> peer)
        {
            httppeer &client = peer->get_peer();
            try
            {
                auto topicm = orm::cms::Topic();
                topicm.where("userid", client.session["userid"].to_int()).asc("parentid").fetch();
        
                client.val["list"].set_array();
                obj_val temp;
        
                for (unsigned int i = 0; i < topicm.record.size(); i++)
                {
                    temp["id"]       = topicm.record[i].topicid;
                    temp["parentid"] = topicm.record[i].parentid;
                    temp["value"]    = topicm.record[i].title;
                    client.val["list"].push(temp);
                }
            }
            catch (std::exception &e)
            {
                client.val["code"] = 1;
            }
        
            peer->view("admin/addtopic");
            return "";
        }
        

他们都是方便利用了注解功能做控制器。

更多入门内容可以访问官方入门文档。

https://hggq.github.io/paozhudocs/documentation.html

Paozhu C++ Web框架 官方github库

https://github.com/hggq/paozhu