Reviewing source code in Vapor boilerplate
The following diagram gives you an overview of the boilerplate generated from Vapor's default template:
The Package.swift is used by Swift Package Manager during build time to configure your project's package. The package description comprises essential information such as application name, dependencies on other packages, and different build targets supported in your project.
There are three modules in the project: helloWorld, App, and AppTests. (The diagram only shows the helloWorld and App modules, but the AppTests module will be soon reviewed in the next chapter.) The functions and classes in the files in helloWorld and App modules are invoked in the following order:
- The helloWorld executable contains a single entry point in main.swift that calls app() to create an application instance and invokes the instance's run() method to boot your app server.
- The App module contains all of your server application logic. The app() function in app.swift first calls configure() to register all services properly, then it will create an application instance, followed by a call to boot().
- configure() in configure.swift registers all services required for your application:routes, middleware, Fluent providers, SQLite database, and migrations.
- The routes() in routes.swift is called to register routes and configure controllers that handle all the requests routed to them.
- Vapor includes a sample controller, TodoController class in /Controllers/TodoController.swift, in the default project to show how to interact with the data model and perform operations such as query, create, and delete.
- The data model, Todo class in /Models/Todo.swift, is a sample model subclassing from SQLiteModel. It is also extended to support migration, content, and parameter to take advantage of the rich built-in features offered by Vapor.
- The boot() in boot.swift has no any code implementation. It is a placeholder for you to put any initialization code after the application instance is created.
See Appendix A: Vapor Boilerplate Project, for a more detailed review of the files generated in a Vapor boilerplate project.