Context switch is a bitch, there’s no nice way to say it. When I work on side projects that needs a backend service it slows me down a lot.
I’ve worked with Sinatra and Ruby on Rails in the past. They’re great but I don’t use the quite often so I’m very slow when I work with them because my memory is terrible.
On the other hand, I code in Swift every day. So there are some things I can quickly code as if it’s matter of “muscle memory”.
Because of that, Vapor looks like a perfect fit for me, or at leas it’s what I thought.
1. Requirements
Like every tutorial, it is likely to get outdated after a while. These are the versions I worked with.
Hopefully, deploying Vapor apps with Dokku will get more straightforward in the future and you won’t need to follow these steps anymore.
2. Create a new project and run it locally
- Create a new vapor project using the CLI tool.
We don’t need Fluent (databases) but we want to install Leaf (templates).
-
Run the project on our machine.
-
Make sure it works
curl http://127.0.0.1:8080/hello
3. Deploy to Dokku
- Create a new Dokku app
ssh dokku@[REDACTED] apps:create hellovapor
- Create a new local git repository and push to Dokku
git init git remote add betzerra dokku@[REDACTED]:hellovapor git push betzerra main:master
… and we’re done, right?
Well, no.
We’ve deployed the app but with no https support. This would’ve been just fine in 2010 but now there’re many browsers that won’t open “unsecured” websites by default.
That’s where letsencrypt plugin comes in.
- Set your email to DOKKU_LETSENCRYPT_EMAIL environment variable so the plugin can do its magic
ssh dokku@[REDACTED] config:set --no-restart hellovapor DOKKU_LETSENCRYPT_EMAIL=[REDACTED]
- Get a letsencrypt certificate for our project
ssh dokku@[REDACTED] letsencrypt:enable hellovapor
If you’re using the versions I defined above you’ll get an error.
I got stuck with this error for more than a week. Reported this here and here but had no luck.
The fix
This piece of information in the README helped me a lot:
For Dockerfile deploys, by default, dokku will determine which ports a container exposes and proxies all those exposed ports in the Docker container by listening on the same port numbers on the host. This means that both the proxies for HTTP port 80 and HTTPS port 443 to the app’s container need to be manually configured.
This is how it should look like when you write:
Use proxy:ports-remove and proxy:ports-add command to set the ports properly. For example:
Now re-run letsencrypt command:
This time should run fine 🙌
4. Herokuish Buildpack (Optional)
If you’re getting an outdated Swift, you could try Heroku’s buildpack:
Create a file called .buildpack with the following content:
Summary
Compared with Sinatra, deploying a simple website took me more energy and time. On the other hand, Swift and Vapor are quite new yet and there’s a long way to go.
Ruby was created in 1995, Rails in 2004. Of course they will have a bigger community, ubiquitous documentation and better support. Picking the right framework is as important as picking the right language.
That being said, I was able to deploy my first simple service (it’s a few lines of code and uses Redis) in Vapor last month, so I’m feeling positive that this will get better and better.
Time will tell! 🤞