Skip to content

Commit

Permalink
Merge pull request #11 from voltgizerz/felix_refactor_gql
Browse files Browse the repository at this point in the history
Refactor project structure
  • Loading branch information
voltgizerz committed Oct 29, 2023
2 parents 5f99e64 + d31a064 commit 52d6665
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 45 deletions.
53 changes: 39 additions & 14 deletions .github/workflows/revive-lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Lint & Test
name: Code Checker
on:
pull_request:
branches:
Expand All @@ -9,32 +9,57 @@ on:
- master
- develop
paths:
- '*/.go'
- 'revive.toml'
- '.github/workflows/revive-lint.yml'

jobs:

lint:
name: Lint & Test Service
name: 👩🏼‍🌾 Lint and Test
runs-on: ubuntu-latest
steps:

- name: Set up Go
uses: actions/setup-go@v2
- uses: actions/checkout@v2
- name: 🔧 Print current working dir
run: |
ls -la $PWD
ls -la $GITHUB_WORKSPACE
- uses: actions/setup-go@v2
with:
go-version: '1.17'
go-version: '1.21.1'

- name: ⚙️ Setup GO environment
run: |
go mod download
- name: 👷 Build
run: go build ./cmd/server.go

- name: 🧪 Run Unit tests
run: |
sudo go test ./... -coverprofile coverage.out -covermode count
sudo go tool cover -func coverage.out
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Run Test
run: go test -v -cover ./...
- name: 🔎 Quality Gate - Test coverage shall be above threshold
env:
TESTCOVERAGE_THRESHOLD: 1
run: |
echo "Quality Gate: checking test coverage is above threshold ..."
echo "Threshold : $TESTCOVERAGE_THRESHOLD %"
totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
echo "Current test coverage : $totalCoverage %"
if (( $(echo "$totalCoverage $TESTCOVERAGE_THRESHOLD" | awk '{print ($1 > $2)}') )); then
echo "OK"
else
echo "Current test coverage is below threshold. Please add more unit tests or adjust threshold to a lower value."
echo "Failed"
exit 1
fi
- name: Run Revive Action
- name: 👨‍🎤 Run Revive Action
uses: docker://morphy/revive-action:v2
with:
config: revive.toml
continue-on-error: true

- name: Check formatting
- name: 👨‍🎤 Check formatting
run: gofmt -l .
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.env
*.env
config.production.yml
12 changes: 8 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"os/signal"

"github.com/go-graphql/config"
"github.com/go-graphql/database"
"github.com/go-graphql/internal/app/repository"
"github.com/go-graphql/internal/app/service"
"github.com/go-graphql/logger"
"github.com/go-graphql/pkg/env"
"github.com/go-graphql/pkg/gqlgen"
"github.com/go-graphql/pkg/logger"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/go-graphql/internal/app/graph/generated"
Expand All @@ -31,10 +34,11 @@ func main() {
// This goroutine will block until a signal is received.
go handleSignal(c)

config.LoadENV()
env.LoadENV()
_ = config.NewConfig()

// initialize database
db, err := config.InitDB()
db, err := database.InitDB()
if err != nil {
logger.Log.Error(err)
}
Expand All @@ -61,5 +65,5 @@ func main() {
}))

// initialize GQL
config.InitGQL(srv)
gqlgen.InitGQL(srv)
}
10 changes: 10 additions & 0 deletions config/config-development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
app:
name: 'Go GQL'
version: '1.0.0'
is_use_fake_data: true

http:
port: '8080'

database:
pool_max: 2
46 changes: 46 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"github.com/go-graphql/pkg/logger"
"github.com/ilyakaznacheev/cleanenv"
)

type (
Config struct {

Check warning on line 9 in config/config.go

View workflow job for this annotation

GitHub Actions / 👩🏼‍🌾 Lint and Test

exported type Config should have comment or be unexported
App `yaml:"app"`
HTTP `yaml:"http"`
Database `yaml:"database"`
}

App struct {

Check warning on line 15 in config/config.go

View workflow job for this annotation

GitHub Actions / 👩🏼‍🌾 Lint and Test

exported type App should have comment or be unexported
Name string `env-required:"true" yaml:"name"`
Version string `env-required:"true" yaml:"version"`
IsUseFakeData bool `env-required:"true" yaml:"is_use_fake_data"`
}

// HTTP -.
HTTP struct {
Port string `env-required:"true" yaml:"port"`
}

Database struct {

Check warning on line 26 in config/config.go

View workflow job for this annotation

GitHub Actions / 👩🏼‍🌾 Lint and Test

exported type Database should have comment or be unexported
PoolMax int `env-required:"true" yaml:"pool_max"`
}
)

