|
c++ web framework 很少
随着 c++ 热度升温,c++ 在人工智能 自然语言处理 加快应用。
最近一款国产 paozhu c++ web framework 问世
写业务速度跟脚步语言一样速度,主要是写业务代码优雅,方便 后面附 CURD 例子
- 自带 json 内置 decode encode 支持,支持标准 json 输出,支持 emoji 编码
- 支持多域名网站
- 支持多域名 ssl 服务端
- 支持 http1.1、http2 协议
- 支持 websocket 服务端,
- 框架自带 websocket 推送,支持定时推送到 webscoket 客户端
- 支持同步 httpclient get post
- 框架自带 ORM, 使用链接池方式,目前支持 mysql
- 框架自带线程池,和用户代码运行的线程池
- 框架使用 asio 自带的协程
- 框架特色是 I/O 使用协程池 运行使用线程池
- 框架支持普通文件 gzip br
- 框架解析 URL 和 POST, 解析结果类似 PHP GET POST 方式获取内容
- 集成 sendmail
- 生成二维码 (qrcode), 需要 gd、qrencode 库
目前支持mac linux
具体可以看官方github

#include "orm.h"
#include <chrono>
#include <thread>
#include &#34;md5.h&#34;
#include &#34;func.h&#34;
#include &#34;httppeer.h&#34;
#include &#34;testcurd.h&#34;
namespace http
{
std::string articlelogin(std::shared_ptr<httppeer> peer)
{
// step1 show login page
peer->view(&#34;login/login&#34;);
return &#34;&#34;;
}
std::string articleloginpost(std::shared_ptr<httppeer> peer)
{
// step2
// get login/login post field
httppeer &client = peer->getpeer();
std::string username = client.post[&#34;username&#34;].to_string();
std::string password = client.post[&#34;password&#34;].to_string();
auto users = orm::cms::User();
std::string md5string;
try
{
md5string = md5(password);
users.where(&#34;name=&#34;, username).whereAnd(&#34;password=&#34;, md5string).limit(1).fetch();
// view orm create sql
// client<<&#34;<p>&#34;<<users.sqlstring<<&#34;</p>&#34;;
if (users.getUserid() > 0)
{
// save session,other page get int userid= client.session[&#34;userid&#34;].to_int();
client.session[&#34;userid&#34;] = users.getUserid();
client.save_session();
client.goto_url(&#34;/cms/list&#34;);
return &#34;&#34;;
}
else
{
client.goto_url(&#34;/cms/login&#34;,3,&#34;用户名或密码错误!&#34;);
return &#34;&#34;;
}
}
catch (std::exception &e)
{
client << &#34;<p>&#34; << e.what() << &#34;</p>&#34;;
return &#34;&#34;;
}
return &#34;&#34;;
}
std::string articlelist(std::shared_ptr<httppeer> peer)
{
// step3 content list
httppeer &client = peer->getpeer();
int userid = client.session[&#34;userid&#34;].to_int();
if (userid == 0)
{
// client.goto_url(&#34;/cms/login&#34;);
client.val[&#34;msg&#34;] = &#34;<a href=\&#34;/cms/login\&#34;>Please login </a>&#34;;
}
auto articles = orm::cms::Article();
int page = client.get[&#34;page&#34;].to_int();
if (page < 0)
{
page = 0;
}
page = page * 20;
articles.where(&#34;isopen=1&#34;).order(&#34; aid desc &#34;).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[&#34;list&#34;].set_array();
if (articles.size() > 0)
{
for (auto &bb : articles)
{
OBJ_ARRAY item;
item[&#34;aid&#34;] = bb.aid;
item[&#34;title&#34;] = bb.title;
item[&#34;createtime&#34;] = bb.createtime;
item[&#34;summary&#34;] = bb.summary;
// client<<&#34;<p><a href=\&#34;/cms/show?id=&#34;<<bb.aid<<&#34;\&#34;>&#34;<<bb.title<<&#34;</a> &#34;<<bb.createtime<<&#34; </p>&#34;;
client.val[&#34;list&#34;].push(std::move(item));
}
}
peer->view(&#34;cms/list&#34;);
return &#34;&#34;;
}
std::string articleshow(std::shared_ptr<httppeer> peer)
{
// step4
httppeer &client = peer->getpeer();
auto articles = orm::cms::Article();
int aid = client.get[&#34;id&#34;].to_int();
articles.where(&#34;isopen=1&#34;).where(&#34; aid=&#34;, aid).limit(1).fetch();
client.val[&#34;title&#34;] = articles.getTitle();
client.val[&#34;content&#34;] = articles.getContent();
peer->view(&#34;cms/show&#34;);
return &#34;&#34;;
}
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[&#34;id&#34;].to_int();
articles.where(&#34;isopen=1&#34;).where(&#34; aid=&#34;, aid).limit(1).fetch();
client.val[&#34;title&#34;] = articles.getTitle();
client.val[&#34;content&#34;] = html_encode(articles.getRefContent());
client.val[&#34;aid&#34;] = articles.getAid();
peer->view(&#34;cms/edit&#34;);
return &#34;&#34;;
}
std::string articleeditpost(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->getpeer();
std::string title = client.post[&#34;title&#34;].to_string();
std::string content = client.post[&#34;content&#34;].to_string();
unsigned int aid = client.post[&#34;aid&#34;].to_int();
auto articles = orm::cms::Article();
// articles.where(&#34;isopen=1&#34;).where(&#34; aid=&#34;,aid).limit(1).fetch();
// articles.data.aid=aid;
// articles.data.title=title;
// articles.setAid(aid);
articles.setTitle(title);
// articles.setTitle(&#34;直接标题&#34;);
articles.setContent(content);
articles.where(&#34; aid=&#34;, aid);
int effectnum = 0;
try
{
effectnum = articles.update(&#34;title,content&#34;);
}
catch (std::exception &e)
{
client << &#34;<p>&#34; << articles.sqlstring << &#34;</p>&#34;;
client << &#34;<p>&#34; << e.what() << &#34;</p>&#34;;
return &#34;&#34;;
}
if (effectnum > 0)
{
client.goto_url(&#34;/cms/list&#34;, 3, &#34;内容已经更新&#34;);
return &#34;&#34;;
}
else
{
client.goto_url(&#34;/cms/list&#34;, 3, &#34;更新出错(error)&#34;);
return &#34;&#34;;
}
return &#34;&#34;;
}
std::string articleadd(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->getpeer();
peer->view(&#34;cms/add&#34;);
return &#34;&#34;;
}
std::string articleaddpost(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->getpeer();
std::string title = client.post[&#34;title&#34;].to_string();
std::string content = client.post[&#34;content&#34;].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(&#34;%Y-%m-%d %X&#34;)); // Y-m-d H:i:s
articles.setAddtime(timeid()); // unix timestamp
articles.setAddip(client.client_ip); // client ip
articles.setTitle(title);
// articles.setTitle(&#34;直接标题&#34;);
articles.setContent(content);
int effectnum = 0;
try
{
effectnum = articles.save();
aid = articles.getAid();
client << &#34;<p>新(new)id &#34; << aid << &#34; 或 新(new)id &#34; << effectnum << &#34;</p>&#34;;
}
catch (std::exception &e)
{
client << &#34;<p>&#34; << articles.sqlstring << &#34;</p>&#34;;
client << &#34;<p>&#34; << e.what() << &#34;</p>&#34;;
return &#34;&#34;;
}
if (effectnum > 0)
{
client.goto_url(&#34;/cms/list&#34;, 3, &#34;内容已经添加&#34;);
return &#34;&#34;;
}
else
{
client.goto_url(&#34;/cms/list&#34;, 3, &#34;添加出错(error)&#34;);
return &#34;&#34;;
}
return &#34;&#34;;
}
std::string articledelete(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->getpeer();
unsigned int aid = client.get[&#34;id&#34;].to_int();
auto articles = orm::cms::Article();
// 可以先查询是否存在或有权限之类
// articles.where(&#34;isopen=1&#34;).where(&#34; aid=&#34;,aid).limit(1).fetch();
int effectnum = 0;
try
{
effectnum = articles.remove(aid);
}
catch (std::exception &e)
{
client << &#34;<p>&#34; << articles.sqlstring << &#34;</p>&#34;;
client << &#34;<p>&#34; << e.what() << &#34;</p>&#34;;
return &#34;&#34;;
}
if (effectnum > 0)
{
client.goto_url(&#34;/cms/list&#34;, 3, &#34;内容已经删除&#34;);
return &#34;&#34;;
}
else
{
client.goto_url(&#34;/cms/list&#34;, 3, &#34;删除出错(error)&#34;);
return &#34;&#34;;
}
return &#34;&#34;;
}
}更看官方 controller 例子 |
|