Perfect: Server-Side Swift Deployment and Usage Guide
Prerequisites
- Swift 5.2 or later
- macOS or Linux (Ubuntu recommended for Linux deployments)
- Xcode 11 or later (for macOS development)
- Git for cloning the repository
Installation
1. Clone the Repository
git clone https://github.com/PerfectlySoft/Perfect.git
cd Perfect
2. Build the Project
Using Xcode (macOS)
# Open the project in Xcode
open Perfect.xcodeproj
Using Swift Package Manager (macOS/Linux)
# Build the project
swift build
# Run tests
swift test
3. Install Additional Dependencies
Perfect has several optional modules that can be installed based on your needs:
# Install Perfect-HTTPServer
swift package update PerfectHTTPServer
# Install Perfect-SQLite
swift package update PerfectSQLite
# Install Perfect-PostgreSQL
swift package update PerfectPostgreSQL
Configuration
Environment Variables
Perfect doesn't require specific environment variables by default, but you may need to configure:
- Database connections: Set connection strings for your database of choice
- Server ports: Configure the HTTP server port (default is 8181)
- SSL certificates: For HTTPS deployments
Configuration Files
Create a configuration file for your application:
// Config.swift
public struct Config {
static let serverPort: Int = 8181
static let databaseURL: String = "sqlite://./database.db"
}
Build & Run
Development Server
# Build and run the development server
swift run PerfectTemplate
# Or build first, then run
swift build
.build/debug/PerfectTemplate
Production Build
# Build for release
swift build -c release
# Run the production server
.build/release/PerfectTemplate
Testing
# Run all tests
swift test
# Run specific tests
swift test --filter "PerfectLibTests"
Deployment
Platform Options
Perfect applications can be deployed to:
- AWS (using Perfect Assistant)
- Google Cloud Platform
- Any Swift-compatible Linux server
- Docker containers
Docker Deployment
# Dockerfile
FROM swift:5.2
WORKDIR /app
COPY . .
RUN swift build -c release
EXPOSE 8181
CMD [".build/release/PerfectTemplate"]
Build and run:
docker build -t perfect-app .
docker run -p 8181:8181 perfect-app
AWS Deployment with Perfect Assistant
- Download Perfect Assistant from perfect.org
- Create a new project
- Configure AWS credentials
- Deploy to AWS EC2 or Lambda
Troubleshooting
Common Issues and Solutions
1. Swift Version Compatibility
Issue: Build fails due to Swift version mismatch Solution: Ensure you're using Swift 5.2 or later
swift --version
2. Missing Dependencies
Issue: Compilation errors for missing modules Solution: Update your Swift Package dependencies
swift package update
3. Port Already in Use
Issue: Server fails to start on port 8181 Solution: Change the server port in your configuration
// Change port number
Config.serverPort = 8080
4. Database Connection Issues
Issue: Unable to connect to database Solution: Verify database connection strings and permissions
// Check your database configuration
let db = try Database(Config.databaseURL)
5. File Permissions on Linux
Issue: Permission denied when accessing files Solution: Ensure proper file permissions
chmod +x /path/to/your/file
Debug Mode
Enable debug logging for troubleshooting:
// In your main.swift
Log.debug("Debug mode enabled")
Log.info("Application starting...")
Performance Monitoring
Use Perfect's built-in monitoring tools:
// Monitor request times
let startTime = Date()
// Your request handling code
let duration = Date().timeIntervalSince(startTime)
Log.info("Request completed in \(duration) seconds")
This guide provides a comprehensive overview of deploying and using Perfect for server-side Swift development. For more detailed documentation, visit the Perfect documentation repository.