[SSS] Setting the project up

:  ~ 2 min read

The first post in the Server Side Swift series will be about initializing the project, its structure, and configuring your Droplet. Running the vapor xcode -y command (although I always run swift build first) will download dependencies, build them, configure an Xcode project, and you will end up with a structure like this:

___ Config
   |___ secrets (optional) - This should be in your .gitignore.
   |___ production (optional)
   |_ app.json
   |_ servers.json
___ Localization - Translation files.
   |_ xx-YY.json
___ Packages - This is where Vapor installs your packages, and links them in Xcode under the Sources folder group.
___ Public - All files that are public should go here, like downloadable assets, CSS, scripts, etc.
   |___ images
   |___ scripts
   |___ stylesheets
   |_ favicon.ico
___ Resources - Not really sure what else can go in here except views.
   |___ Views
       |___ Partials
       |   |_ article.leaf
       |   |_ footer.leaf
       |   |_ ... other partials.
       |___ Standalone
       |   |_ iwj.html
       |   |_ ... other standalone pages.
       |_ about.leaf
       |_ base.leaf
       |_ ... other pages.
___ Sources - Not really sure what else can go here except your app files.
   |___ App
       |___ Controllers
       |   |_ AboutController.swift
       |   |_ SearchController.swift
       |   |_ ... etc.
       |___ Models
       |   |_ Post.swift
       |   |_ File.swift
       |___ ... etc.
       |_ main.swift
___ Tests
   |___ AppTests
       |_ PostTests.swift
       |_ FileTests.swift
       |_ ... etc.
_ Package.swift
_ ... misc, like README, etc

Finally, let's quickly cover the droplet. The first file you get after creating your project is main.swift, that initializes a Droplet, and offers a route example. I took a different approach, and created two methods in an extension that configure the Droplet, so that I can test them:

import Vapor
// Be sure to have added a PostgreSQL provider to your Packages:
// .Package(url: "https://github.com/vapor/postgresql-provider", majorVersion: 1)
import VaporPostgreSQL

extension Droplet {

	static func setUp() throws -> Droplet {
      let config = try Config()
      // [...] Contains more stuff that we'll cover in later posts.
      
      config.preparations.append(Post.self) // This tells Vapor that Post is a model.
      try config.addProvider(PostgreSQLProvider.Provider.self) // This tells Vapor to use PostgreSQL as the database provider.
      
      let drop = try Droplet(config: config)
      
      // [...] Contains more stuff that we'll cover in later posts.
      
      return drop
	}

	func addRoutes() -> Droplet {
		get("/") { request in
			return "We got steam!"
		}

		// [...] Contains more stuff that we'll cover in later posts.

		return self
	}

}

Now we can have a really simple main.swift file, and easier to test code:

let drop = try Droplet.setUp().addRoutes()
try drop.run() // returns Never, so we can't chain it above.