使用FastDo的异步任务功能编写的一个简易HTTP服务器,作为FastDo内置服务器的探索。
- 安装 Microsoft Visual C++ 2017 x64
- 安装 FastDo v0.6.4+
- 安装 GCC for C++ 4.8+
- 安装 FastDo v0.6.4+
安装FastDo可以查看官方网站作为C++库安装教程。
注意,由于FastDo目前采取的是二进制发布,所以Windows上编译器版本应该要对应上才能编译使用。
// 初始化Socket库
SocketLib initSock;
// 创建App,App需要传递一个配置对象,可以设置很多选项,一个空配置对象表示全部采用默认值
HttpApp app{ ConfigureSettings{}, nullptr };
// 注册过径路由,过径路由是指URL只要路过就会触发,并且你可以控制是否继续。
// 第一个参数表示HTTP方法,"*"表示通配所有HTTP方法,也可以是逗号分隔的方法列表串,如:GET,POST,PUT
// 第二个参数表示路径,"/"是每一个URL都会过径的路径
app.crossRoute( "*", "/", [] ( auto clientCtx, Response & rsp, auto parts, auto i ) -> bool {
// 执行一些操作
return true; // 返回true表示继续查找并执行路由处理,返回false表示终止路由过程
} );
// 注册普通路由
// 第一个参数表示HTTP方法,"*"表示通配所有HTTP方法,也可以是逗号分隔的方法列表串,如:GET,POST,PUT
// 第二个参数表示路径
app.route( "GET,POST", "/hello", [] ( auto clientCtx, Response & rsp ) {
rsp << "Hello, SimpleHttpServer!\n";
} );
// 运行服务
app.run(nullptr);
app.route( "GET", "/json", [] ( auto clientCtx, Response & rsp ) {
Mixed v = $c{
{ "status", "Running..." },
{ "error", 0 }
};
rsp << v;
} );
app.route( "GET,POST", "/vars", [] ( auto clientCtx, Response & rsp ) {
Request & req = rsp.request;
rsp << "GET: a = " << req.get["a"] << endl;
rsp << "POST: b = " << req.post["b"].toInt() << endl;
rsp << "COOKIES: c = " << req.cookies["c"] << endl;
} );
app.route( "POST", "/upload", [] ( auto clientCtx, Response & rsp ) {
Request & req = rsp.request;
Mixed & myfile = req.post["myfile"];
if ( myfile["path"] )
{
// 从临时路径拷贝到自定义的路径
FilePutContentsEx( CombinePath( "my-uploads", myfile["name"] ), FileGetContentsEx( myfile["path"], false ), false );
rsp << "Upload ok!";
// 删除临时文件
CommonDelete(myfile["path"]);
}
} );
app.route( "*", "/database", [] ( auto clientCtx, Response & rsp ) {
Mixed dbConfig = $c{
{ "driver", "mysql" },
{ "host", "localhost" },
{ "user", "root" },
{ "pwd", "123" },
{ "dbname", "test" },
{ "charset", "utf-8" }
};
Database db{dbConfig};
auto rs = db->query("select * from mytable1");
Mixed row;
while ( rs->fetchRow(&row) )
{
rsp << row["field1"] << ", " << row["field2"] << ", " << row["field3"] << "<br/>\n";
}
} );