上QQ阅读APP看书,第一时间看更新
Reviewing source code in Kitura boilerplate
The following diagram illustrates the source code files generated from the last section:
The Package.swift is used in the same way as that in a Vapor project. Your application's name, required package dependencies, and different build targets are configured in this manifest file.
A typical Kitura project is partitioned in a similar fashion as in a Vapor project; an application executable module, a test-executable module, and a core application module with all of your application logics:
- There is only one Swift file in the helloWorld executable module. It serves as an entry point for your project and does several things: It configures Kitura's Helium logger to be used in this project, creates an App() instance, and calls the instance's run() function. The entry point is implemented in a do-try-catch block to catch any runtime errors in your project. In run(), it first calls the postInit() function, then it will configure a HTTP server and start running a Kitura application instance.
- The App() class used in the main.swift file's do-try-catch block is defined in Application.swift. Before an instance of App() is being constructed, the initializer function init() will be called. The init() function will in turn call initializeMetrics().
- The initializeMetrics() function is implemented in Metrics.swift. It creates three metrics monitoring instances: a SwiftMetrics to gather performance metrics, a SwiftMetricsDash to enable a dashboard web page that displays the performance metrics, and a SwiftMetricsPrometheus to allow the performance metrics to be used in application clusters monitoring tool.
- In the basic template, the postInit() function in main.swift sets up only one endpoint: Health Route, by calling the initializeHealthRoutes(app:) function that is defined in HealthRoutes.swift. initializeHealthRoutes(app:) handles the response to the /health endpoint. Any routes such as this Health Route are initialized after an app instance is created, but before a Kitura server is started.
- Initialization.swift contains only one struct InitializationError() that is inherited from Error() struct. It gives you a chance to modify error messages when there is an error during initialization.
See Appendix B: Kitura Boilerplate Project, for a more detailed review of the files generated in a Kitura boilerplate project.