[SWIFT] Deploy Vapor Project to Heroku

I decided to participate in Vapor Advent Calendar 2020, and I wrote it as a memorandum.

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

Launch Xcode

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.

pic1.png

You can set the Working Directry on this screen, so please match it with the location of the Vapor project.

pic2.png

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.

Push to Heroku

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.

My Great on Heroku

Set the database on the management screen of heroku.

Select Heroku Postgres

20201120-ss1.png

Based on the information of DatabaseCredentials, we will register the environment variables on the management screen of heroku.

スクリーンショット 2020-12-19 12.34.53.png

Go to Settings-> Config Vars

DATABASE_HOST
DATABASE_USERNAME
DATABASE_PASSWORD
DATABASE_NAME

Add the four environment variables of.

20201120-ss3.png

After adding, select Run console from the menu on the upper right

20201120-ss4.png

heroku_console


$ Run migrate

If successful, you will be able to use the database.

Recommended Posts

Deploy Vapor Project to Heroku
Steps to deploy to Heroku
How to deploy on heroku
heroku deploy
Deploy Rails on Docker to heroku
Deploy to heroku with Docker (Rails 6, MySQL)
How to deploy
Deploy to Heroku [Ruby on Rails] Beginner
heroku: docker: deploy
Deploy a Tomcat-based Eclipse project on Heroku
Deploy to Heroku (for second and subsequent edits)
Steps to deploy Struts 2.5.8
Can't upload to heroku
How to deploy to Heroku from a local docker image
How to check CircleCI code and automatically deploy to Heroku
Deploy the Spring Boot project to Tomcat on XAMPP
Deploy Spring Boot applications to Heroku without using the Heroku CLI
How to deploy a simple Java Servlet app on Heroku
Deploy the application created by Spring Boot to Heroku (public) ②
Introduce Maven to Tomcat project
Deploy the application created by Spring Boot to Heroku (public) ①
Deploy your application to WildFly
When you deploy to Heroku but it doesn't reflect well
How to deploy an app that references a local jar to heroku
I was able to deploy the Docker + laravel + MySQL app to Heroku!
How to deploy jQuery on Rails
How to deploy Laravel on CentOS 7
[Note] Update to production environment [heroku]
How to deploy Bootstrap on Rails
[STS] Error due to project overlap
Deploy a war file on Heroku
Deploy to EC2 with CircleCi + Capistrano
upload images to refile heroku S3
How to connect Heroku and Sequel
Deploy Flask's Docker image on Heroku
[Note] Update to production environment (heroku)
To switch JVM for each project
Addicted to project imports from GitHub
How to make a Maven project
Deploy SpringBoot application to AWS EC2
Deploy your Rails app on Heroku