How swiftfiddle/swiftfiddle-formatter works
Project Type and Purpose
• From the README (“SwiftFiddle Formatter is a versatile playground for experimenting with swift-format … right in your browser”) and the file tree, this is a full-stack web application.
• The frontend is a single-page application (SPA) built with plain JavaScript, CSS/SCSS, HTML, and bundled by Webpack.
• The backend is a Swift-based web server (likely using Vapor or a similar Swift HTTP framework), which exposes REST endpoints to invoke the Swift formatting engine.
High-Level File Structure Analysis
• Public/
• Static web assets (index.html, JS modules, CSS/SCSS, images, favicons)
• webpack.{common,dev,prod}.js and package.json/lock.json at root manage JS build pipelines.
• Sources/App/
• entrypoint.swift, configure.swift, routes.swift define server startup, routing, middleware registration.
• Middlewares/ contains reusable middleware (error handling, custom headers).
• Resources/formatter/
• A Swift package that wraps or embeds the official swift-format library.
• Compiled into the application or invoked as a subprocess to perform code formatting.
• Dockerfile, fly.toml, .github/workflows/…
• Containerization and CI/CD definitions for testing, deployment, and security analysis.
Key Architectural Insights from README & Code
• Single endpoint (e.g., POST /format) receives Swift source + config → returns formatted code.
• No persistent storage; this is stateless and can be horizontally scaled.
• Uses SwiftPM to build both the server and the embedded formatter.
• Frontend communicates over HTTP(S) with the Swift server API.
Components
a. Client Layer (Browser)
• Editor UI (Public/index.html + JS modules: editor.js, defaults.js, configuration.js, etc.)
b. CDN / Static Assets Server
• Serves the bundled JS/CSS/HTML (could be same Vapor server or a separate static host)
c. Application Server (Swift/Vapor)
• HTTP listener (entrypoint.swift)
• Routing (routes.swift)
• Middleware chain (CommonErrorMiddleware, CustomHeaderMiddleware)
• FormatController (handles /format requests)
d. Formatter Engine
• Embedded swift-format library (Resources/formatter)
• Invoked as an in-process call or subprocess
e. CI/CD & Infrastructure
• Docker container orchestration (Dockerfile, fly.toml for Fly.io)
• GitHub Actions (test.yml, deploy.yml, codeql-analysis.yml)
• Optional: Load balancer, HTTPS termination
f. External Services
• GitHub (repo, sponsors)
Interaction Flows & Relationships
• User types Swift code in the browser editor → clicks “Format.”
• Browser sends HTTP POST /format with payload { code, config } → Application Server.
• Middleware validates request and appends headers.
• Route handler calls Formatter Engine → returns formatted code or an error.
• Server responds with JSON { formattedCode } → Browser.
• UI displays the formatted code or error in result_view.js/snackbar.js.
Architectural Patterns and Principles
• SPA + REST API: clear separation of concerns between presentation and business logic
• Stateless server: no database, easy horizontal scaling
• Middleware pipeline for cross-cutting concerns (error handling, headers)
• Containerized deployment ensures environment parity
Final Checklist
• All components labeled (Client, CDN/Static Host, Server, Engine, CI/CD, External)
• Notes on important middleware and entry points (e.g., entrypoint.swift, routes.swift)