A Vapor-based REST API server for syncing chats with the FMChats iOS/macOS application.
- RESTful API for chat synchronization
- In-memory storage (easily replaceable with database)
- CORS support for web clients
- ISO8601 date encoding for cross-platform compatibility
- Comprehensive logging
- Swift 6.0+
- macOS 14.0+
- Vapor 4.99.0+
- Clone or navigate to this directory:
cd /Users/rob/Projects/Swift/FMChatsServer- Resolve dependencies:
swift package resolve- Build the project:
swift buildRun the server directly:
swift runThe server will start on http://localhost:8080
Build for release:
swift build -c releaseRun the release binary:
.build/release/FMChatsServerGET /healthReturns server health status.
Response:
{
"status": "ok",
"service": "FMChatsServer",
"timestamp": "2024-02-10T12:00:00Z"
}GET /chatsReturns all chats sorted by timestamp (newest first).
Response:
[
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Sample Chat",
"timestamp": "2024-02-10T12:00:00Z",
"questions": [...]
}
]POST /chats
Content-Type: application/jsonRequest Body:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "New Chat",
"timestamp": "2024-02-10T12:00:00Z",
"questions": [
{
"id": "660e8400-e29b-41d4-a716-446655440001",
"questionText": "What is Swift?",
"answerText": "Swift is a programming language...",
"timestamp": "2024-02-10T12:01:00Z"
}
]
}Response: Returns the uploaded chat (same format as request).
GET /chats/:chatIdResponse: Returns the requested chat or 404 if not found.
DELETE /chats/:chatIdResponse: Returns 204 No Content on success.
# Health check
curl http://localhost:8080/health
# Get all chats
curl http://localhost:8080/chats
# Upload a chat
curl -X POST http://localhost:8080/chats \
-H "Content-Type: application/json" \
-d '{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Test Chat",
"timestamp": "2024-02-10T12:00:00Z",
"questions": []
}'
# Get specific chat
curl http://localhost:8080/chats/550e8400-e29b-41d4-a716-446655440000
# Delete chat
curl -X DELETE http://localhost:8080/chats/550e8400-e29b-41d4-a716-446655440000FMChatsServer/
├── Package.swift
├── README.md
├── Sources/
│ └── FMChatsServer/
│ ├── FMChatsServer.swift # Main entry point
│ ├── configure.swift # App configuration
│ ├── Models/
│ │ └── ChatDTO.swift # Data models
│ └── Controllers/
│ └── ChatController.swift # API endpoints
The server runs on port 8080 by default. To change it, modify configure.swift:
app.http.server.configuration.port = 8080CORS is configured to allow all origins. For production, modify configure.swift:
let corsConfiguration = CORSMiddleware.Configuration(
allowedOrigin: .custom("https://yourdomain.com"),
allowedMethods: [.GET, .POST, .PUT, .DELETE],
allowedHeaders: [.accept, .authorization, .contentType, .origin]
)Replace in-memory storage with a database:
- Add Fluent dependency to
Package.swift:
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0"),- Create database models and migrations
- Update
ChatControllerto use database queries
- Add authentication middleware (JWT or session-based)
- Use HTTPS with SSL/TLS certificates
- Implement rate limiting
- Add input validation
- Set up environment variables for secrets
- Vapor Cloud: Official Vapor hosting platform
- Heroku: Easy deployment with buildpacks
- AWS: EC2, ECS, or Lambda
- Docker: Containerize for any cloud provider
- Create a new controller in
Controllers/ - Register it in
configure.swift:
try app.register(collection: YourController())The server uses Vapor's built-in logging:
req.logger.info("Your message")
req.logger.warning("Warning message")
req.logger.error("Error message")lsof -ti:8080 | xargs kill -9swift package clean
swift package resolve
swift build- Ensure server is running
- Check firewall settings
- Verify iOS app is using correct URL (http://localhost:8080)
- For iOS simulator, localhost should work
- For physical device, use your Mac's IP address
This project is part of the FMChats application.