草庐IT

新款 c++ web framework 支持orm http/2

paozhu 2023-03-28 原文

c++ web framework很少,

随着c++ 热度升温,c++ 在人工智能 自然语言处理 加快应用。

最近一款国产 c++ web framework 问世

写业务速度跟脚步语言一样速度

  1. 自带json内置支持

  2. 支持多域名网站

  3. 支持多域名ssl 服务端

  4. 支持http1.1、http2协议

  5. 支持websocket服务端,

  6. 框架自带websocket推送,支持定时推送到webscoket客户端

  7. 支持同步httpclient get post

  8. 框架自带ORM,使用链接池方式,目前支持mysql

  9. 框架自带线程池,和用户代码运行的线程池

  10. 框架使用asio自带的协程

  11. 框架特色是I/O 使用协程池 运行使用线程池

  12. 框架支持普通文件gzip br

  13. 框架解析URL和POST,解析结果类似PHP GET POST方式获取内容

  14. 集成sendmail

  15. 生成二维码(qrcode),需要gd、qrencode库

 

目前支持mac linux

具体可以看官方github

https://github.com/hggq/paozhu

 

 

 

主要是写业务代码优雅,方便 CURD例子

 

#include "orm.h"
#include <chrono>
#include <thread>
#include "md5.h"
#include "func.h"
#include "httppeer.h"
#include "testcurd.h"
namespace http
{

   std::string articlelogin(std::shared_ptr<httppeer> peer)
   {
      // step1  show login page
      peer->view("login/login");
      return "";
   }
   std::string articleloginpost(std::shared_ptr<httppeer> peer)
   {
      // step2
      // get login/login post field
      httppeer &client = peer->getpeer();
      std::string username = client.post["username"].to_string();
      std::string password = client.post["password"].to_string();

      auto users = orm::cms::User();
      std::string md5string;
 
      try
      {
         md5string = md5(password);
         users.where("name=", username).whereAnd("password=", md5string).limit(1).fetch();
         // view orm create sql
         // client<<"<p>"<<users.sqlstring<<"</p>";
         if (users.getUserid() > 0)
         {
            // save session,other page get  int userid= client.session["userid"].to_int();
            client.session["userid"] = users.getUserid();
            client.save_session();
            client.goto_url("/cms/list");
            return "";
         }
         else
         {
            client.goto_url("/cms/login",3,"用户名或密码错误!");
            return "";
         }
      }
      catch (std::exception &e)
      {
         client << "<p>" << e.what() << "</p>";
         return "";
      }

      return "";
   }
   std::string articlelist(std::shared_ptr<httppeer> peer)
   {
      // step3 content list
      httppeer &client = peer->getpeer();
      int userid = client.session["userid"].to_int();
      if (userid == 0)
      {
         // client.goto_url("/cms/login");
         client.val["msg"] = "<a href=\"/cms/login\">Please login </a>";
      }

      auto articles = orm::cms::Article();

      int page = client.get["page"].to_int();
      if (page < 0)
      {
         page = 0;
      }
      page = page * 20;
      articles.where("isopen=1").order(" aid desc ").limit(page, 20).fetch();
      // 也可以直接返回OBJ_VALUE 对象; 不过正常业务会要处理下结果集
      // You can also return the OBJ_VALUE object directly; but normal business process will need to process the result set
      client.val["list"].set_array();
      if (articles.size() > 0)
      {
         for (auto &bb : articles)
         {

            OBJ_ARRAY item;
            item["aid"] = bb.aid;
            item["title"] = bb.title;
            item["createtime"] = bb.createtime;
            item["summary"] = bb.summary;
            // client<<"<p><a href=\"/cms/show?id="<<bb.aid<<"\">"<<bb.title<<"</a>  "<<bb.createtime<<" </p>";
            client.val["list"].push(std::move(item));
         }
      }

      peer->view("cms/list");
      return "";
   }
   std::string articleshow(std::shared_ptr<httppeer> peer)
   {
      // step4
      httppeer &client = peer->getpeer();
      auto articles = orm::cms::Article();
      int aid = client.get["id"].to_int();

      articles.where("isopen=1").where(" aid=", aid).limit(1).fetch();

      client.val["title"] = articles.getTitle();
      client.val["content"] = articles.getContent();

      peer->view("cms/show");
      return "";
   }
   std::string articleedit(std::shared_ptr<httppeer> peer)
   {
      // same the show content
      httppeer &client = peer->getpeer();
      auto articles = orm::cms::Article();
      int aid = client.get["id"].to_int();

      articles.where("isopen=1").where(" aid=", aid).limit(1).fetch();

      client.val["title"] = articles.getTitle();
      client.val["content"] = html_encode(articles.getRefContent());
      client.val["aid"] = articles.getAid();
      peer->view("cms/edit");
      return "";
   }

