First, create a new project. Go to any directory in the terminal
console
$ vapor new [project name]
And enter the command. Please change the place of `[project name]`
as appropriate.
console
Would you like to use Fluent? (--fluent)
Which database would you like to use? (--fluent.db)
Would you like to use Leaf? (--leaf)
This time, we will also use Fluent to migrate the DB. Answer y to the question on the terminal. DB uses Postgress.
Next, add LinuxMain.swift with the following command.
console
$ swift test --generate-linuxmain
Make settings to read index.leaf in Resources/views. After launching Xcode, first click on the project name in the upper left and select Edit Scheme.
You can set the Working Directry on this screen, so please match it with the location of the Vapor project.
Next, add the code for migrating to configure.swift.
configure.swift
app.migrations.add(SessionRecord.migration)
app.sessions.use(.fluent(.psql))
Next, make routes.swift as follows.
routes.swift
import Fluent
import Vapor
// MARK: - Date extention
extension Date {
func currentTime() -> String {
let formatter = DateFormatter()
formatter.timeZone = .current
formatter.locale = .current
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter.string(from: self)
}
}
func routes(_ app: Application) throws {
app.middleware.use(app.sessions.middleware)
app.get() { req -> EventLoopFuture<View> in
//Record last visit time in cookie
req.session.data["last_visited_at"] = Date().currentTime()
return req.view.render("index", ["title": "Hello Vapor!"])
}
app.get("hello") { req -> String in
return "Hello, world!"
}
try app.register(collection: TodoController())
}
With this, after Migrate, Session ID will be given to the person who visited the website and it will be possible to check it on the DB.
Create a heroku account before pushing to heroku.
Because I use heloku CLI in the terminal
console
$ brew tap heroku/brew && brew install heroku
Then create a local repository.
console
$ git init
$ git add .
$ git commit -m "initial commit"
Now that you're ready to push to heroku, log in
console
$ heroku login
Create a new heroku application.
console
$ vapor heroku init
Region is US, deploy method is buildpack, swift ver is optional (my environment was 5.3)
If you answer all the other questions as y, the deployment will start. (It takes about 10 minutes.)
console
Verifying deploy... done.
Is displayed, it is successful. However
console
$ vapor heroku init
Since the randomly generated name is applied to the heroku application created in
console
$ heroku rename [name]
You can change the name with.
Set the database on the management screen of heroku.
Select Heroku Postgres
Based on the information of DatabaseCredentials, we will register the environment variables on the management screen of heroku.
Go to Settings-> Config Vars
DATABASE_HOST
DATABASE_USERNAME
DATABASE_PASSWORD
DATABASE_NAME
Add the four environment variables of.
After adding, select Run console from the menu on the upper right
heroku_console
$ Run migrate
If successful, you will be able to use the database.
Recommended Posts