3.1 KiB
Structure
This section explains the structure of a typical Vapor application to help get you familiar with where things go.
Folder Structure
Vapor's folder structure builds on top of SPM's folder structure.
.
├── Public
├── Sources
│ ├── App
│ │ ├── Controllers
│ │ ├── Models
│ │ ├── boot.swift
│ │ ├── configure.swift
│ │ └── routes.swift
│ └── Run
│ └── main.swift
├── Tests
│ └── AppTests
└── Package.swift
Let's take a look at what each of these folders and files does.
Public
This folder contains any public files that will be served by your app. This is usually images, style sheets, and browser scripts.
Whenever Vapor responds to a request, it will first check if the requested item is in this folder. If it is, it skips your application logic and returns the file immediately.
For example, a request to localhost:8080/favicon.ico will check to see
if Public/favicon.ico exists. If it does, Vapor will return it.
Sources
This folder contains all of the Swift source files for your project.
The top level folders (App and Run) reflect your package's modules,
as declared in the package manifest.
App
This is the most important folder in your application, it's where all of the application logic goes!
Controllers
Controllers are great way of grouping together application logic. Most controllers have many functions that accept a request and return some sort of response.
!!! tip Vapor supports, but does not enforce the MVC pattern
Models
The Models folder is a great place to store your Content structs or
Fluent Models.
boot.swift
This file contains a function that will be called after your application has booted, but before it has started running. This is a great place do things that should happen every time your application starts.
You have access to the Application here which you can use to create
any services you might need.
configure.swift
This file contains a function that receives the config, environment, and services for your application as input. This is a great place to make changes to your config or register services to your application.
routes.swift
This file contains a function for adding routes to your router.
You will notice there's one example route in there that returns the "hello, world" response we saw earlier.
You can create as many methods as you want to further organize your code. Just make sure to call them in this main route collection.
Tests
Each non-executable module in your Sources folder should have a corresponding ...Tests folder.
AppTests
This folder contains the unit tests for code in your App module.
Learn more about testing in Testing → Getting Started.
Package.swift
Finally is SPM's package manifest.