Starsky backend represents the API and database portion of starsky application for employee scheduling. It uses Java (Spring Boot) for REST API, PostgreSQL for data storage and Nginx for reverse proxy in production.
Front-end React application is located in a repository called starsky-frontend.
It also uses the transactional email API that is located in a repository called starsky-mail.
- docker
- docker-compose (at least 3.8 version support)
Please note that this has only been tested with docker on Ubuntu 20.04.
- Download source files and go to
docker
directory:
git clone https://github.com/peroxy/starsky-backend.git
cd starsky-backend/docker
- Build and run the entire stack:
docker-compose up
- You will now be able to access:
- API at http://localhost:8080/ and swagger-ui at http://localhost:8080/api/swagger-ui.html
- database at http://localhost:5432/
You can access the starsky
database by using credentials (this can be changed in the docker-compose.override.yml
file):
- username:
starsky
- password:
starsky
You can run the API locally using your favorite Java IDE:
- Run the database:
cd starsky-backend/docker
docker-compose up database
- Set Spring active profiles:
dev
andlocal
- Run and debug the application
We are using JUnit5 for tests with Spring Boot. An in-memory H2 database is used.
No extra configuration is needed, just run: gradle test
You can generate an OpenAPI client by running backend API locally, then running (for example TypeScript client):
docker run --rm --network host -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i http://localhost:8080/api/v3/api-docs -g typescript -o /local/out/ts
We host entire infrastructure on Azure, specifically using Azure Virtual Machine.
The server (in our case Azure VM) must have these installed:
- docker,
- docker-compose (at least 3.8 version support).
-
Create Azure Virtual Machine with Ubuntu installed and setup your public SSH key. Ubuntu 18.04 was used at the time of writing this.
-
Enable SSH (port 22) and whitelist your IP.
-
Connect to your machine:
ssh username@ipAddress
-
curl -fsSL https://get.docker.com -o get-docker.sh sudo usermod -aG docker <your-user>
Log out and log back in to be able to use
docker
withoutsudo
. -
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
-
Generate a SSH key:bb
ssh-keygen -t rsa -b 4096 -c "starsky_deploy"
Currently, SSL is done manually by using ZeroSSL and copying certificates to Azure VM.
- Generate a certificate, you will get 3 files: certificate.crt, private.key, ca_bundle.crt.
- Copy them to Azure VM with rsync:
cd ~/certs
rsync ./* user@host:~/certs
- Restart Nginx on Azure VM.
These are the required secrets that should be stored inside Github repository secrets:
- Dockerhub:
DOCKERHUB_USERNAME
DOCKERHUB_TOKEN
- see Create an access token for more information
- PostgreSQL:
POSTGRES_USER
POSTGRES_PASSWORD
- don't make it too long, there were some issues with authentication with a 128 character password, even though it should be supported in theory...
- Server host (Azure VM):
REMOTE_HOST
- remote host IP address / domain to SSH intoREMOTE_USER
- username to SSH withSERVER_SSH_KEY
- private SSH key (OpenSSH, for example the contents of your~/.ssh/id_rsa
key) to connect to your server
- API:
STARSKY_JWT_SECRET
- needed for generating JWT tokens, use a random 32 character secretSTARSKY_FRONTEND_REGISTER_URL
- needed to send invite links with proper URL
Push a tag *.*.*
(e.g. 1.0.3
) to master
branch and it will automatically deploy everything via Github workflow.
See .github/main.yml
workflow for more info.
In short, it does this if it gets triggered by a new tag:
- Takes source code from
master
branch and extracts the newest version from tag. - Configures environment variables used by docker containers from Github repository's secrets.
- Builds and pushes all apps as Docker images to DockerHub.
- Copies environment variables and docker-compose files to Azure VM.
- Stops
starsky-backend
containers on Azure VM, pulls the newest images and starts the containers again.