http://37.139.6.166/Ghost 0.11Thu, 07 Sep 2017 16:47:35 GMT60We will start a new vapor app with RESTful controller and SQLite support. So let's get started

Installation:

Vapor comes with nice CLI utility called Toolbox.
The easiest way to install it on macOS is to use homebrew ( package manager for macOS )
if you don't have homebrew installed already run

]]>http://37.139.6.166/gettings-started-with-vapor-a-web-framework-for-swift/06edff37-c574-4aba-922a-03634a270893Mon, 03 Apr 2017 19:56:58 GMTWe will start a new vapor app with RESTful controller and SQLite support. So let's get started

Installation:

Vapor comes with nice CLI utility called Toolbox.
The easiest way to install it on macOS is to use homebrew ( package manager for macOS )
if you don't have homebrew installed already run following command in the Terminal:

Let's go line by line.
At first we import the Vapor framework, then we instaniate Droplet object.
Droplet is a service container that gives you access to many of Vapor's facilities. It is responsible for registering routes, starting the server, appending middleware, and more stuff like that.

after we are saying that on the root route we would like to return a view rendered from the "welcome" template and passing localized string with key "welcome.title" to our view accessible there with name of "message".

the next line register REST'full controller PostController for the route "posts"

and the final line actually starts the server

Adding database support (SQLite)

So if look at the PostController.swift we would see that it actually maps CRUD operations on Post model to REST interface, using makeResource method. but if we would try to open http://localhost:8080/posts we would get a page which states "EntityError: noDatabase"
and returns http status 500. Let's fix that by adding database support so we could work with our Post model.

It would fetch dependencies for our project, based on the contents of Package.swift
next we need to regenerate our xcode project with included support of new dependencies
so let's close our project and run

vapor xcode

command again, and reopen the project when command is finished.

Next step is to add configuration file for the database named sqlite.json to the Config folder of our project.
since we are using sqlite the only config param we actually need for now is path of the database file, so the file would look like this:

{
"path": "hello-vapor.db"
}

Next we need to let Vapor know that we are using sqlite, so let's open main.swift

Second let's add Preparations to our model. Basically preparation is Vapor name for migrations.
So it's a right place to prepare our database for our data objects. I'll just post a full file listing here since it's pretty
self explanatory.