// NewConfig returns app config.
func NewConfig() *Config {
cfg := &Config{}

err := cleanenv.ReadConfig("./config/config-development.yml", cfg) // Change this base on your env
if err != nil {
logger.Log.Fatalln(err)
}

err = cleanenv.ReadEnv(cfg)
if err != nil {
logger.Log.Fatalln(err)
}

return cfg
}
4 changes: 2 additions & 2 deletions config/database.go → database/database.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package database

import (
"database/sql"
Expand All @@ -7,7 +7,7 @@ import (
"time"

// database
"github.com/go-graphql/logger"
"github.com/go-graphql/pkg/logger"
_ "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/go-chi/chi v1.5.4
github.com/go-sql-driver/mysql v1.6.0
github.com/joho/godotenv v1.4.0
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
github.com/vektah/gqlparser v1.3.1
github.com/vektah/gqlparser/v2 v2.5.4
)

require github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect
require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)

require (
github.com/agnivade/levenshtein v1.1.1 // indirect
Expand All @@ -25,6 +29,7 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang/mock v1.6.0
github.com/gorilla/websocket v1.5.0 // indirect
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/99designs/gqlgen v0.17.34 h1:5cS5/OKFguQt+Ws56uj9FlG2xm1IlcJWNF2jrMIKYFQ=
github.com/99designs/gqlgen v0.17.34/go.mod h1:Axcd3jIFHBVcqzixujJQr1wGqE+lGTpz6u4iZBZg1G8=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
Expand Down Expand Up @@ -28,8 +30,10 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru/v2 v2.0.3 h1:kmRrRLlInXvng0SmLxmQpQkpbYAvcXm7NPDrgxJa9mE=
github.com/hashicorp/golang-lru/v2 v2.0.3/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -109,3 +113,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
2 changes: 1 addition & 1 deletion internal/app/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ var (
ErrorNoAuthorization = errors.New("authorization is not valid")

// ErrorAuth - .
ErrorAuth = errors.New("username or Password invalid")
ErrorAuth = errors.New("username or password invalid")
)
2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/authorization.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/create_pokemon.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/delete_pokemon.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/pokemon.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/pokemons.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/types.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/graph/resolvers/update_pokemon.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/app/repository/pokemon.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"strconv"

"github.com/go-graphql/config"
"github.com/go-graphql/internal/app/models"
"github.com/go-graphql/database"
)

// PokemonRepositoryInterface - .
Expand All @@ -20,11 +20,11 @@ type PokemonRepositoryInterface interface {

// PokemonRepository -
type PokemonRepository struct {
DB *config.Database
DB *database.Database
}

// NewPokemonRepository -
func NewPokemonRepository(DB *config.Database) PokemonRepositoryInterface {
func NewPokemonRepository(DB *database.Database) PokemonRepositoryInterface {
return &PokemonRepository{
DB: DB,
}
Expand Down
6 changes: 3 additions & 3 deletions internal/app/repository/type.repository.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package repository

import (
"github.com/go-graphql/config"
"github.com/go-graphql/internal/app/models"
"github.com/go-graphql/database"
)

// TypeRepositoryInterface - .
Expand All @@ -13,11 +13,11 @@ type TypeRepositoryInterface interface {

// TypeRepository -
type TypeRepository struct {
DB *config.Database
DB *database.Database
}

// NewTypeRepository -
func NewTypeRepository(DB *config.Database) TypeRepositoryInterface {
func NewTypeRepository(DB *database.Database) TypeRepositoryInterface {
return &TypeRepository{
DB: DB,
}
Expand Down
4 changes: 2 additions & 2 deletions config/env.go → pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config
package env

import (
"github.com/go-graphql/logger"
"github.com/go-graphql/pkg/logger"
"github.com/joho/godotenv"
)

Expand Down
4 changes: 2 additions & 2 deletions config/gqlgen.go → pkg/gqlgen/gqlgen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package gqlgen

import (
"context"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-graphql/internal/app/auth"
"github.com/go-graphql/logger"
"github.com/go-graphql/pkg/logger"
"github.com/sirupsen/logrus"
"github.com/vektah/gqlparser/gqlerror"
)
Expand Down
Loading

0 comments on commit 52d6665

Please sign in to comment.