Skip to content
This repository was archived by the owner on Mar 12, 2023. It is now read-only.

Project Restructure #78

Merged
merged 11 commits into from
Jan 8, 2022
12 changes: 5 additions & 7 deletions .github/workflows/backend-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ on:
pull_request:
jobs:
backend-test:
strategy:
matrix:
go-version: [1.16.x]
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Set up Golang
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
go-version: 1.16
- name: Spin up containers
run: docker compose --env-file example.env up -d
- name: Running Tests
run: make test
run: ENV=example.env make test
48 changes: 36 additions & 12 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"gorm.io/gorm"
)

var DB *gorm.DB

type dbConfig struct {
host string
port string
Expand All @@ -17,28 +19,50 @@ type dbConfig struct {
password string
}

var config = dbConfig{
os.Getenv("POSTGRES_HOST"),
os.Getenv("POSTGRES_PORT"),
os.Getenv("POSTGRES_USER"),
os.Getenv("POSTGRES_DB"),
os.Getenv("POSTGRES_PASSWORD"),
func getConfig() dbConfig {
return dbConfig{
os.Getenv("POSTGRES_HOST"),
os.Getenv("POSTGRES_PORT"),
os.Getenv("POSTGRES_USER"),
os.Getenv("POSTGRES_DB"),
os.Getenv("POSTGRES_PASSWORD"),
}
}

var dns = fmt.Sprintf(
"host=%s port=%s user=%s dbname=%s password=%s sslmode=disable",
config.host, config.port, config.user, config.dbname, config.password)
func getTestConfig() dbConfig {
return dbConfig{
os.Getenv("POSTGRES_HOST"),
os.Getenv("POSTGRES_TEST_PORT"),
os.Getenv("POSTGRES_USER"),
os.Getenv("POSTGRES_TEST_DB"),
os.Getenv("POSTGRES_PASSWORD"),
}
}

func getDns(config dbConfig) string {
return fmt.Sprintf(
"host=%s port=%s user=%s dbname=%s password=%s sslmode=disable",
config.host, config.port, config.user, config.dbname, config.password)
}

func Init() error {
DB, err := gorm.Open(postgres.Open(dns), &gorm.Config{})
config := getConfig()
dns := getDns(config)
db, err := gorm.Open(postgres.Open(dns), &gorm.Config{})
if err != nil {
return err
}
DB = db
return nil
}

err = initGormUserStore(DB)
func InitTest() error {
config := getTestConfig()
dns := getDns(config)
db, err := gorm.Open(postgres.Open(dns), &gorm.Config{})
if err != nil {
return err
}

DB = db
return nil
}
76 changes: 0 additions & 76 deletions database/gorm_user_store.go

This file was deleted.

32 changes: 0 additions & 32 deletions database/users.go

This file was deleted.

10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ services:
ports:
- ${POSTGRES_PORT}:5432

db-test:
image: postgres
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_TEST_DB}
ports:
- ${POSTGRES_TEST_PORT}:5432

adminer:
image: adminer
restart: always
Expand Down
7 changes: 5 additions & 2 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# copy and rename this file to '.env' to take effect
BACKEND_PORT=3000
ADMINER_PORT=1337
USE_DB=false
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_TEST_PORT=5433
POSTGRES_USER=admin
POSTGRES_DB=speedrunwebsite
POSTGRES_DB=leaderboardsmain
POSTGRES_TEST_DB=leaderboardtest
POSTGRES_PASSWORD=example
ADMINER_PORT=1337
9 changes: 0 additions & 9 deletions handlers/common.go

This file was deleted.

19 changes: 0 additions & 19 deletions handlers/ping.go

This file was deleted.

Loading