ServiceKit
ServiceKit is a minimalist foundation for building web services in Swift, micro or otherwise.
A super simple example:
import ServiceKit
let service = Service() { req, res in
res.write("Sup, nerds?")
}
service.listen()
Getting Started
Getting set up is pretty quick. Make a folder for your project and start a new Swift Package:
swift package init --name DemoService --type executable
Then, update your Package.swift
file. If you’re new to Swift this is the main manifest file
for your project. Similar to package.json
in Node, for example.
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "DemoService",
dependencies: [
// Dependencies declare other packages that this package depends on.
// This is the magic line.
.package(url: "https://github.com/cbaltzer/ServiceKit", from: "0.1.0"),
],
targets: [
.target(
name: "DemoService",
dependencies: ["ServiceKit"]), // Tell the linker to actually connect our dependency
.testTarget(
name: "DemoServiceTests",
dependencies: ["DemoService"]),
]
)
The initializer for your package should have provided a main.swift
file. Fill it out with
a Hello World sample to make sure everything works:
import ServiceKit
let demo = Service() { req, res in
res.write("Hi, it works!")
}
demo.listen()
Now finally to launch your new service:
swift run
Check it out at http://localhost:5000/
Environment Setup
Xcode (Mac)
This is the way to go if you’re on a Mac. Installing Xcode from the Mac App Store will include the Swift toolchains.
Editing is also easiest with Xcode:
swift package generate-xcodeproj
VSCode (Mac, Linux, Windows)
Getting a Swift dev environment set up on any platform is made pretty easy with the Remote - Containers extension. This will launch a Docker container with the specified environment, including toolchains and other extensions.
Check out the .devcontainer folder for reference.
Deploying
Deploying is also easiest with Docker. Check out the Dockerfile for a basic example.