silver-bassoon

Whats going on here?

Stack used ๐Ÿฅž

  • PostgreSQL
  • Golang 1.21
  • SQLC (jump)
  • VueJS
  • Vite (bundler)

Setup ๐Ÿ› 

After cloning the repo, you need to install Go dependencies. Run the following command:

go mod tidy

Then, install NodeJS dependencies. Run the following command:

yarn install

Initialize the Database

For database initialization section you can found it here: Click the link

About SQLC

By using sqlc, this app does not require you to write the models mapping of your table into Golang native structs yourself.

Moreover, you can write a raw SQL query in a .sql file and convert it into a Golang native func by placing the .sql file under the scripts/queries directory.

โ”œโ”€โ”€ cmd
โ”‚   โ”œโ”€โ”€ migrate
โ”‚   โ”‚   โ”œโ”€โ”€ main.go
โ”‚   โ”‚   โ””โ”€โ”€ migrations
โ”‚   โ”‚       โ””โ”€โ”€ 00_initial.up.sql
โ”œโ”€โ”€ scripts
โ”‚   โ””โ”€โ”€ queries
โ”‚       โ”œโ”€โ”€ order_items.sql (EXAMPLE)
โ”‚       โ””โ”€โ”€ orders.sql (EXAMPLE)

Generate struct as models and func as queries

Since you already have your migration and query files, the next step is to generate the struct and func for it by running this command:

go run cmd/sqlc/main.go

This will generate .go files with an expected output like this:

โ”œโ”€โ”€ internal
โ”‚   โ””โ”€โ”€ repo
โ”‚       โ””โ”€โ”€ psql
โ”‚           โ”œโ”€โ”€ db.go
โ”‚           โ”œโ”€โ”€ models.go
โ”‚           โ”œโ”€โ”€ order_items.sql.go
โ”‚           โ”œโ”€โ”€ orders.sql.go
โ”‚           โ””โ”€โ”€ querier.go

You can read the file as well by:

cat internal/repo/psql/models.go

How to Run ๐Ÿ‘Ÿ

Run Locally ๐Ÿƒ

NOTE: If you've done Database Initialization steps from here, make sure to clean up the temporary files that generated in that step.

To run the app locally, you need to run migration first (can skip if you are from Database Initialization steps)

DATABASE_URL=postgres://user:password@ip:5432/dbname go run cmd/migrate/main.go

Then you can start the backend:

DATABASE_URL=postgres://user:password@ip:5432/dbname go run cmd/server/*.go
# http start at :3980

Then, frontend:

VITE_SERVER_URL=http://localhost:3980 yarn start
#    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
#    โ”‚                                          โ”‚
#    โ”‚   Serving!                               โ”‚
#    โ”‚                                          โ”‚
#    โ”‚   - Local:    http://localhost:8080      โ”‚
#    โ”‚   - Network:  http://10.XX.XXX.XX:8080   โ”‚
#    โ”‚                                          โ”‚
#    โ”‚   Copied local address to clipboard!     โ”‚
#    โ”‚                                          โ”‚
#    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Or:

VITE_SERVER_URL=http://localhost:3980 yarn start:dev
# Open: http://localhost:5173/

Dockering ๐Ÿณ

Using Dockerfile

NOTE: this steps below is assume that you are already have a running PostgreSQL instance.

Run the migration (skip if already done):

DATABASE_URL=postgres://user:password@ip:5432/dbname go run cmd/migrate/main.go

Build docker image for backend:

docker build \
    -t silver-bassoon/backend \
    -f Dockerfile.backend \
    --no-cache .

Run backend container from created image:

docker run -d \
    --name packform-be \
    -e DATABASE_URL='postgres://user:password@ip:5432/silver_bassoon?sslmode=disable' \
    -p 3980:3980 \
    silver-bassoon/backend

Build docker image for frontend:

docker build \
    -t silver-bassoon/frontend \
    -f Dockerfile.frontend \
    --build-arg server_url=http://localhost:3980 \
    --build-arg use_browser_tz=false \
    --no-cache .

Run frontend container from created image:

docker run -d \
    --name packform-fe \
    -p 8080:80 \
    silver-bassoon/frontend

Using docker-compose (Recommended)

By using docker compose, you are not required to have PostgreSQL running, and you are not required to run the migration as well. Everything will be done automatically, encapsulated.

docker compose up -d

Testing ๐Ÿงช

Backend Unit Testing

go test -v ./...

Frontend Unit Testing

yarn test