   std::string articleeditpost(std::shared_ptr<httppeer> peer)
   {
      httppeer &client = peer->getpeer();
      std::string title = client.post["title"].to_string();
      std::string content = client.post["content"].to_string();
      unsigned int aid = client.post["aid"].to_int();

      auto articles = orm::cms::Article();
      // articles.where("isopen=1").where(" aid=",aid).limit(1).fetch();
      // articles.data.aid=aid;
      // articles.data.title=title;
      // articles.setAid(aid);
      articles.setTitle(title);
      // articles.setTitle("直接标题");
      articles.setContent(content);

      articles.where(" aid=", aid);
      int effectnum = 0;
      try
      {
         effectnum = articles.update("title,content");
      }
      catch (std::exception &e)
      {
         client << "<p>" << articles.sqlstring << "</p>";
         client << "<p>" << e.what() << "</p>";
         return "";
      }
      if (effectnum > 0)
      {

         client.goto_url("/cms/list", 3, "内容已经更新");
         return "";
      }
      else
      {
         client.goto_url("/cms/list", 3, "更新出错(error)");
         return "";
      }

      return "";
   }

   std::string articleadd(std::shared_ptr<httppeer> peer)
   {
      httppeer &client = peer->getpeer();
      peer->view("cms/add");
      return "";
   }
   std::string articleaddpost(std::shared_ptr<httppeer> peer)
   {
      httppeer &client = peer->getpeer();
      std::string title = client.post["title"].to_string();
      std::string content = client.post["content"].to_string();
      unsigned int aid = 0;

      auto articles = orm::cms::Article();

      // articles.data.aid=aid;
      // articles.data.title=title;
      // articles.setAid(aid);
      articles.setIsopen(1);
      articles.setCreatetime(date("%Y-%m-%d %X")); // Y-m-d H:i:s
      articles.setAddtime(timeid());               // unix timestamp
      articles.setAddip(client.client_ip);    // client ip
      articles.setTitle(title);
      // articles.setTitle("直接标题");
      articles.setContent(content);

      int effectnum = 0;
      try
      {
         effectnum = articles.save();
         aid = articles.getAid();
         client << "<p>新(new)id " << aid << " 或 新(new)id " << effectnum << "</p>";
      }
      catch (std::exception &e)
      {
         client << "<p>" << articles.sqlstring << "</p>";
         client << "<p>" << e.what() << "</p>";
         return "";
      }
      if (effectnum > 0)
      {

         client.goto_url("/cms/list", 3, "内容已经添加");
         return "";
      }
      else
      {
         client.goto_url("/cms/list", 3, "添加出错(error)");
         return "";
      }

      return "";
   }
   std::string articledelete(std::shared_ptr<httppeer> peer)
   {
      httppeer &client = peer->getpeer();
      unsigned int aid = client.get["id"].to_int();

      auto articles = orm::cms::Article();
      //  可以先查询是否存在或有权限之类
      // articles.where("isopen=1").where(" aid=",aid).limit(1).fetch();

      int effectnum = 0;
      try
      {
         effectnum = articles.remove(aid);
      }
      catch (std::exception &e)
      {
         client << "<p>" << articles.sqlstring << "</p>";
         client << "<p>" << e.what() << "</p>";
         return "";
      }
      if (effectnum > 0)
      {

         client.goto_url("/cms/list", 3, "内容已经删除");
         return "";
      }
      else
      {
         client.goto_url("/cms/list", 3, "删除出错(error)");
         return "";
      }

      return "";
   }
}

 

更看官方controller例子

 

