Cats Social is an app that allows cat owners to match their cats with each other.
This application requires the following:
- Golang: https://go.dev/dl/
- PostgreSQL: https://www.postgresql.org/download/
- Golang Migrate: https://github.com/golang-migrate/migrate
To install the boilerplate, follow these steps:
-
Make sure you have Golang, PostgreSQL, and Golang Migrate installed and configured on your system.
-
Clone this repository:
git clone https://github.com/BennoAlif/ps-cats-social.git
-
Navigate to the project directory:
cd ps-cats-social
-
Run the following command to install dependencies:
go mod download
Cats Social offers the following features:
- Authentication:
- User registration
- User login
- Cat Management (CRUD):
- Create new cat profiles
- View existing cat profiles
- Update cat profiles
- Delete cat profiles
- Matching:
- Match your cat with other cats
- View matching cats
- Approve or reject matches
- Delete matches
-
Setting Up Environment Variables
Before starting the application, you need to set up the following environment variables:
DB_NAME
: Name of your PostgreSQL databaseDB_PORT
: Port of your PostgreSQL database (default: 5432)DB_HOST
: Hostname or IP address of your PostgreSQL serverDB_USERNAME
: Username for your PostgreSQL databaseDB_PASSWORD
: Password for your PostgreSQL databaseDB_PARAMS
: Additional connection parameters for PostgreSQL (e.g., sslmode=disabled)JWT_SECRET
: Secret key used for generating JSON Web Tokens (JWT)BCRYPT_SALT
: Salt for password hashing (use a higher value than 8 in production!)
-
Database Migrations
Cats Social uses Golang Migrate to manage database schema changes. Here's how to work with migrations:
-
Apply migrations to the database:
migrate -database "postgres://username:password@host:port/dbname?sslmode=disable" -path db/migrations up
-
-
Running the Application
Once you have set up the environment variables, you can start the application by running:
go run src/main.go
This will start the Cats Social application on the default port (usually 8080). You can access the application in your web browser at http://localhost:8080
The application uses environment variables for configuration. You can configure the database connection, JWT secret, and bcrypt salt by setting the following environment variables:
- Refer to the Usage section for a detailed explanation of each environment variable.
Database migration must use golang-migrate as a tool to manage database migration
-
Direct your terminal to your project folder first
-
Initiate folder
mkdir db/migrations
-
Create migration
migrate create -ext sql -dir db/migrations add_user_table
This command will create two new files named
add_user_table.up.sql
andadd_user_table.down.sql
inside thedb/migrations
folder.up.sql
can be filled with database queries to create / delete / change the table.down.sql
can be filled with database queries to perform arollback
or return to the state before the table from.up.sql
was created
-
Execute migration
migrate -database "postgres://username:password@host:port/dbname?sslmode=disable" -path db/migrations up
-
Rollback migration
migrate -database "postgres://username:password@host:port/dbname?sslmode=disable" -path db/migrations dow
-
View the current migration state
migrate -database "postgres://username:password@host:port/dbname?sslmode=disable" version