HTTP Server for IoT
sudo apt install libz-dev
ZenNIO primarily uses SwiftPM as its build tool, so we recommend using that as well. If you want to depend on ZenNIO in your own project, it's as simple as adding a dependencies clause to your Package.swift:
dependencies: [
.package(url: "https://github.com/gerardogrisolini/ZenNIO.git", from: "2.5.2")
]
and then adding the appropriate ZenNIO module(s) to your target dependencies.
import ZenNIO
let server = ZenNIO()
server.addDocs("/Library/WebServer/Documents")
server.addCORS()
server.addAuthentication(handler: { (email, password) -> String in
if email == "admin" && password == "admin" {
return "uniqueId"
}
return ""
})
server.setFilter(true, methods: [.POST], url: "/*")
let router = ZenIoC.shared.resolve() as Router
router.get("/hello.html") { req, res in
res.send(html: "<html><body><h1>Hello World!</h1></body></html>")
res.success()
}
router.get("/hello.txt") { req, res in
res.send(text: "Hello World!")
res.success()
}
router.get("/hello/:name") { req, res in
guard let name: String = req.getParam("name") else {
return res.failure(.badRequest("parameter name"))
}
do {
let json = [
"ip": req.clientIp,
"message": "Hello \(name)!"
]
try res.send(json: json)
res.success()
} catch {
res.failure(.internalError(error.localizedDescription))
}
}
try server.start()
try server.startSecure(
certFile: "certificate.crt",
keyFile: "private.pem",
http: .v2
)
link |
Stars: 1 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics