Skip to content

Commit

Permalink
Feature/db client (#13)
Browse files Browse the repository at this point in the history
* adds db client layer
* updates oapi spec
* updates readme commands
* adds golang-ci linter config

---------

Signed-off-by: Amit Singh <[email protected]>
  • Loading branch information
semmet95 authored Dec 15, 2024
1 parent 0d64faa commit 6aca912
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 25 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go
name: Tests

on:
push:
Expand All @@ -10,8 +10,7 @@ on:
branches: [ "main" ]

jobs:

build:
lint_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -24,5 +23,8 @@ jobs:
- name: Golangci-lint
uses: golangci/[email protected]

- name: Test
run: make acceptance-test
- name: Unit Tests
run: make unit-tests

- name: Acceptance Tests
run: make acceptance-tests
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
issues:
exclude:
- (.*)_test\.go$
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
CNPG_VERSION ?= "1.24.0"
PG_HOST ?= "http://127.0.0.1"
PG_USER_PASSWORD ?= "test_123"
SERVER_PORT ?= 8070

# common env setup
export PG_SERVER=$(PG_HOST):$(SERVER_PORT)
export PG_USER_PASSWORD
export PORT=$(SERVER_PORT)

Expand All @@ -13,17 +15,25 @@ codegen:
go mod tidy

lint: codegen
go run github.com/golangci/golangci-lint/cmd/golangci-lint run
go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./...

build: codegen lint
build: codegen
go build

start: build
unit-tests:
go run github.com/onsi/ginkgo/v2/ginkgo run --skip-package=acceptance ./...

acceptance-tests: build
chmod +x ./source-score
./source-score &
( \
./source-score & BG_PID=$$!; \
trap "echo 'terminating the app'; kill $$BG_PID" EXIT; \
echo "app running with PID $$BG_PID"; \
go run github.com/onsi/ginkgo/v2/ginkgo run acceptance/...; \
)

acceptance-test: start
go run github.com/onsi/ginkgo/v2/ginkgo run ./...
start: codegen
go run main.go

minikube-cleanup:
@if minikube status > /dev/null 2>&1; then \
Expand Down
23 changes: 22 additions & 1 deletion api/source-score.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,20 @@ components:
properties:
name:
type: string
x-oapi-codegen-extra-tags:
binding: required
summary:
type: string
x-oapi-codegen-extra-tags:
binding: required
tags:
type: string
x-oapi-codegen-extra-tags:
binding: required
uri:
type: string
x-oapi-codegen-extra-tags:
binding: required

Source:
type: object
Expand All @@ -128,14 +136,27 @@ components:
properties:
name:
type: string
x-oapi-codegen-extra-tags:
binding: required
score:
type: integer
format: double
x-oapi-codegen-extra-tags:
binding: required
uriDigest:
type: string
x-oapi-codegen-extra-tags:
binding: required
gorm: primaryKey
summary:
type: string
x-oapi-codegen-extra-tags:
binding: required
tags:
type: string
x-oapi-codegen-extra-tags:
binding: required
uri:
type: string
type: string
x-oapi-codegen-extra-tags:
binding: required
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ require (
github.com/onsi/gomega v1.34.2
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)

require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
4d63.com/gochecknoglobals v0.2.1 // indirect
Expand Down Expand Up @@ -105,6 +116,7 @@ require (
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/jgautheron/goconst v1.7.1 // indirect
Expand Down Expand Up @@ -227,6 +239,8 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.11
gorm.io/gorm v1.25.12
honnef.co/go/tools v0.5.1 // indirect
mvdan.cc/gofumpt v0.7.0 // indirect
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect
Expand Down
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ github.com/Antonboom/nilnil v1.0.0/go.mod h1:fDJ1FSFoLN6yoG65ANb1WihItf6qt9PJVTn
github.com/Antonboom/testifylint v1.5.0 h1:dlUIsDMtCrZWUnvkaCz3quJCoIjaGi41GzjPBGkkJ8A=
github.com/Antonboom/testifylint v1.5.0/go.mod h1:wqaJbu0Blb5Wag2wv7Z5xt+CIV+eVLxtGZrlK13z3AE=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down Expand Up @@ -344,16 +345,32 @@ github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSo
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jgautheron/goconst v1.7.1 h1:VpdAG7Ca7yvvJk5n8dMwQhfEZJh95kl/Hl9S1OI5Jkk=
github.com/jgautheron/goconst v1.7.1/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4=
github.com/jingyugao/rowserrcheck v1.1.1 h1:zibz55j/MJtLsjP1OF4bSdgXxwL1b+Vn7Tjzq7gFzUs=
github.com/jingyugao/rowserrcheck v1.1.1/go.mod h1:4yvlZSDb3IyDTUZJUmpZfm2Hwok+Dtp+nu2qOq+er9c=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jjti/go-spancheck v0.6.2 h1:iYtoxqPMzHUPp7St+5yA8+cONdyXD3ug6KK15n7Pklk=
github.com/jjti/go-spancheck v0.6.2/go.mod h1:+X7lvIrR5ZdUTkxFYqzJ0abr8Sb5LOo80uOhWNqIrYA=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down Expand Up @@ -1056,6 +1073,10 @@ gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d/go.mod h1:K4uyk7z7BCEPqu6E+C
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=
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand All @@ -1070,6 +1091,8 @@ mvdan.cc/gofumpt v0.7.0/go.mod h1:txVFJy/Sc/mvaycET54pV8SW8gWxTlUuGHVEcncmNUo=
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f h1:lMpcwN6GxNbWtbpI1+xzFLSW8XzX0u72NttUGVFjO3U=
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f/go.mod h1:RSLa7mKKCNeTTMHBw5Hsy2rfJmd6O2ivt9Dw9ZqCQpQ=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
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=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
4 changes: 4 additions & 0 deletions helm/cnpg-database/templates/cnpg-all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ spec:
containers:
- name: adminer
image: adminer:4.8.1
limits:
memory: "256Mi"
requests:
cpu: "50m"
ports:
- containerPort: 8080
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"github.com/gin-gonic/gin"

"source-score/pkg/api"
"source-score/pkg/conf"
)

func main() {
conf.LoadConfig()

server := gin.Default()
api.RegisterHandlers(server, api.NewRouter())

Expand Down
11 changes: 9 additions & 2 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ import (
)

type router struct {
pingHandler *handlers.PingHandler
pingHandler *handlers.PingHandler
}

func NewRouter() *router {
return &router{
pingHandler: handlers.NewPingHandler(),
pingHandler: handlers.NewPingHandler(),
}
}

func (r *router) CreateSource(ctx *gin.Context) {
body := CreateSource{}
// using BindJson method to serialize body with struct
if err := ctx.BindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
_ = ctx.AbortWithError(http.StatusBadRequest, err)
return
}
log.Println("unimplemented")
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/api/server.gen.go

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

34 changes: 34 additions & 0 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package conf

import (
"log"
"os"

"github.com/ilyakaznacheev/cleanenv"
)

type conf struct {
PgUserPassword string `env:"PG_USER_PASSWORD" yaml:"PG_USER_PASSWORD" env-required:"true"`
PgServer string `env:"PG_SERVER" yaml:"PG_SERVER" env-required:"true"`
}

var Cfg conf

func LoadConfig() {
if envPath, ok := os.LookupEnv("DOTENV_PATH"); ok {
file, err := os.Open(envPath)
if err != nil {
log.Fatalf("error while reading dotenv file: %s :: %s", envPath, err)
}

err = cleanenv.ParseYAML(file, &Cfg)
if err != nil {
log.Fatalf("error while parsing dotenv file: %s :: %s", envPath, err)
}
} else {
err := cleanenv.ReadEnv(&Cfg)
if err != nil {
log.Fatalf("error while reading config environment variables :: %s", err)
}
}
}
2 changes: 2 additions & 0 deletions pkg/conf/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PG_USER_PASSWORD: env-pwd
PG_SERVER: env-server
13 changes: 13 additions & 0 deletions pkg/conf/conf_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package conf_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConf(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Conf Test Suite")
}
Loading

0 comments on commit 6aca912

Please sign in to comment.