Skip to content

Commit

Permalink
Merge pull request #2 from AoTTG-2/1-workshop-service
Browse files Browse the repository at this point in the history
#1 Implemented workshop service
  • Loading branch information
Jagerente authored Jan 17, 2025
2 parents 42d1f57 + 7180434 commit 3975e37
Show file tree
Hide file tree
Showing 74 changed files with 9,251 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Auto-generated config

#############################
# App configuration
#############################

LOCAL=true

# Allowed to be empty
# Description:
# List of allowed origins for CORS
LOCAL_ORIGINS=http://localhost:*

#############################
# Logger configuration
#############################

# Description:
# https://pkg.go.dev/github.com/rs/zerolog@v1.33.0#Level
LOG_LEVEL=-1

#############################
# Router configuration
#############################

SERVER_PORT=8080

ROUTER_DEBUG=true

AUTH_DEBUG=true

#############################
# Redis configuration
#############################

REDIS_HOST=redis:6379

REDIS_DB=0

# Allowed to be empty
REDIS_USERNAME=

# Allowed to be empty
REDIS_PASSWORD=

#############################
# Postgres configuration
#############################

POSTGRES_HOST=postgres:5432

POSTGRES_USERNAME=postgres

POSTGRES_PASSWORD=12345

POSTGRES_DATABASE=workshop

# Allowed to be empty
POSTGRES_PARAMS=

# Allowed to be empty
POSTGRES_MIGRATIONS_PATH=

# Description:
# Whether to run app-driver migrations on start
POSTGRES_DO_MIGRATE=false
2 changes: 2 additions & 0 deletions .env.postgres_test.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST_POSTGRES_CONN_STRING=postgres://postgres:12345@postgres:5432/test_workshop?sslmode=disable
TEST_POSTGRES_MIGRATIONS_PATH=/workshop/migrations/postgres
49 changes: 49 additions & 0 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: PR Tests

on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev

jobs:
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:alpine
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 12345
POSTGRES_DB: test_workshop
options: >-
--health-cmd "pg_isready -d postgres -U postgres"
--health-interval 2s
--health-timeout 60s
--health-retries 15
--health-start-period 80s
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ^1.23.4
id: go

- name: Checkout
uses: actions/checkout@v3

- name: Test Repository
env:
TEST_POSTGRES_CONN_STRING: postgres://postgres:12345@localhost:5432/test_workshop?sslmode=disable
# Local run with `act`
# TEST_POSTGRES_MIGRATIONS_PATH: /mnt/c/Users/Jagerente/Documents/GitHub/workshop-service/migrations/postgres
TEST_POSTGRES_MIGRATIONS_PATH: ${{ github.workspace }}/migrations/postgres
run: go test -v ./internal/repository/driver/postgres
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
###########################
# Docker / Local deploy
###########################

# (Re-)start container without rebuild and cleaning dependencies (volumes/images/networks)
restart: down up
# Use this to rebuild and clean old junk (volumes/images/networks)
rebuild: clean build

up:
docker compose up -d
down:
docker compose down
build:
docker compose up -d --build
clean:
docker compose down -v --remove-orphans
docker image prune -f
docker volume prune -f

###########################
# Documentation
###########################

# Generate go app env documentation
godoc:
go run cmd\docs\main.go

# Generate go app swagger documentation
swag:
swag fmt
swag init --ot go,json --parseInternal -g ./cmd/workshop/main.go


###########################
# Tests and Benchmarks
###########################

DOCKER_IMAGE_NAME := workshop-test-runner
DOCKERFILE_PATH := ./docker/test/Dockerfile
DOCKER_MOUNTS := -v .:/workshop

TEST_CMD := go test
TEST_ARGS := -v

BENCH_CMD := go test -bench=. -run=^\#
BENCH_ARGS := -test.count 5 -test.benchmem -test.v

# Init docker image before usage
test_build:
docker build -t $(DOCKER_IMAGE_NAME) -f $(DOCKERFILE_PATH) .

