httpbeastNim 实现的高性能多线程 HTTP 服务器

联合创作 · 2023-10-01

httpbeast 是使用 Nim 语言编写的高性能、多线程 HTTP 1.1 服务器。

主要特性

  • 基于 Nimselectors模块构建,可以有效地利用 Linux 上的 epoll 和 macOS 上的 kqueue
  • 自动并行化,只需确保编译时添加--threads:on参数即可
  • 支持 HTTP pipelining
  • 按需使用的解析器,可用于仅解析请求的数据
  • 与 Nim 的集成asyncdispatch支持在必要时在请求回调中使用 async/await

示例代码

创建helloHttp.nimble文件:

# Package

version       = "0.1.0"
author        = "Your Name"
description   = "Your Description"
license       = "MIT"
srcDir        = "src"
bin           = @["helloHttp"]


# Dependencies

requires "nim >= 1.0.0"
requires "httpbeast >= 0.3.0"

创建src/helloHttp.nim文件:

import options, asyncdispatch

import httpbeast

proc onRequest(req: Request): Future[void] =
  if req.httpMethod == some(HttpGet):
    case req.path.get()
    of "/":
      req.send("Hello World")
    else:
      req.send(Http404)

run(onRequest)

通过nimble c -r helloHttp.nim运行。

浏览 8
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报