Now that you've created, built, and run your first Vapor app, let's take a moment to familiarize you with Vapor's folder structure. The structure is based on SPM's folder structure, so if you've worked with SPM before it should be familiar.
. ├── Public ├── Sources │ ├── App │ │ ├── Controllers │ │ ├── Migrations │ │ ├── Models │ │ ├── configure.swift │ │ └── routes.swift │ └── Run │ └── main.swift ├── Tests │ └── AppTests └── Package.swift
The sections below explain each part of the folder structure in more detail.
This folder contains any public files that will be served by your app if
FileMiddleware is enabled. This is usually images, style sheets, and browser scripts. For example, a request to
localhost:8080/favicon.ico will check to see if
Public/favicon.ico exists and return it.
You will need to enable
FileMiddleware in your
configure.swift file before Vapor can serve public files.
// Serves files from `Public/` directory let fileMiddleware = FileMiddleware( publicDirectory: app.directory.publicDirectory ) app.middleware.use(fileMiddleware)
This folder contains all of the Swift source files for your project.
The top level folders,
Run, reflect your package's modules,
as declared in the SPM manifest.
This is where all of your application logic goes.
Controllers are great way of grouping together application logic. Most controllers have many functions that accept a request and return some sort of response.
The migrations folder is where your database migrations go if you are using Fluent.
The models folder is a great place to store your
Content structs or Fluent
This file contains the
configure(_:) function. This method is called by
main.swift to configure the newly created
Application. This is where you should register services like routes, databases, providers, and more.
This file contains the
routes(_:) function. This method is called near the end of
configure(_:) to register routes to your
This is the main executable target, containing just the code needed to get your application up and running.
This file creates and runs a configured instance of your Vapor
Each non-executable module in your
Sources folder can have a corresponding folder in
Tests. This contains code built on the
XCTest module for testing your package. Tests can be run using
swift test on the command line or pressing ⌘+U in Xcode.
This folder contains the unit tests for code in your
Finally is SPM's package manifest.