有关新款 c++ web framework 支持orm http/2的更多相关文章

  1. ruby - 如何使用 readline 支持重新安装 ruby​​? - 2

    我已经按照https://github.com/wayneeseguin/rvm#installation上的说明通过RVM安装了Ruby.有关信息,我有所有文件(readline-5.2.tar.gz、readline-6.2.tar.gz、ruby-1.9.3-p327.tar.bz2、rubygems-1.8.24.tgz、wayneeseguin-rvm-stable.tgz和yaml-0.1.4.tar.gz)在~/.rvm/archives目录中,我不想在任何目录中重新下载它们方式。当我这样做时:sudo/usr/bin/apt-getinstallbuild-essent

  2. ruby-on-rails - "undefined method ` stub_request '"访问 RSpec 支持文件中的方法时 - 2

    我的Ruby-on-Rails项目中有以下文件结构,用于规范:/spec/msd/serviceservice_spec.rb/support/my_modulerequests_stubs.rb我的request_stubs.rb有:moduleMyModule::RequestsStubsmodule_functiondeflist_clientsurl="dummysite.com/clients"stub_request(:get,url).to_return(status:200,body:"clientsbody")endend在我的service_spec.rb我有:re

  3. ruby - Ruby 是否支持逐字字符串? - 2

    Ruby是否支持(找不到更好的词)非转义(逐字)字符串?就像在C#中一样:@"c:\ProgramFiles\"...或者在Tcl中:{c:\ProgramFiles\} 最佳答案 是的,您需要在字符串前加上%前缀,然后是描述其类型的单个字符。你想要的是%q{c:\programfiles\}。镐书很好地涵盖了这一点here,部分是通用分隔输入。 关于ruby-Ruby是否支持逐字字符串?,我们在StackOverflow上找到一个类似的问题: https:/

  4. ruby - 在 Ruby 1.8 中支持 Ruby 1.9 的哈希语法 - 2

    我正在编写一个Rubygem,在我的代码中使用{key:'value'}哈希语法。我的测试都在1.9.x中通过,但我(可以理解)在1.8.7中得到syntaxerror,unexpected':',expecting')'。是否有支持1.8.x的最佳实践?我是否需要使用我们的老friend=>重写代码,还是有更好的策略? 最佳答案 我认为你运气不好,如果你想支持1.8,那么你必须使用=>。像往常一样,我会提到在1.9的某些情况下您必须使用=>:如果键不是一个符号。请记住,任何对象(符号、字符串、类、float……)都可以是Ruby哈

  5. ruby-on-rails - Rails 是否支持监听 UDP 套接字的简洁方式? - 2

    在Rails中,什么是集成更新模型某些元素的UDP监听过程的最佳方式(特别是它将向其中一个表添加行)。简单的答案似乎是在同一个进程中使用UDP套接字对象启动一个线程,但我什至不清楚我应该在哪里做适合Rails方式的事情。有没有一种巧妙的方法来开始收听UDP?具体来说,我希望能够编写一个UDPController并在每个数据报消息上调用一个特定的方法。理想情况下,我希望避免在UDP上使用HTTP(因为它会浪费一些在这种情况下非常宝贵的空间),但我完全控制消息格式,因此我可以为Rails提供它需要的任何信息。 最佳答案 Rails是一个

  6. ruby - Watir-Webdriver 是否支持点击目标为 javascript 的链接? - 2

    我是Ruby和Watir-Webdriver的新手。我有一套用VBScript编写的站点自动化程序,我想将其转换为Ruby/Watir,因为我现在必须支持Firefox。我发现我真的很喜欢Ruby,而且我正在研究Watir,但我已经花了一周时间试图让Webdriver显示我的登录屏幕。该站点以带有“我同意”区域的“警告屏幕”开头。用户点击我同意并显示登录屏幕。我需要单击该区域以显示登录屏幕(这是同一页面,实际上是一个表单,只是隐藏了)。我整天都在用VBScript这样做:objExplorer.Document.GetElementsByTagName("area")(0).click

  7. Ruby - 不支持的密码算法 (AES-256-GCM) - 2

    我收到错误:unsupportedcipheralgorithm(AES-256-GCM)(RuntimeError)但我似乎具备所有要求:ruby版本:$ruby--versionruby2.1.2p95OpenSSL会列出gcm:$opensslenc-help2>&1|grepgcm-aes-128-ecb-aes-128-gcm-aes-128-ofb-aes-192-ecb-aes-192-gcm-aes-192-ofb-aes-256-ecb-aes-256-gcm-aes-256-ofbRuby解释器:$irb2.1.2:001>require'openssl';puts

  8. ruby - jekyll - 插件支持 - 它是如何工作的? - 2

    我刚找到thiscomment来自mojombo:ThelatestonmasternowhasPluginsupport.Lookatlib/jekyll/convertersforexamplesofhowthey'redone.Also,any*.rbfilesina_pluginsdirectorywillbeloadedsothatyoucancreatecustompluginsofyourown.我看过/lib/jekyll/converters但无法理解它们应该如何工作。谁能给我解释一下?非常感谢。 最佳答案 一个新

  9. ruby-on-rails - routes.rb - 不支持 Controller 名称 - 2

    我遇到了一个很奇怪的问题。这是与routes.rb相关的部分:resources:playersmatch'/players/:userid',:to=>'Players#show'当您访问localhost:3000/players/1234时出现此错误:'Players'isnotasupportedcontrollername.Thiscanleadtopotentialroutingproblems.Controller中的相关代码:defshowbeginifPlayer.find_by(:uid=>:userid)then@playerattributes=Player.f

  10. ruby - 具有 HTTPS、SSL 客户端证书和 Keep-Alive 支持的 Ruby HTTP 库? - 2

    我正在尝试用Ruby编写一个HTTPS客户端。它将使用HTTPS连接到服务器,传递身份验证token(通过单独的登录过程获得)和SSL客户端证书。我正在使用rest-client执行以下操作:client=RestClient::Resource.new(url,:ssl_client_cert=>OpenSSL::X509::Certificate.new(File.read('./certificate/client-2048.pem')),:ssl_client_key=>OpenSSL::PKey::RSA.new(File.read('./certificate/client

随机推荐