文章目录

  • 一、Oatpp 编译
  • 下载源码
  • cmake构建工程
  • Configue配置x64
  • Generate构建项目
  • VS2019编译项目
  • 二、Oatpp搭建http服务器
  • VS2019创建空项目
  • 导入oatpp项目配置


一、Oatpp 编译

下载源码

下载源码:https://github.com/oatpp/oatpp

git拉取或者下载压缩包都可,我是在2023.8.29日拉的最新代码编译运行的

cmake构建工程

下载的 oatpp-master 文件夹里面建立两个文件夹 x64 和 win32 ,这两个文件夹用来作为工作目录。

Oatpp编译使用Windows版本----windows搭建http服务器_windows

运行 CMake (cmake-gui)

Oatpp编译使用Windows版本----windows搭建http服务器_ide_02

添加项目路径和 build 目录

Oatpp编译使用Windows版本----windows搭建http服务器_ide_03

Configue配置x64

点击Configure按钮

我用的是VS2019,项目,配置选择x64其他默认

Oatpp编译使用Windows版本----windows搭建http服务器_服务器_04

选择x64 , 这样编译出来的就是x64版本的软件。如果要编译win32版本的,就选择Win32

点击Finish按钮,回到CMake主窗口

Oatpp编译使用Windows版本----windows搭建http服务器_http服务器_05

在点击finish的时候已经在Configure了,出现上图的Configureing done即可

Generate构建项目

继续点击Generator

Oatpp编译使用Windows版本----windows搭建http服务器_ide_06

出现 “Generatoring done” 即可

VS2019编译项目

打开x64文件夹就会看到里面已经产生了Visual Studio的解决方案

Oatpp编译使用Windows版本----windows搭建http服务器_服务器_07

双击 oatpp.sln 文件,启动Visual Studio

Oatpp编译使用Windows版本----windows搭建http服务器_服务器_08

编译oatpp生成oatpp.lib库

Oatpp编译使用Windows版本----windows搭建http服务器_#include_09

编译完成

Oatpp编译使用Windows版本----windows搭建http服务器_ide_10

文件夹 D:\msys64\home\xian-3\github\oatpp-master\x64\src\Debug 里面的oatpp.lib文件,就是我们需要的库文件,它是Debug x64版本的,可以在VS编译的时候选择Release版本,或者 cmake configue 的时候选择win32版本。

文件夹 D:\msys64\home\xian-3\github\oatpp-master\src 里面就是头文件目录

二、Oatpp搭建http服务器

VS2019创建空项目

创建一个空项目,添加测试代码main.cpp和handler.h

main.cpp

// main.cpp
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/network/Server.hpp"
#include "handler.h"
#pragma comment(lib,"oatpp.lib")
#pragma comment(lib,"oatpp-test.lib")
void run()
{
    // 为 HTTP 请求创建路由器
    auto router = oatpp::web::server::HttpRouter::createShared();

    // 路由 GET - "/hello" 请求到处理程序
    router->route("GET", "/hello", std::make_shared<Handler>());

    // 创建 HTTP 连接处理程序
    auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);

    // 创建 TCP 连接提供者
    auto connectionProvider = oatpp::network::tcp::server::ConnectionProvider::createShared({ "0.0.0.0", 8080, oatpp::network::Address::IP_4 });

    // 创建服务器,它接受提供的 TCP 连接并将其传递给 HTTP 连接处理程序
    oatpp::network::Server server(connectionProvider, connectionHandler);

    // 打印服务器端口
    OATPP_LOGI("MyApp", "Server running on port %s", connectionProvider->getProperty("port").getData());

    // 运行服务器
    server.run();
}

int main()
{
    // 初始化 oatpp 环境
    oatpp::base::Environment::init();

    // 运行应用
    run();

    // 销毁 oatpp 环境
    oatpp::base::Environment::destroy();

    return 0;
}

handler.h

// handler.h
#ifndef HANDLER_H
#define HANDLER_H

#include "oatpp/web/server/HttpRequestHandler.hpp"

#define O_UNUSED(x) (void)x;

// 自定义请求处理程序
class Handler : public oatpp::web::server::HttpRequestHandler
{
public:
    // 处理传入的请求,并返回响应
    std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override {
        O_UNUSED(request);

        return ResponseFactory::createResponse(Status::CODE_200, "Hello, World!");
    }
};

#endif // HANDLER_H

导入oatpp项目配置

配置头文件目录

Oatpp编译使用Windows版本----windows搭建http服务器_windows_11

配置库目录

Oatpp编译使用Windows版本----windows搭建http服务器_windows_12

添加库

Oatpp编译使用Windows版本----windows搭建http服务器_windows_13

oatpp-test.lib
oatpp.lib
wsock32.lib
ws2_32.lib

ps: wsock32.lib ws2_32.lib这两个库没加一直报错,翻了好久博客多次测试才找到原因,要不是好几次粉丝找我windows搭建http服务器我都不想写这篇博客

编译运行,postmain验证

Oatpp编译使用Windows版本----windows搭建http服务器_ide_14

第一次运行会弹窗是否允许网络访问

over,下课