Plugins
Working with SwiftX's plugin-driven architecture.
Plugins
Everything in SwiftX is a plugin. Even core functionality like CORS and static file serving is implemented as separate plugins. This architecture allows you to customize the engine for your exact needs.
Anatomy of a Plugin
A SwiftX plugin is a class that conforms to the SwiftXPlugin protocol.
public protocol SwiftXPlugin {
func boot(app: SwiftXApp)
func onStart()
func onStop()
}Official Plugins
SwiftX comes with a few official plugins to help you get started.
CORS Plugin
Allows you to control cross-origin requests.
import SwiftX
let app = SwiftXApp()
app.use(CorsPlugin(allowedOrigins: ["*"]))Static Files Plugin
Serves files from a local directory.
import SwiftX
let app = SwiftXApp()
app.use(StaticFilesPlugin(at: "/public", from: "public"))Creating Your Own Plugins
You can create your own plugins to add custom behavior to the SwiftX engine.
Define the Plugin Class
Create a new class that conforms to SwiftXPlugin.
import SwiftX
public class MyCustomPlugin: SwiftXPlugin {
public func boot(app: SwiftXApp) {
// Register routes, middlewares, etc.
app.get("/custom") { _, _ in .text("Built-in with my plugin!") }
}
public func onStart() { print("Plugin started") }
public func onStop() { print("Plugin stopped") }
}Register the Plugin
Add it to your app instance.
let app = SwiftXApp()
app.use(MyCustomPlugin())
app.start()Sharing Plugins
Since SwiftX is 100% plugin-driven, you can package your plugins as separate Swift Packages and share them with the world.
dependencies: [
.package(url: "https://yourgithub.com/SwiftXLoggerPlugin.git", from: "1.0.0")
]