A tutorial for building a server-side TODO management application using Swift.
SwiftServerTodos demonstrates building an HTTP service in Swift. This repository contains a tutorial and code samples for creating a backend service using Vapor, OpenAPI, and PostgreSQL.
Contents:
SwiftServerTodos.docc
: DocC tutorial with step-by-step instructions.<root directory>
: Starter code for tutorial exercises.Completed
: Completed implementation for reference.
The tutorial covers these Swift server development topics:
- HTTP service implementation with Vapor.
- Code generation from an OpenAPI document.
- Database persistence using the Fluent ORM and PostgreSQL.
- Service configuration and lifecycle management.
- Swift 6.1 or later
- Git
- Docker Compose
git clone https://github.com/swiftlang/swift-server-todos-tutorial
cd SwiftServerTodos
To preview the tutorial locally:
xcrun docc preview -p 8090
The tutorial is available at http://localhost:8090/tutorials/getting-started-swift-server
.
- Open the DocC tutorial to read concepts and implementation details.
- Use the root directory as the starting package for the tutorial.
- Reference the package in the
Completed/
directory for the final implementation. - Modify
Package.swift
and files inSources/
as you progress through the tutorial steps.
The application uses a three-tier architecture:
- HTTP Layer: Vapor web framework for HTTP request handling.
- Business Logic: OpenAPI-generated handlers for request processing.
- Data Layer: Fluent ORM for database operations with PostgreSQL.
- Vapor – Swift web framework.
- Fluent ORM – Database ORM and migrations.
- Swift OpenAPI Generator – Code generation from OpenAPI specs.
- Swift Log – Logging framework.
- ServiceLifecycle – Service lifecycle management.
The service implements a RESTful API for TODO management:
GET /todos
– Retrieve all todos.POST /todos
– Create a new todo.GET /todos/{id}
– Retrieve a specific todo.PUT /todos/{id}
– Update a todo.DELETE /todos/{id}
– Delete a todo.
The complete API specification is available in Sources/SwiftServerTodos/openapi.yaml
.
- Hummingbird – Alternative Swift web framework.
- Swift GRPC – gRPC framework.
Verify the tutorial builds without errors or warnings using the command:
xcrun docc convert --warnings-as-errors --analyze
Preview the tutorial content. Run the following command,
then review your updates at http://localhost:8090/tutorials/getting-started-swift-server
:
xcrun docc preview -p 8090