Apache 的 Swift API。它封装了 CApache 系统包,并在其基础上提供了 Swift 的便捷性。此包是 mod_swift 项目的一部分。
Apache 提供了非常底层的 API,如果需要更方便的 API,请查看 ApacheExpress。
注意:这需要安装 mod_swift。如果没有安装,CApache 将无法构建!
如果您从头开始设置新模块,请使用
swift apache init
否则,请设置您的 Package.swift 以包含 Apache
import PackageDescription
let package = Package(
name: "MyTool",
dependencies: [
.Package(url: "git@github.com:modswift/Apache.git", from: "0.5.0"),
]
)
只需调用
swift apache build
这将封装 Swift Package Manager 来构建您的包,然后生成可以加载到 Apache 中的适当模块。
要运行测试 Apache 实例,请使用
swift apache serve
使用以下地址访问 Apache:https://:8042/。
查看 mods_baredemo
以了解使用此包 API 的底层 Apache 模块。
一个简单的 Apache 处理程序
func HelloHandler(p: UnsafeMutablePointer<request_rec>?) -> Int32 {
let req = ApacheRequest(raw: p!)
guard req.handler == "helloswift" else { return DECLINED }
req.puts("Hello World!")
return OK
}
一个使用数据库的 Apache 处理程序
func DatabaseHandler(p: UnsafeMutablePointer<request_rec>?) -> Int32 {
let req = ApacheRequest(raw: p!)
guard req.handler == "dbswift" else { return DECLINED }
guard let dbd = req.dbdAcquire() else {
req.puts("Could not access database!")
return 500
}
dbd.select("SELECT name, count FROM pets") { (name : String, count : Int?) in
req.puts("\(name): \(count)\n")
}
return OK
}
两者的 Apache 配置
<Location /helloworld>
SetHandler helloswift
</Location>
<Location /database>
SetHandler dbswift
DBDriver sqlite3
DBDParams "absolute-path-to-testdb.sqlite3"
</Location>
mod_swift 由 ZeeZide 提供。我们欢迎反馈、GitHub 星星、酷炫的合同工作,以及任何您能想到的赞扬形式。
在 Noze.io Slack 上有一个 #mod_swift
频道。