This commit is contained in:
67
.drone.yml
67
.drone.yml
@@ -1,67 +0,0 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: static_check
|
||||
image: golang:latest
|
||||
commands:
|
||||
- go install honnef.co/go/tools/cmd/staticcheck@latest
|
||||
- cd src && staticcheck ./...
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: lint
|
||||
image: golang:latest
|
||||
commands:
|
||||
- go install golang.org/x/lint/golint@latest
|
||||
- golint ./src/...
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: analyze
|
||||
image: golang:latest
|
||||
commands:
|
||||
- cd src && go vet ./...
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: publish_image
|
||||
image: plugins/docker
|
||||
environment:
|
||||
DOCKER_USERNAME:
|
||||
from_secret: registry_username
|
||||
DOCKER_PASSWORD:
|
||||
from_secret: registry_password
|
||||
commands:
|
||||
- sleep 5
|
||||
- ./deploy/image-build.sh
|
||||
- ./deploy/image-push.sh
|
||||
volumes:
|
||||
- name: docker-sock
|
||||
path: /var/run
|
||||
when:
|
||||
branch:
|
||||
- main
|
||||
|
||||
services:
|
||||
- name: docker
|
||||
image: docker:dind
|
||||
privileged: true
|
||||
volumes:
|
||||
- name: docker-sock
|
||||
path: /var/run
|
||||
- name: etc_hosts
|
||||
path: /etc/hosts
|
||||
|
||||
volumes:
|
||||
- name: gopath
|
||||
temp: {}
|
||||
- name: docker-sock
|
||||
temp: {}
|
||||
- name: etc_hosts
|
||||
host:
|
||||
path: /etc/hosts
|
||||
112
.woodpecker/workflow.yaml
Normal file
112
.woodpecker/workflow.yaml
Normal file
@@ -0,0 +1,112 @@
|
||||
when:
|
||||
- event: push
|
||||
branch: develop
|
||||
|
||||
steps:
|
||||
- name: analyze
|
||||
image: golang:1.24
|
||||
commands:
|
||||
- echo "Analyze"
|
||||
- cd src && go vet ./...
|
||||
- name: static check
|
||||
image: golang:1.24
|
||||
commands:
|
||||
- echo "Static Check"
|
||||
- go install honnef.co/go/tools/cmd/staticcheck@latest
|
||||
- cd src && staticcheck ./...
|
||||
- name: lint
|
||||
image: golang:1.24
|
||||
commands:
|
||||
- echo "Lint"
|
||||
- go install golang.org/x/lint/golint@latest
|
||||
- golint ./src/...
|
||||
- name: build image
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
username: keedosn
|
||||
# from_secret: docker_username
|
||||
password: Lokalne5Oprogramowanie@
|
||||
# from_secret: docker_password
|
||||
repo: git.ego.freeddns.org/egommerce/catalog-svc
|
||||
registry: git.ego.freeddns.org
|
||||
dockerfile: Dockerfile.builder
|
||||
tag: dev
|
||||
- name: push image
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
username: keedosn
|
||||
# from_secret: docker_username
|
||||
password: Lokalne5Oprogramowanie@
|
||||
# from_secret: docker_password
|
||||
repo: git.ego.freeddns.org/egommerce/catalog-svc
|
||||
registry: git.ego.freeddns.org
|
||||
dockerfile: Dockerfile.target
|
||||
tag: dev
|
||||
# commands:
|
||||
# - sleep 5
|
||||
# - ./deploy/image-build.sh
|
||||
# - ./deploy/image-push.sh
|
||||
|
||||
# steps:
|
||||
# - name: static_check
|
||||
# image: golang:latest
|
||||
# commands:
|
||||
# - go install honnef.co/go/tools/cmd/staticcheck@latest
|
||||
# - cd src && staticcheck ./...
|
||||
# volumes:
|
||||
# - name: gopath
|
||||
# path: /go
|
||||
|
||||
# - name: lint
|
||||
# image: golang:latest
|
||||
# commands:
|
||||
# - go install golang.org/x/lint/golint@latest
|
||||
# - golint ./src/...
|
||||
# volumes:
|
||||
# - name: gopath
|
||||
# path: /go
|
||||
|
||||
# - name: analyze
|
||||
# image: golang:latest
|
||||
# commands:
|
||||
# - cd src && go vet ./...
|
||||
# volumes:
|
||||
# - name: gopath
|
||||
# path: /go
|
||||
|
||||
# - name: publish_image
|
||||
# image: plugins/docker
|
||||
# environment:
|
||||
# DOCKER_USERNAME:
|
||||
# from_secret: registry_username
|
||||
# DOCKER_PASSWORD:
|
||||
# from_secret: registry_password
|
||||
# commands:
|
||||
# - sleep 5
|
||||
# - ./deploy/image-build.sh
|
||||
# - ./deploy/image-push.sh
|
||||
# volumes:
|
||||
# - name: docker-sock
|
||||
# path: /var/run
|
||||
# when:
|
||||
# branch:
|
||||
# - main
|
||||
|
||||
# services:
|
||||
# - name: docker
|
||||
# image: docker:dind
|
||||
# privileged: true
|
||||
# volumes:
|
||||
# - name: docker-sock
|
||||
# path: /var/run
|
||||
# - name: etc_hosts
|
||||
# path: /etc/hosts
|
||||
|
||||
# volumes:
|
||||
# - name: gopath
|
||||
# temp: {}
|
||||
# - name: docker-sock
|
||||
# temp: {}
|
||||
# - name: etc_hosts
|
||||
# host:
|
||||
# path: /etc/hosts
|
||||
@@ -3,15 +3,22 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
redis "github.com/go-redis/redis/v8"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
db "github.com/jackc/pgx/v5/pgxpool"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
type (
|
||||
Plugin struct {
|
||||
name string
|
||||
connect PluginConnectFn
|
||||
}
|
||||
|
||||
PluginConnectFn func() any // returns connection handle
|
||||
)
|
||||
|
||||
type PluginManager struct {
|
||||
plugins map[string]any
|
||||
}
|
||||
@@ -22,68 +29,144 @@ func NewPluginManager() *PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *PluginManager) addPlugin(name string, fn PluginFn) {
|
||||
func (pm *PluginManager) addPlugin(name string, fn PluginConnectFn) {
|
||||
pm.plugins[name] = fn()
|
||||
}
|
||||
|
||||
func (pm *PluginManager) getCache() *redis.Client {
|
||||
func (pm *PluginManager) GetCache() *redis.Client {
|
||||
return (pm.plugins["cache"]).(*redis.Client)
|
||||
}
|
||||
|
||||
func (pm *PluginManager) getDatabase() *pgxpool.Pool {
|
||||
func (pm *PluginManager) GetDatabase() *pgxpool.Pool {
|
||||
return (pm.plugins["database"]).(*pgxpool.Pool)
|
||||
}
|
||||
func (pm *PluginManager) getEventbus() *amqp.Channel {
|
||||
|
||||
func (pm *PluginManager) GetEventbus() *amqp.Channel {
|
||||
return (pm.plugins["eventbus"]).(*amqp.Channel)
|
||||
}
|
||||
|
||||
func CachePlugin(cnf *Config) Plugin {
|
||||
// plugin := &Plugin{
|
||||
// name: "cache",
|
||||
// connectFn: func() any {},
|
||||
// afterConnFn: func() any {},
|
||||
// }
|
||||
|
||||
connectFn := func() *redis.Client {
|
||||
log.Println("establishing api-cache connection...")
|
||||
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: cnf.CacheAddr,
|
||||
Username: cnf.CacheUsername,
|
||||
Password: cnf.CachePassword,
|
||||
DB: 0, // TODO
|
||||
DialTimeout: 100 * time.Millisecond, // TODO
|
||||
})
|
||||
}
|
||||
|
||||
// checking if the connection is still alive and try to reconnect when it is not
|
||||
go func(conn *redis.Client) {
|
||||
tick := time.NewTicker(5 * time.Second) // is 5 seconds is not too much?
|
||||
defer tick.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
if err := conn.Ping(context.Background()).Err(); err != nil {
|
||||
log.Println("lost connection with api-cache. Reconnecting...")
|
||||
conn = connectFn()
|
||||
}
|
||||
}
|
||||
}
|
||||
}(connectFn())
|
||||
|
||||
return Plugin{
|
||||
name: "cache",
|
||||
fn: func() any {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: cnf.CacheAddr,
|
||||
Username: cnf.CacheUsername,
|
||||
Password: cnf.CachePassword,
|
||||
DB: 0, // TODO
|
||||
DialTimeout: 100 * time.Millisecond, // TODO
|
||||
})
|
||||
connect: func() any {
|
||||
return connectFn
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DatabasePlugin(cnf *Config) Plugin {
|
||||
connectFn := func() *pgxpool.Pool {
|
||||
log.Println("establishing db-postgres connection...")
|
||||
|
||||
conn, err := pgxpool.New(context.Background(), cnf.DbURL)
|
||||
if err != nil {
|
||||
log.Printf("failed to connect to the database: %s. Err: %s\n", cnf.DbURL, err.Error())
|
||||
return nil
|
||||
// os.Exit(1)
|
||||
}
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
// checking if the connection is still alive and try to reconnect when it is not
|
||||
go func(conn *pgxpool.Pool) {
|
||||
tick := time.NewTicker(5 * time.Second) // is 5 seconds is not too much?
|
||||
defer tick.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
if err := conn.Ping(context.Background()); err != nil {
|
||||
log.Println("lost connection with db-postgres. Reconnecting...")
|
||||
conn = connectFn()
|
||||
}
|
||||
}
|
||||
}
|
||||
}(connectFn())
|
||||
|
||||
return Plugin{
|
||||
name: "database",
|
||||
fn: func() any {
|
||||
dbConn, err := db.New(context.Background(), cnf.DbURL)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to the Database: %s. Err: %v\n", cnf.DbURL, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return dbConn
|
||||
connect: func() any {
|
||||
return connectFn
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func EventbusPlugin(cnf *Config) Plugin {
|
||||
connectFn := func() *amqp.Channel {
|
||||
log.Println("establishing api-eventbus connection...")
|
||||
|
||||
conn, err := amqp.Dial(cnf.EventbusURL)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to connect to the eventbus: %s. Err: %v\n", cnf.EventbusURL, err.Error())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
chn, err := conn.Channel()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open new eventbus channel. Err: %v\n", err.Error())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return chn
|
||||
}
|
||||
|
||||
// checking if the connection is still alive and try to reconnect when it is not
|
||||
go func(chn *amqp.Channel) {
|
||||
tick := time.NewTicker(5 * time.Second) // is 5 seconds is not too much?
|
||||
defer tick.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
if closed := chn.IsClosed(); closed {
|
||||
log.Println("lost connection with api-eventbus. Reconnecting...")
|
||||
chn = connectFn()
|
||||
}
|
||||
}
|
||||
}
|
||||
}(connectFn())
|
||||
|
||||
return Plugin{
|
||||
name: "eventbus",
|
||||
fn: func() any {
|
||||
conn, err := amqp.Dial(cnf.EventbusURL)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to the Eventbus: %s. Err: %v\n", cnf.EventbusURL, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
chn, err := conn.Channel()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open new Eventbus channel. Err: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return chn
|
||||
connect: func() any {
|
||||
return connectFn
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user