# Run all tests
test_all: test_build test_postgres

# Tests
test_postgres:
docker run --rm --network host --env-file .env.postgres_test $(DOCKER_MOUNTS) $(DOCKER_IMAGE_NAME) sh -c "$(TEST_CMD) $(TEST_ARGS) ./internal/repository/driver/postgres"
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Workshop Service

## Local run

### Requirements

- Docker
- Windows: https://www.docker.com/
- Make CLI
- Windows: https://gnuwin32.sourceforge.net/downlinks/make.php

### Configuration

1. Make a copy of `.env.dist` file and name it `.env`.
2. Configure `.env` if you need.

### Run

1. Run `make rebuild` cmd.
2. To restart container run `make restart`.

> Keep in mind that `make rebuild` will also prune unused images and volumes.
- Workshop service is available at `http://localhost:8080/` by default.
- Swagger documentation is available at `http://localhost:14000/` by default.
- DB connection data is inside `.env` file.
- With `AUTH_DEBUG` mode you are available to impersonate any user / roles.
- Use `X-Debug-User-Roles` and `X-Debug-User-ID` headers to modify it.
- More info in swagger.

### Tests

To run tests run `make test_all` cmd.

## Project structure

- `cmd` / `internal` / `pkg` are code related directories.
- `docker` - contains all required resources and `Dockerfiles` for building container.
- `docker/postgres/init` - contains initialization scripts for postgres database.
- `docs` - contains auto-generated documentation.
- `migrations` - contains all required migrations.

## Docker Compose structure

- `workshop` is a core service.
- `postgres` is a main DB.
- `migrate` is a migrator image, that applies migrations when container starts.
- `redis` is a main cache. Used for rate-limiting purposes.

## Features

### Posts

- Get One
- Get List

> Only users with `POST_CREATOR` role can access following:
- Create
- Update
- Delete

### Interactions

> Only users with `USER` role can access this.
- Favorite post
- Unfavorite post
- Rate post

### Comments

- Get List

> Only users with `USER` role can access following:
- Add
- Update
- Delete

### Moderation

> Only users with `POST_MODERATOR` role can access this.
- Moderate Post
- Approve / Decline
- Note
- Persistent moderation history
- Get Moderation Actions
25 changes: 25 additions & 0 deletions cmd/docs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"github.com/Jagerente/gocfg"
"github.com/Jagerente/gocfg/pkg/docgens"
"os"
"workshop/internal/config"
)

const outputFile = ".env.dist"

func main() {
cfg := new(config.Config)

file, err := os.Create(outputFile)
if err != nil {
panic(fmt.Errorf("error creating %s file: %v", outputFile, err))
}

cfgManager := gocfg.NewDefault()
if err := cfgManager.GenerateDocumentation(cfg, docgens.NewEnvDocGenerator(file)); err != nil {
panic(err)
}
}
38 changes: 38 additions & 0 deletions cmd/workshop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"github.com/rs/zerolog/log"
_ "workshop/docs"
"workshop/internal/app"
)

// @title Workshop Service
// @version 1.0
//
// @host localhost:8080
// @BasePath /api
//
// @securityDefinitions.apikey DebugUserRoles
// @in header
// @name X-Debug-User-Roles
// @description Force user roles if debug mode is enabled. Split by comma ',' for multiple roles.
//
// @securityDefinitions.apikey DebugUserID
// @in header
// @name X-Debug-User-ID
// @description Force user ID if debug mode is enabled. Required if Debug User Roles is provided.
func main() {
a, err := app.New()
if err != nil {
log.Fatal().Msgf("Failed to create app: %v", err)
}

if err := a.Run(); err != nil {
log.Fatal().Msgf("Failed to run app: %v", err)
}

if err := a.Stop(context.Background()); err != nil {
log.Fatal().Msgf("Failed to stop app: %v", err)
}
}
Loading

0 comments on commit 3975e37

Please sign in to comment.