walkccc / greenlight

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Greenlight

Go 1.21

This repository is based on "Let's Go Further" by Alex Edwards. The Goal is to provide a well-structured way to start a Go-backed backend infrastructure.

Prerequesites

Install Docker and PostgreSQL image

# Install Docker.
brew install docker

# Run Docker app so that we can access the `docker` command.

# Pull the PostgresSQL image.
docker pull postgres:15.5-alpine

# Check the downloaded image.
docker images

Install migrate

# Install `migrate` command.
brew install golang-migrate

# Check the installed `migrate` command.
migrate --version

Take a look at Makefile and bootstrap.sh

# See all the available commands.
make help

Get Started

Run a Docker container using the official PostgreSQL image

Creates and runs a Docker container with the name postgres, using the official postgres:15.5-alpine Docker image. The container is started as a background process (-d flag) and is mapped to port 5432 of the host machine (-p 127.0.0.1:5432:5432/tcp flag), which is the default port for PostgreSQL.

The container is also configured with the environment variables POSTGRES_USER and POSTGRES_PASSWORD, which set the default username and password for the PostgreSQL database. In this case, the username is set to root and the password is set to password.

docker run --name postgres \
  -p 127.0.0.1:5432:5432/tcp \
  -e POSTGRES_USER=root \
  -e POSTGRES_PASSWORD=password \
  -d postgres:15.5-alpine
# Interact with a PostgreSQL database running inside a Docker container named
# 'postgres'. Open an interactive terminal (`psql`) as the 'root' user for
# executing SQL commands.
docker exec -it postgres psql -U root

# Try the following query in the shell.
SELECT NOW();

Create .envrc

touch .envrc
echo "export GREENLIGHT_DB_DSN=postgres://pengyuc:password@localhost:5432/greenlight?sslmode=disable" >> .envrc

Run bootstrap.sh to create db, role, extension and migrate the db

bash bootstrap.sh

Style Guide

While cases and spaces don't make a difference in the SQL engine, except for the size of the raw file, I aim to stay consistent and enhance readability whenever possible, just as in my other projects.

  • Use UpperCase for table names, which might be slightly different from what's in the book.
  • Use lower_snake_case for column names.
  • Capitalize the keywords in Postgres.

Visual Studio Code extensions

code --install-extension esbenp.prettier-vscode
code --install-extension foxundermoon.shell-format
code --install-extension golang.go

Appendix

Add new migration scripts

migrate create -ext sql -dir migrations -seq <new_script_file_name>

About


Languages

Language:Go 94.4%Language:Makefile 3.7%Language:Shell 1.9%