Skip to content

Commit 6aca912

Browse files
authored
Feature/db client (#13)
* adds db client layer * updates oapi spec * updates readme commands * adds golang-ci linter config --------- Signed-off-by: Amit Singh <[email protected]>
1 parent 0d64faa commit 6aca912

File tree

16 files changed

+222
-25
lines changed

16 files changed

+222
-25
lines changed

.github/workflows/go.yml

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build a golang project
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
33

4-
name: Go
4+
name: Tests
55

66
on:
77
push:
@@ -10,8 +10,7 @@ on:
1010
branches: [ "main" ]
1111

1212
jobs:
13-
14-
build:
13+
lint_and_test:
1514
runs-on: ubuntu-latest
1615
steps:
1716
- uses: actions/checkout@v4
@@ -24,5 +23,8 @@ jobs:
2423
- name: Golangci-lint
2524
uses: golangci/[email protected]
2625

27-
- name: Test
28-
run: make acceptance-test
26+
- name: Unit Tests
27+
run: make unit-tests
28+
29+
- name: Acceptance Tests
30+
run: make acceptance-tests

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
issues:
2+
exclude:
3+
- (.*)_test\.go$

Makefile

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
CNPG_VERSION ?= "1.24.0"
2+
PG_HOST ?= "http://127.0.0.1"
23
PG_USER_PASSWORD ?= "test_123"
34
SERVER_PORT ?= 8070
45

56
# common env setup
7+
export PG_SERVER=$(PG_HOST):$(SERVER_PORT)
68
export PG_USER_PASSWORD
79
export PORT=$(SERVER_PORT)
810

@@ -13,17 +15,25 @@ codegen:
1315
go mod tidy
1416

1517
lint: codegen
16-
go run github.com/golangci/golangci-lint/cmd/golangci-lint run
18+
go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./...
1719

18-
build: codegen lint
20+
build: codegen
1921
go build
2022

21-
start: build
23+
unit-tests:
24+
go run github.com/onsi/ginkgo/v2/ginkgo run --skip-package=acceptance ./...
25+
26+
acceptance-tests: build
2227
chmod +x ./source-score
23-
./source-score &
28+
( \
29+
./source-score & BG_PID=$$!; \
30+
trap "echo 'terminating the app'; kill $$BG_PID" EXIT; \
31+
echo "app running with PID $$BG_PID"; \
32+
go run github.com/onsi/ginkgo/v2/ginkgo run acceptance/...; \
33+
)
2434

25-
acceptance-test: start
26-
go run github.com/onsi/ginkgo/v2/ginkgo run ./...
35+
start: codegen
36+
go run main.go
2737

2838
minikube-cleanup:
2939
@if minikube status > /dev/null 2>&1; then \

api/source-score.yaml

+22-1
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ components:
110110
properties:
111111
name:
112112
type: string
113+
x-oapi-codegen-extra-tags:
114+
binding: required
113115
summary:
114116
type: string
117+
x-oapi-codegen-extra-tags:
118+
binding: required
115119
tags:
116120
type: string
121+
x-oapi-codegen-extra-tags:
122+
binding: required
117123
uri:
118124
type: string
125+
x-oapi-codegen-extra-tags:
126+
binding: required
119127

120128
Source:
121129
type: object
@@ -128,14 +136,27 @@ components:
128136
properties:
129137
name:
130138
type: string
139+
x-oapi-codegen-extra-tags:
140+
binding: required
131141
score:
132142
type: integer
133143
format: double
144+
x-oapi-codegen-extra-tags:
145+
binding: required
134146
uriDigest:
135147
type: string
148+
x-oapi-codegen-extra-tags:
149+
binding: required
150+
gorm: primaryKey
136151
summary:
137152
type: string
153+
x-oapi-codegen-extra-tags:
154+
binding: required
138155
tags:
139156
type: string
157+
x-oapi-codegen-extra-tags:
158+
binding: required
140159
uri:
141-
type: string
160+
type: string
161+
x-oapi-codegen-extra-tags:
162+
binding: required

go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ require (
1111
github.com/onsi/gomega v1.34.2
1212
)
1313

14+
require (
15+
github.com/jackc/pgpassfile v1.0.0 // indirect
16+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
17+
github.com/jackc/pgx/v5 v5.5.5 // indirect
18+
github.com/jackc/puddle/v2 v2.2.1 // indirect
19+
github.com/jinzhu/inflection v1.0.0 // indirect
20+
github.com/jinzhu/now v1.1.5 // indirect
21+
github.com/joho/godotenv v1.5.1 // indirect
22+
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
23+
)
24+
1425
require (
1526
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
1627
4d63.com/gochecknoglobals v0.2.1 // indirect
@@ -105,6 +116,7 @@ require (
105116
github.com/hashicorp/go-version v1.7.0 // indirect
106117
github.com/hashicorp/hcl v1.0.0 // indirect
107118
github.com/hexops/gotextdiff v1.0.3 // indirect
119+
github.com/ilyakaznacheev/cleanenv v1.5.0
108120
github.com/inconshreveable/mousetrap v1.1.0 // indirect
109121
github.com/invopop/yaml v0.3.1 // indirect
110122
github.com/jgautheron/goconst v1.7.1 // indirect
@@ -227,6 +239,8 @@ require (
227239
gopkg.in/ini.v1 v1.67.0 // indirect
228240
gopkg.in/yaml.v2 v2.4.0 // indirect
229241
gopkg.in/yaml.v3 v3.0.1 // indirect
242+
gorm.io/driver/postgres v1.5.11
243+
gorm.io/gorm v1.25.12
230244
honnef.co/go/tools v0.5.1 // indirect
231245
mvdan.cc/gofumpt v0.7.0 // indirect
232246
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect

go.sum

+23
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ github.com/Antonboom/nilnil v1.0.0/go.mod h1:fDJ1FSFoLN6yoG65ANb1WihItf6qt9PJVTn
4646
github.com/Antonboom/testifylint v1.5.0 h1:dlUIsDMtCrZWUnvkaCz3quJCoIjaGi41GzjPBGkkJ8A=
4747
github.com/Antonboom/testifylint v1.5.0/go.mod h1:wqaJbu0Blb5Wag2wv7Z5xt+CIV+eVLxtGZrlK13z3AE=
4848
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
49+
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
4950
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
5051
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
5152
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
@@ -344,16 +345,32 @@ github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSo
344345
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
345346
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
346347
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
348+
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
349+
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
347350
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
348351
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
349352
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
350353
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
354+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
355+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
356+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
357+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
358+
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
359+
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
360+
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
361+
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
351362
github.com/jgautheron/goconst v1.7.1 h1:VpdAG7Ca7yvvJk5n8dMwQhfEZJh95kl/Hl9S1OI5Jkk=
352363
github.com/jgautheron/goconst v1.7.1/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4=
353364
github.com/jingyugao/rowserrcheck v1.1.1 h1:zibz55j/MJtLsjP1OF4bSdgXxwL1b+Vn7Tjzq7gFzUs=
354365
github.com/jingyugao/rowserrcheck v1.1.1/go.mod h1:4yvlZSDb3IyDTUZJUmpZfm2Hwok+Dtp+nu2qOq+er9c=
366+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
367+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
368+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
369+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
355370
github.com/jjti/go-spancheck v0.6.2 h1:iYtoxqPMzHUPp7St+5yA8+cONdyXD3ug6KK15n7Pklk=
356371
github.com/jjti/go-spancheck v0.6.2/go.mod h1:+X7lvIrR5ZdUTkxFYqzJ0abr8Sb5LOo80uOhWNqIrYA=
372+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
373+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
357374
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
358375
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
359376
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
@@ -1056,6 +1073,10 @@ gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d/go.mod h1:K4uyk7z7BCEPqu6E+C
10561073
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10571074
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10581075
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1076+
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
1077+
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
1078+
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
1079+
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
10591080
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
10601081
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
10611082
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
@@ -1070,6 +1091,8 @@ mvdan.cc/gofumpt v0.7.0/go.mod h1:txVFJy/Sc/mvaycET54pV8SW8gWxTlUuGHVEcncmNUo=
10701091
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f h1:lMpcwN6GxNbWtbpI1+xzFLSW8XzX0u72NttUGVFjO3U=
10711092
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f/go.mod h1:RSLa7mKKCNeTTMHBw5Hsy2rfJmd6O2ivt9Dw9ZqCQpQ=
10721093
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
1094+
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
1095+
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
10731096
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
10741097
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
10751098
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=

helm/cnpg-database/templates/cnpg-all.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@ spec:
5656
containers:
5757
- name: adminer
5858
image: adminer:4.8.1
59+
limits:
60+
memory: "256Mi"
61+
requests:
62+
cpu: "50m"
5963
ports:
6064
- containerPort: 8080

main.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66
"github.com/gin-gonic/gin"
77

88
"source-score/pkg/api"
9+
"source-score/pkg/conf"
910
)
1011

1112
func main() {
13+
conf.LoadConfig()
14+
1215
server := gin.Default()
1316
api.RegisterHandlers(server, api.NewRouter())
1417

pkg/api/router.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ import (
99
)
1010

1111
type router struct {
12-
pingHandler *handlers.PingHandler
12+
pingHandler *handlers.PingHandler
1313
}
1414

1515
func NewRouter() *router {
1616
return &router{
17-
pingHandler: handlers.NewPingHandler(),
17+
pingHandler: handlers.NewPingHandler(),
1818
}
1919
}
2020

2121
func (r *router) CreateSource(ctx *gin.Context) {
22+
body := CreateSource{}
23+
// using BindJson method to serialize body with struct
24+
if err := ctx.BindJSON(&body); err != nil {
25+
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
26+
_ = ctx.AbortWithError(http.StatusBadRequest, err)
27+
return
28+
}
2229
log.Println("unimplemented")
2330
}
2431

pkg/api/server.gen.go

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/conf/conf.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package conf
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
"github.com/ilyakaznacheev/cleanenv"
8+
)
9+
10+
type conf struct {
11+
PgUserPassword string `env:"PG_USER_PASSWORD" yaml:"PG_USER_PASSWORD" env-required:"true"`
12+
PgServer string `env:"PG_SERVER" yaml:"PG_SERVER" env-required:"true"`
13+
}
14+
15+
var Cfg conf
16+
17+
func LoadConfig() {
18+
if envPath, ok := os.LookupEnv("DOTENV_PATH"); ok {
19+
file, err := os.Open(envPath)
20+
if err != nil {
21+
log.Fatalf("error while reading dotenv file: %s :: %s", envPath, err)
22+
}
23+
24+
err = cleanenv.ParseYAML(file, &Cfg)
25+
if err != nil {
26+
log.Fatalf("error while parsing dotenv file: %s :: %s", envPath, err)
27+
}
28+
} else {
29+
err := cleanenv.ReadEnv(&Cfg)
30+
if err != nil {
31+
log.Fatalf("error while reading config environment variables :: %s", err)
32+
}
33+
}
34+
}

pkg/conf/conf.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PG_USER_PASSWORD: env-pwd
2+
PG_SERVER: env-server

pkg/conf/conf_suite_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package conf_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestConf(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Conf Test Suite")
13+
}

0 commit comments

Comments
 (0)