![Fire Image] (http://i.imgur.com/1qR6Nl4.png)
An extremely fast Swift web framework
If you're familiar with express.js then Blackfire will be known to you. The most simple example of how to use can be seen below:
main.swift
import Blackfire
// Create a nice new 🔥 app for us to play with.
let app = Flame()
// Let's add a route to begin with.
app.get("/") { (req, res) in
res.send(text: "Hello World")
}
app.start(port: 3000) { result in
switch result {
case .success:
print("Server started on port 3000")
case .failure(let error):
print("Server failed with error: \(error)")
}
}
$ curl localhost:3000
Hello World%
Blackfire has all the standard features of a typical minimal Web Framework, let's take a look at some of these.
Routing, as seen in the above example, takes place by assigning a handler to a method in your App
app.get("/") { (req, res) in
res.send(text: "I'm a GET request")
}
app.post("/users") { (req, res) in
res.send(text: "I'm a POST request to /users")
}
app.delete("/all/files") { (req, res) in
res.send(text: "I'm a DELETE request to /all/files ...wait")
}
app.put("/em/up") { (req, res) in
res.send(text: "I'm a PUT request to /em/up Am I being robbed?")
}
This can become tedious if you have a lot of /users/<something>
routes however, so we created the........
Don't be scared that it's a monkey handling it, he had a pretty decent job interview on the whiteboard and seems to be doing ok.
The router object allows you to group routes together. For example
let users = Router()
users.get("/") { req, res in
res.send(text: "Get me all the users")
}
users.post("/") { req, res in
res.send(text: "Creating a new user")
}
users.get("/favourites") { req, res in
res.send(json: ["food": "🍌"])
}
// Let's use the router to match for /users
app.use("/users", users)
$ curl localhost:3000/users
Get me all the users%
$ curl localhost:3000/users/favourites
{"food":"🍌"}%
Powerful stuff.
The request or req
object contains a bunch of helpful information that your handler may want to use:
These include:
request.params
A key value pair ofStrings
that are matched in the routerequest.body
The raw body of the recieved request, in the form of aString
request.headers
A key value pair ofStrings
of the headers that appeared in the routerequest.method
The method of this request, formed from theHTTPMethod
enum.request.path
The path of this requesthttpProtocol
The protocol for this request.
The response or res
object contains everything you need to return data back to the consumer
res.send(text: String)
Send back a basic text response in the form ofContent-Type: text/plain
res.send(json: Any)
Send back some JSON, takes in a JSON parseable object. This method can fail if the object is not parseableres.send(status: Int)
Send back a HTTP status with no bodyres.send(html: String)
Send back some html with the header ofContent-Type: text/html
res.send(error: String)
Sends back an error, setting the status to500
.res.headers
Set some headers to send back to the client