main.swift is always contained in an executable target and cannot be imported by other modules:
// File: /Sources/Run/main.swift
import App // [1]
try app(.detect()).run() // [2]
The preceding code does two things:
- The main.swift file imports the App module
- It gets an app instance from the app() constructor and calls the run() function of app to launch the server
Vapor uses an Application instance, app, in every project to run a server and create other services. The instance is obtained from the app() function implemented in app.swift. Vapor avoids statically accessing the Application instance using this approach. It has no need to implement any locking mechanism for thread-safety that is required for static access to variables.