Skip to content

coenttb/swift-server-foundation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swift-server-foundation: Server Development Foundation for Swift

swift-server-foundation is a comprehensive server-side Swift library that consolidates essential packages for building robust, scalable server applications. Rather than managing multiple dependencies, this foundation provides unified access to authentication, database connectivity, rate limiting, logging, routing, and server infrastructure tools.

Included Libraries

This foundation re-exports the following specialized packages, organized by domain:

Type Safety & Domain Modeling

  • EmailAddress - Domain-accurate and type-safe email address handling that adheres to web standards
  • Domain - Type-safe domain model consistent with web standards for reliable domain handling
  • JWT - Standards-compliant JSON Web Token creation, signing, and verification using Apple's Crypto framework
  • PasswordValidation - Robust password validation with customizable security policies

Authentication & Security

  • Crypto - Apple's Swift Crypto framework for cryptographic operations, hashing, and secure key management
  • Throttling - Flexible rate limiting and throttling implementation to protect APIs from abuse and ensure fair usage

Database & Persistence

  • PostgresKit - PostgreSQL database driver with async/await support for reliable data persistence
  • Database Configuration - Built-in utilities for managing database connections and configurations

Server Infrastructure

  • AsyncHTTPClient - Non-blocking HTTP client built on SwiftNIO for making external API calls
  • Logging - Apple's unified logging API for structured, configurable application logging
  • Event Loop Management - Connection pooling and event loop group utilities for optimal performance

Environment & Configuration

  • EnvironmentVariables - Type-safe environment variable handling with validation and defaults
  • Environment Variables Test Support - Testing utilities for environment-dependent code

Date & Time Handling

  • DateParsing - Comprehensive date parsing for RFC 2822, RFC 5322, and Unix epoch timestamps with robust error handling
  • FoundationExtensions - Powerful extensions for date manipulation, validation, and formatting with intuitive operations like date + 1.day

Routing & Translation

  • URLRouting - Point-Free's powerful URL routing library for type-safe navigation
  • URLRoutingTranslating - Internationalization support for routing with multi-language URL patterns
  • Translating - Comprehensive translation and localization framework

Utilities & Infrastructure

  • Builders - Result builders for creating collections and content with declarative, SwiftUI-like syntax
  • Dependencies - SwiftUI Environment-inspired dependency management for controllable and testable applications
  • CasePaths - Key path-like functionality for enum cases
  • IssueReporting - Runtime issue reporting and debugging utilities

Usage

Instead of importing multiple individual packages:

import AsyncHTTPClient
import PostgresKit
import JWT
import Throttling
import Logging
import EnvironmentVariables
// ... and many more

Simply import the foundation:

import ServerFoundation

// All server functionality is now available:
let logger = Logger(label: "com.example.app")
let jwt = try JWT.signed(/* ... */)
let client = HTTPClient(eventLoopGroupProvider: .shared)
let rateLimiter = RateLimiter(rate: 100, per: .minute)
let dbConfig = try DatabaseConfiguration.live

Installation

To use swift-server-foundation in your project, add it to your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/coenttb/swift-server-foundation.git", exact: "0.0.1")
]

Environment Variables Support

The package includes ServerFoundationEnvVars for environment-specific configuration:

import ServerFoundationEnvVars

// Access environment variables with type safety
let port = EnvVars.PORT.value ?? 8080
let databaseUrl = EnvVars.DATABASE_URL.require()

Related projects

Boiler & coenttb

  • boiler: A minimal Swift web framework for building type-safe servers
  • swift-web-foundation: Web development foundation library with HTML, CSS, and form handling
  • coenttb-com-server: The backend server for coenttb.com, written entirely in Swift and powered by this foundation

Feedback is Much Appreciated!

If you're working on your own Swift server project, feel free to learn, fork, and contribute.

Got thoughts? Found something you love? Something you hate? Let me know! Your feedback helps make this project better for everyone. Open an issue or start a discussion—I'm all ears.

Subscribe to my newsletter

Follow me on X

Link on Linkedin

License

This project is licensed under the Apache 2.0 License. See the LICENSE.

About

A Swift library for tools to simplify server development

Topics

Resources

License

Stars

Watchers

Forks

Languages