- Install and run Postgres. PostgresApp does a great job.
- Clone this repo
- Set up the enivronment variables found in
env_example.- Be sure to remember the admin email and password for step 5
- Xcode: You can set runtime environment variables by editting Xcode’s project scheme.
- Product > Scheme > Edit Scheme. Then go to Run > Arguments > Environment Variables
- Visual Studio Code: You can set the runtime environment variables by editting the
.vscode/launch.json - macOS: Note that by default Postgres will use your macOS login name as the database name and have no password
- Build and run
- Explore the API using RapidAPI for Mac.
- Open the CoffeeServer.paw file to find various routes. Many routes are admin protected. You’ll need your login info from step 3.
- Be sure your RapidAPI Environment is using values from Step 3.
On the first run, the super user will be created using the values from Step 3. You’ll need the super user account to do most create update and delete actions.
- Some type of constantly internet connected server-like host
- ssh into your host server
- Install git. Documentation
- Install Docker. Documentation
- Clone this repo into a reasonable directory. Ex:
/home/
The entire service fleet (app, database, and Caddy proxy) are configured with the docker-compose file.
From the outside in:
A request to your service will first reach Caddy which knows that your server name — say, https://CoffeeCoffee.world — exists and should proxy the request to the app. Caddy also handles https. The app is a Vapor server written in Swift. It connects to the database which is Postres to save and fetch data.
- Configure the
Caddyfile.- Copy the example file
cp Caddyfile-example Caddyfile - Open Caddyfile in your text editor and change the
example.comdomain name to whichever domain you’re using. Note that this doesn’t include the url scheme — just the stuff after://.
- Copy the example file
- Configure the
.envfile- Copy the example file
cp env_example .env - Open
.envin your text editor and add your values for each value
- Copy the example file
Now... let’s run the service.
Please see Containerization section for more complete deployment instructions.
- Boot the service
- If this is the first run then you’ll need to build the app image:
docker compose up --build -d- If the build fails for any reason, you can build on your local machine and push to docker hub. Your dev sandbox is also likely a great deal faster than a typical web host.
- In standard operation
docker compose up -d
- If this is the first run then you’ll need to build the app image:
- Run any migrations
- If this is the first run then you must run the database migrations:
docker compose run migrate - Running migrations more than once will have no negative effect
- The only time you won’t need to run migrations is if you’re certain that the data models haven’t changed
- If this is the first run then you must run the database migrations:
At this point the service fleet should be running and connected to the database.
- Assert that the appropriate docker containers are running.
docker psshould have three running containers. coffee-server:latest, caddy:XXXX, and postgres:XXXX
- Assert
/healthcheckis OK. From inside your ssh session on the server…curl "http://127.0.0.1:8080/healthcheck"should have output similiar to:OK. Database Check: Event count = 0
- Assert the service is reachable at your domain. Close your server ssh session and try…
curl "http://EXAMPLE.COM/healthcheck"should be OK.
- Finally, assert that https is working.
curl "https://EXAMPLE.COM/healthcheck"should also be OK.
At this point, using a web browser and opening your URL should see the “Coffee”.
Next... containerization
I (@mcritz) deploy the service using OCI files built on a dev box a deployed to a container registry. I use GitHub, modify as needed for your needs.
First, be sure to log in to your container registry.
echo $LOGIN_TOKEN_VAR | docker login ghcr.io -u YOUR_USERNAME --password-stdinbuild and push image script:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t ghcr.io/mcritz/coffee-server:XX.XX.XX \
-t ghcr.io/mcritz/coffee-server:latest \
--push .Of course, this can be altered for your favorite container registry (DockerHub, Amazon ECR, your Raspberry Pi, etc).
Once the Docker file is built and the image pushed, it’s just a matter of pulling the images on the production servers. Run the following command instead of Step 7. Feel free to use the exact same images I push to GitHub or create your own docker-compose.prod.yml file.
docker compose -f docker-compose.yml -f docker-compose.prod.yml pullI build images for linux/amd64 and linux/arm64 so, it should cover the most popular instruction sets available today.