This project is a Go-based API for managing financial assistance schemes, applicants, and applications. The API uses PostgreSQL as the database and secures endpoints using JWT-based authentication.
Before setting up the development environment, ensure you have the following installed on your machine:
- Docker: Install Docker
- Docker Compose: Install Docker Compose
- Go (Golang): Install Go (Optional, for local development)
- Postman: Install Postman (for API testing and documentation)
Use these credentials for login endpoint to obtain JSON web token to be used in authentication header for all other endpoints
{
"email": "root@root.com",
"password": "root"
}
This project includes a script generate_password_and_run.sh
that:
- Generates a random password for the PostgreSQL database.
- Updates the
docker-compose.yml
file with the generated password. - Exports the necessary environment variables.
- Runs the application using Docker Compose.
To run the application using Docker Compose:
chmod +x generate_password_and_run.sh
./generate_password_and_run.sh
This script will:
- Generate a random password for the PostgreSQL database.
- Start the database and the application services.
- Apply any pending migrations to the database.
- Expose the API on
localhost:8080
.
You can stop the containers using:
docker-compose down
For local deployment (without Docker), follow these steps:
-
Clone the Repository:
git clone https://github.com/bensiauu/financial-assistance-scheme.git cd financial-assistance-scheme
-
Install Dependencies: Ensure you have Go installed, then install the dependencies:
go mod tidy
-
Setup PostgreSQL: Ensure you have a local PostgreSQL instance running. You can set it up as follows:
-
Install PostgreSQL: Follow the instructions on PostgreSQL's website.
-
Create a Database and User:
CREATE USER govtech WITH ENCRYPTED PASSWORD 'password123'; CREATE DATABASE financial_assistance; ALTER DATABASE financial_assistance OWNER TO govtech; <!-- connect to financial_assistance database as govtech --> CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-
-
Configure Environment Variables: Set the required environment variables:
export DB_USER=govtech export DB_PASSWORD=password123 export DB_NAME=financial_assistance export DB_HOST=localhost export DB_PORT=5432
-
Run the Application: Start the application:
go run cmd/api/main.go
The API will now be running at
http://localhost:8080
.
To run your tests inside Docker using Docker Compose:
docker-compose run --rm app go test ./...
To run the tests locally:
go test ./...
Ensure the PostgreSQL test database is running with the correct environment variables for testing.
API documentation can be found here.
-
Database Connection Issues:
- Ensure that the
DB_HOST
,DB_PORT
,DB_USER
,DB_PASSWORD
, andDB_NAME
environment variables are set correctly. - Ensure that the PostgreSQL service is running and accessible.
- Ensure that the
-
Migrations Not Applied:
- Check if the migrations have been run by using
go run cmd/migrate/main.go
or by checking themigrations
table in the database.
- Check if the migrations have been run by using
-
Token Expiry or Invalid Token:
- Ensure that the JWT secret and token expiration time are set properly in the code and environment.
To check the logs for the running application, use:
docker-compose logs