Simple Web Server
Build a long-running HTTP server on WendyOS using Hummingbird
Building a Web Server with Hummingbird
Often times you'll want a long-running server where you can make HTTP or WebSocket calls to your WendyOS device. This allows your device to accept incoming requests and respond to them, making it easy to build interactive applications or APIs that can be accessed from other devices on your network.
To prove this out, we'll use Hummingbird version 2.17.0, a lightweight and flexible HTTP server framework for Swift.
Setting Up Your Project
Create a New Directory
First, create a directory for your project:
mkdir simple-web-server
cd simple-web-serverInitialize the Project
Run the Wendy CLI initialization command:
wendy initThis creates a new Swift project structure with a main.swift file.
Configure Package Dependencies
Update your Package.swift file to include Hummingbird as a dependency:
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "simple-web-server",
platforms: [
.macOS(.v15), // a requirement for compiling Hummingbird on macOS
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.17.0"),
],
targets: [
.executableTarget(
name: "simple-web-server",
dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
]
),
]
)Implementing the Web Server
Replace the contents of Sources/simple-web-server/main.swift with the following code:
import Hummingbird
// Create a router and add a simple route
let router = Router()
router.get("/") { request, context in
"Hello, World!"
}
// Create and configure the application
let app = Application(
router: router,
configuration: .init(address: .hostname("0.0.0.0", port: 9080))
)
// Run the server
try await app.run()Important: It's important that your Hummingbird server is running on 0.0.0.0 and not localhost or 127.0.0.1. This is because your WendyOS device is not on the same network as your development machine. You'll need to access your server from the outside world, and 0.0.0.0 is the same as localhost but it's accessible from the outside world.
Running Your Server
You can run your web server using either method:
Using Swift Directly
swift runUsing Wendy CLI
wendy runOnce the server is running, you should see output indicating the server has started. You can then access it by opening a web browser or using curl:
curl http://localhost:9080You should see the response:
Hello, World!Understanding the Code
Let's break down what the code does:
-
Import Hummingbird: We import the Hummingbird framework to access its HTTP server functionality.
-
Create a Router: The router handles incoming HTTP requests and routes them to the appropriate handler.
-
Add a Route: We define a simple GET route at the root path
/that returns "Hello, World!". -
Configure the Application: We create an Application instance with:
- Our router
- A configuration that binds to
0.0.0.0on port9080
-
Run the Server: The
app.run()method starts the server and keeps it running.
Test Your Server
After deploying your server to your WendyOS device, you can test it from your Macbook.
Open up your browser and navigate to:
http://wendyos-device.local:9080You should see the following output:
Hello, World!This confirms your web server is successfully running on your WendyOS device and accepting requests from your network.
Verifying Deployment
You can also verify the server is running by listing the applications on your device:
wendy device apps listLearn More
Hummingbird is a fantastic lightweight web framework for Swift. Learn more by visiting https://hummingbird.codes/ and exploring the examples to make your server much more advanced.
Next Steps
Now that you have a basic web server running:
- Add more routes to handle different endpoints
- Implement POST, PUT, and DELETE routes for a full REST API
- Connect to WendyOS device features to control hardware via HTTP
- Add WebSocket support for real-time communication
- Explore Hummingbird's middleware and authentication features
- Check out Hummingbird Examples for a plethora of comprehensive examples