Added Chronos to schedule tasks, restructured config as common pkg, updated server pkg
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -2,11 +2,13 @@ deploy/.env
|
||||
!deploy/.env.dist
|
||||
deploy/.env.*
|
||||
|
||||
deploy/app.run
|
||||
|
||||
deploy/server
|
||||
deploy/chronos
|
||||
|
||||
!deploy/certs/.gitkeep
|
||||
deploy/certs/*
|
||||
|
||||
deploy/app.run
|
||||
|
||||
.vscode/
|
||||
__debug_bin
|
||||
@@ -4,11 +4,13 @@ FROM golang:alpine
|
||||
ARG BIN_OUTPUT=/go/bin
|
||||
ARG GO_SERVER=cmd/server/main.go
|
||||
ARG GO_MIGRATE=cmd/migrate/main.go
|
||||
ARG GO_CHRONOS=cmd/chronos/main.go
|
||||
|
||||
WORKDIR /go/src/app
|
||||
COPY src ./
|
||||
COPY src/ ./
|
||||
|
||||
RUN export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \
|
||||
go build -ldflags="-w -s" -o "$BIN_OUTPUT/server" $GO_SERVER && \
|
||||
go build -ldflags="-w -s" -o "$BIN_OUTPUT/migrate" $GO_MIGRATE
|
||||
go build -ldflags="-w -s" -o "$BIN_OUTPUT/migrate" $GO_MIGRATE && \
|
||||
go build -ldflags="-w -s" -o "$BIN_OUTPUT/chronos" $GO_CHRONOS
|
||||
|
||||
@@ -8,7 +8,6 @@ FROM alpine:3.22
|
||||
|
||||
ARG SVC_NAME
|
||||
ARG SVC_VER
|
||||
ARG BIN_OUTPUT
|
||||
ARG BUILD_TIME
|
||||
|
||||
LABEL dev.egommerce.image.author="Piotr Biernat"
|
||||
@@ -18,16 +17,15 @@ LABEL dev.egommerce.image.version=${SVC_VER}
|
||||
LABEL dev.egommerce.image.build_time=${BUILD_TIME}
|
||||
|
||||
WORKDIR /
|
||||
COPY --from=builder $BIN_OUTPUT /app
|
||||
COPY --from=builder /go/bin/migrate /bin/migrate
|
||||
COPY --from=builder /go/bin/server /usr/local/bin/server
|
||||
COPY --from=builder /go/bin/migrate /usr/local/bin/migrate
|
||||
COPY --from=builder /go/bin/chronos /usr/local/bin/chronos
|
||||
COPY deploy/.env.docker /.env
|
||||
COPY ./bin /bin
|
||||
COPY ./bin/* /usr/local/bin/
|
||||
|
||||
RUN chmod 755 /bin/entrypoint.sh /bin/migrate.sh
|
||||
|
||||
# RUN apk add curl
|
||||
RUN chmod 755 /usr/local/bin/entrypoint.sh /usr/local/bin/migrate.sh
|
||||
|
||||
EXPOSE 443
|
||||
|
||||
ENTRYPOINT ["entrypoint.sh"]
|
||||
CMD ["sh", "-c", "/app"]
|
||||
CMD ["sh", "-c", "/usr/local/bin/server"]
|
||||
|
||||
10
Makefile
10
Makefile
@@ -13,8 +13,14 @@ push-image-dev:
|
||||
push-image-prod:
|
||||
- sh ${DEPLOY_DIR}/image-push.sh
|
||||
|
||||
build-local:
|
||||
build-local-server:
|
||||
- go build -C ${SRC_DIR} -o ../deploy/server cmd/server/main.go
|
||||
|
||||
run-local:
|
||||
run-local-server:
|
||||
- cd deploy/ && ./server
|
||||
|
||||
build-local-chronos:
|
||||
- go build -C ${SRC_DIR} -o ../deploy/chronos cmd/chronos/main.go
|
||||
|
||||
run-local-chronos:
|
||||
- cd deploy/ && ./chronos
|
||||
|
||||
@@ -19,7 +19,6 @@ then
|
||||
docker build \
|
||||
--build-arg SVC_NAME=identity-service \
|
||||
--build-arg SVC_VER="1.0" \
|
||||
--build-arg BIN_OUTPUT=/go/bin/server \
|
||||
--build-arg BUILDER_IMAGE=$BUILDER_IMAGE \
|
||||
--build-arg BUILD_TIME \
|
||||
--rm --cache-from $SERVER_IMAGE:$TARGET \
|
||||
@@ -30,7 +29,6 @@ else
|
||||
docker build \
|
||||
--build-arg SVC_NAME=identity-service \
|
||||
--build-arg SVC_VER="dev" \
|
||||
--build-arg BIN_OUTPUT=/go/bin/server \
|
||||
--build-arg BUILDER_IMAGE=$BUILDER_IMAGE \
|
||||
--build-arg BUILD_TIME \
|
||||
--rm --no-cache -t $SERVER_IMAGE:$TARGET \
|
||||
|
||||
36
src/cmd/chronos/main.go
Normal file
36
src/cmd/chronos/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/common"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/chronos"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if cnf.ErrLoadingEnvs != nil {
|
||||
log.Panicln(cnf.ErrLoadingEnvs)
|
||||
}
|
||||
|
||||
c := common.NewConfig("identity-cronjob")
|
||||
cArr := c.GetArray()
|
||||
|
||||
doer := chronos.New(c)
|
||||
a := common.NewApp(doer)
|
||||
a.RegisterPlugin(common.CachePlugin(cArr))
|
||||
a.RegisterPlugin(common.DatabasePlugin(cArr))
|
||||
|
||||
while := make(chan struct{})
|
||||
err := a.Start(while)
|
||||
<-while
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to chronos. Reason: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/app"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/common"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/server"
|
||||
)
|
||||
|
||||
@@ -16,13 +15,13 @@ func main() {
|
||||
log.Panicln(cnf.ErrLoadingEnvs)
|
||||
}
|
||||
|
||||
c := server.NewConfig("identity-svc")
|
||||
c := common.NewConfig("identity-svc")
|
||||
cArr := c.GetArray()
|
||||
|
||||
doer := server.New(c)
|
||||
a := app.NewApp(doer)
|
||||
a.RegisterPlugin(app.CachePlugin(cArr))
|
||||
a.RegisterPlugin(app.DatabasePlugin(cArr))
|
||||
a := common.NewApp(doer)
|
||||
a.RegisterPlugin(common.CachePlugin(cArr))
|
||||
a.RegisterPlugin(common.DatabasePlugin(cArr))
|
||||
|
||||
while := make(chan struct{})
|
||||
err := a.Start(while)
|
||||
@@ -33,6 +32,5 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("Gone")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package app
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -44,9 +43,6 @@ func (a *App) Start(while chan struct{}) error {
|
||||
close(while)
|
||||
}()
|
||||
|
||||
runFile := a.createRunFile("./app.run") // FIXME path...
|
||||
defer a.removeRunFile(runFile)
|
||||
|
||||
err := a.doer.Start()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to start app. Reason: %v\n", err)
|
||||
@@ -66,18 +62,3 @@ func (a *App) RegisterPlugin(p Plugin) error {
|
||||
func (a *App) Shutdown() {
|
||||
a.doer.OnShutdown()
|
||||
}
|
||||
|
||||
func (a *App) createRunFile(path string) *os.File {
|
||||
run, err := os.Create(path)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create run file. Reason: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
run.WriteString(strconv.Itoa(os.Getpid()))
|
||||
|
||||
return run
|
||||
}
|
||||
|
||||
func (a *App) removeRunFile(f *os.File) error {
|
||||
return f.Close()
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package server
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -64,20 +63,12 @@ func (c *Config) GetAppFullName() string {
|
||||
return fmt.Sprintf("%s_%s", c.Name, c.ID)
|
||||
}
|
||||
|
||||
func (c *Config) GetIP() string {
|
||||
host, _ := os.Hostname()
|
||||
ips, _ := net.LookupIP(host)
|
||||
|
||||
return ips[0].String()
|
||||
}
|
||||
|
||||
func (c *Config) GetArray() map[string]string { // FIXME fix types etc
|
||||
arr := make(map[string]string)
|
||||
arr["id"] = c.ID
|
||||
arr["name"] = c.Name
|
||||
arr["appFullname"] = c.GetAppFullName()
|
||||
arr["domain"] = c.Domain
|
||||
arr["ip"] = c.GetIP()
|
||||
arr["netAddr"] = c.NetAddr
|
||||
arr["pathPrefix"] = c.PathPrefix
|
||||
arr["cacheAddr"] = c.CacheAddr
|
||||
@@ -1,4 +1,4 @@
|
||||
package app
|
||||
package common
|
||||
|
||||
import (
|
||||
"time"
|
||||
@@ -18,7 +18,7 @@ type (
|
||||
func CachePlugin(cArr map[string]string) Plugin {
|
||||
return Plugin{
|
||||
name: "cache",
|
||||
fn: func() any { // FIXME: return type
|
||||
fn: func() any {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: cArr["cacheAddr"],
|
||||
Username: cArr["cacheUsername"],
|
||||
@@ -33,7 +33,7 @@ func CachePlugin(cArr map[string]string) Plugin {
|
||||
func DatabasePlugin(cArr map[string]string) Plugin {
|
||||
return Plugin{
|
||||
name: "database",
|
||||
fn: func() any { // FIXME: return type
|
||||
fn: func() any {
|
||||
dbConn, _ := db.Connect(cArr["dbURL"])
|
||||
// if err != nil {
|
||||
// log.Fatalf("Failed to connect to the Database: %s. Err: %v\n", cArr["dbURL"], err)
|
||||
@@ -15,6 +15,7 @@ require (
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.7.6
|
||||
github.com/onatm/clockwerk v1.1.0
|
||||
golang.org/x/crypto v0.43.0
|
||||
)
|
||||
|
||||
@@ -38,7 +39,6 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.19 // indirect
|
||||
github.com/rogpeppe/go-internal v1.11.0 // indirect
|
||||
github.com/stretchr/testify v1.8.4 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasthttp v1.67.0 // indirect
|
||||
|
||||
@@ -98,6 +98,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||
github.com/onatm/clockwerk v1.1.0 h1:Ig2tTdZGtYWM1n5sDcf/LZ9zKe5l569G8jIPejHS0DM=
|
||||
github.com/onatm/clockwerk v1.1.0/go.mod h1:9R0U2KkwobOCqRvPtIAt0qHgaCWbo3kjvySmmMth2Ao=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
||||
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
|
||||
@@ -121,8 +123,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
|
||||
52
src/internal/chronos/chronos.go
Normal file
52
src/internal/chronos/chronos.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package chronos
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/common"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/chronos/jobs"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/onatm/clockwerk"
|
||||
)
|
||||
|
||||
type Chronos struct {
|
||||
handlers map[string]any
|
||||
}
|
||||
|
||||
func New(c *common.Config) *Chronos {
|
||||
return &Chronos{
|
||||
handlers: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Chronos) Start() error {
|
||||
job := jobs.NewCachePermissionsJob()
|
||||
s := clockwerk.New()
|
||||
s.Every(30 * time.Second).Do(job)
|
||||
s.Start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Chronos) RegisterHandler(name string, fn func() any) {
|
||||
c.handlers[name] = fn()
|
||||
}
|
||||
|
||||
func (s *Chronos) OnShutdown() {
|
||||
log.Println("Chronos is going down...")
|
||||
|
||||
// s.GetDatabase().Close()
|
||||
}
|
||||
|
||||
// Plugin helper funcitons
|
||||
// TODO: move functions below to some common place
|
||||
func (s *Chronos) GetCache() *redis.Client {
|
||||
return (s.handlers["cache"]).(*redis.Client)
|
||||
}
|
||||
|
||||
func (s *Chronos) GetDatabase() *pgxpool.Pool {
|
||||
return (s.handlers["database"]).(*pgxpool.Pool)
|
||||
}
|
||||
16
src/internal/chronos/jobs/cache_permissions_job.go
Normal file
16
src/internal/chronos/jobs/cache_permissions_job.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CachePermissionsJob struct{}
|
||||
|
||||
func NewCachePermissionsJob() CachePermissionsJob {
|
||||
return CachePermissionsJob{}
|
||||
}
|
||||
|
||||
func (j CachePermissionsJob) Run() {
|
||||
fmt.Println(time.Now().String() + " Every 30 seconds")
|
||||
}
|
||||
@@ -2,16 +2,16 @@ package server
|
||||
|
||||
import (
|
||||
dto "git.ego.freeddns.org/egommerce/api-entities/identity/dto"
|
||||
domain "git.ego.freeddns.org/egommerce/identity-service/domain/repository"
|
||||
repo "git.ego.freeddns.org/egommerce/identity-service/domain/repository"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/service"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/ui"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func (s *Server) AccessHandlerFn(c *fiber.Ctx) error {
|
||||
userRepo := domain.NewUserRepository(s.GetDatabase())
|
||||
roleRepo := domain.NewRoleRepository(s.GetDatabase())
|
||||
urlRepo := domain.NewURLAccessRepository(s.GetDatabase())
|
||||
userRepo := repo.NewUserRepository(s.GetDatabase())
|
||||
roleRepo := repo.NewRoleRepository(s.GetDatabase())
|
||||
urlRepo := repo.NewURLAccessRepository(s.GetDatabase())
|
||||
authSrv := service.NewAuthService(userRepo, s.GetCache())
|
||||
guardSrv := service.NewGuardService(authSrv, s.GetCache(), userRepo, roleRepo, urlRepo)
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
dto "git.ego.freeddns.org/egommerce/api-entities/common/dto"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/common"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -26,7 +28,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func New(c *Config) *Server {
|
||||
func New(c *common.Config) *Server {
|
||||
return &Server{
|
||||
ID: c.ID,
|
||||
App: fiber.New(fiber.Config{
|
||||
@@ -58,17 +60,13 @@ func (s *Server) Start() error {
|
||||
}
|
||||
|
||||
func (s *Server) RegisterHandler(name string, fn func() any) {
|
||||
// fmt.Printf("Registering plugin(with handler): %s... OK\n", name)
|
||||
s.handlers[name] = fn()
|
||||
}
|
||||
|
||||
func (s *Server) OnShutdown() {
|
||||
log.Printf("Server %s is going down...", s.ID)
|
||||
|
||||
// s.GetEventBus().Close()
|
||||
s.GetDatabase().Close()
|
||||
log.Printf("Gone.")
|
||||
|
||||
s.Shutdown()
|
||||
}
|
||||
|
||||
@@ -93,7 +91,3 @@ func (s *Server) GetCache() *redis.Client {
|
||||
func (s *Server) GetDatabase() *pgxpool.Pool {
|
||||
return (s.handlers["database"]).(*pgxpool.Pool)
|
||||
}
|
||||
|
||||
// func (s *Server) GetEventBus() *amqp.Channel {
|
||||
// return (s.handlers["eventbus"]).(*amqp.Channel)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user