End-to-end shipping API solution using Easypost and Stripe. Buy shipping labels, track and ship your packages all in one API.
To run the stack locally, follow these steps:
- Edit the
.env
file and update the values as needed. - Run
docker-compose -f docker-compose.dev.yml up
to start the Postgres database and pgAdmin. - Load the environment variables from the
.env
file. If you are using IntelliJ IDEA, you can follow the example in the next section (Loading Environment Variables in IntelliJ IDEA) - Run the API
To test Easypost and Stripe webhooks your connection needs to be over HTTPS. To do so we can use ngrok
to setup a HTTPS tunnel pointing to your local webhook endpoint. This tunnel exposes your endpoint to the Internet over SSL.
Install and run ngrok. You can do this by following these steps:
- Download ngrok from https://ngrok.com/download.
- Launch ngrok by using the command
ngrok http 8080
in a Terminal (where 8080 is the post the server is running on). Ngrok will provide a URL that you can use to expose your local server. - Set
EASYPOST_WEBHOOK_ENDPOINT_URL
andSTRIPE_WEBHOOK_ENDPOINT_URL
envionement variables in.env
to{ngrokURL}/api/v1/shipments/easypost-webhook
and{ngrokURL}/api/v1/subscriptions/stripe-webhook
respectively. Replace{ngrokURL}
with the URL provided by ngrok.
If you're using IntelliJ IDEA, you can load environment variables from your .env file directly into the IDE. Follow these steps:
- Open your project in IntelliJ IDEA.
- Go to File > Settings (or IntelliJ IDEA > Preferences on macOS).
- In the Settings dialog, navigate to Plugins.
- Click the Marketplace tab and search for EnvFile.
- Install the EnvFile plugin and restart IntelliJ IDEA when prompted.
Now, you can configure IntelliJ to load the environment variables from the .env file when running your application:
- In the top-right corner, click on the Edit Configurations button (it looks like a dropdown list with a gear icon).
- In the Run/Debug Configurations dialog, select your Spring Boot application configuration in the left pane.
- In the right pane, under the Configuration tab, scroll down to the Environment section.
- Check the Enable box next to EnvFile.
- Click the + button and browse to your .env file in the project directory. Select the file and click OK.
- Click Apply and then OK to save the configuration.
You can access authenticated endpoints by logging in with the default admin account through the API:
- Email: admin@lol.com
- Password: admin123
A Swagger documentation is automatically generated for the API when running it on dev mode (specified by spring.profiles.active).
The API's documentation is accessible through this following link:
Where 8080 is the port number the API is running on.
To use authenticated endpoints, you can obtain an access token directly from the swagger doc:
- Use the authentication-controller login endpoint. Login with the default admin account or another one of your accounts. Get the access_token from the response.
- Navigate to the top right and click on the green Authorize button and paste the access-token preceded by the word "Bearer" in the value field, e.g "Bearer access-token"
- Click on "Authorize". You're now ready to use authenticated endpoints!
We use Flyway for database migrations. To add a new database migration script, follow these steps:
-
Create a new SQL file in the resources/db/migration directory.
-
Ensure that the SQL file follows the correct naming convention for migration files, which is
V<VERSION_NUMBER>__<MIGRATION_NAME>.sql
. For example,V1__Initial_Setup.sql
orV2__Add_New_Table.sql
. -
When you run the production stack, Flyway will automatically handle the updates by executing the migration scripts in the correct order based on their version numbers.
Local dev doesn't use Flyway, but uses spring.jpa.hibernate.ddl-auto=create-drop
to create the tables and drop them when the API stops running locally
This project uses a Continuous Integration (CI) pipeline with GitHub Actions. When new changes are pushed to the main
branch, the CI pipeline is triggered.
The CI pipeline performs the following steps:
- Build the Docker image for the API.
- Push the Docker image to the GitHub Docker Registry.
The pipeline ensures that the API is containerized and available in the GitHub Docker Registry for deployment purposes.
This project utilizes Continuous Deployment (CD) with GitHub Actions, automatically deploying the application to the VPS when changes are pushed to the release
branch.
The Continuous Deployment (CD) pipeline uses docker-compose
to deploy the production stack. The stack consists of the following services:
- PostgreSQL Database: A PostgreSQL database is used as the primary data store for the application.
- API: The Navaship API.
The CD pipeline performs the following steps:
- Check out the repository.
- Copy the repository to the VPS using SCP (Secure Copy Protocol) with SSH.
- SSH into the VPS.
- Call
docker-compose.prod.yml
to deploy the application.
The following environment variables are used in the continuous deployment pipeline to connect to the VPS:
- VPS_HOST
- VPS_PORT
- PASSPHRASE
- USERNAME
The table below lists all the other environment variables that are used to deploy the backend stack and whether they are stored as secrets (the secure column is checked) or plain old environment variables
Variable Name | Description | Secured | Dev Only |
---|---|---|---|
DATABASE_NAME | Database name for the application | ||
DATABASE_PORT | Port number for the database connection | ||
DATABASE_USER | Username for database access | ||
DATABASE_PASSWORD | Password for database user | ✓ | |
PGADMIN_DEFAULT_EMAIL | Default email for PgAdmin access | ✓ | |
PGADMIN_DEFAULT_PASSWORD | Default password for PgAdmin access | ✓ | |
API_DOMAIN | API domain for the application | ||
API_PORT | API port for the application | ||
WEBAPP_URL | URL for the web application | ||
SENDGRID_SENDER_EMAIL | Sender email for SendGrid email service | ||
SENDGRID_APIKEY | API key for SendGrid email service | ✓ | |
EASYPOST_APIKEY | API key for EasyPost shipping service | ✓ | |
EASYPOST_WEBHOOK_ENDPOINT_URL | URL for EasyPost webhook endpoint | ||
STRIPE_PRODUCT_ID | The product ID in Stripe that contains the memberships | ||
STRIPE_APIKEY | API key for Stripe payment processing | ✓ | |
STRIPE_WEBHOOK_ENDPOINT_URL | URL for Stripe webhook endpoint | ✓ |