Big Refacotr - part2
This commit is contained in:
@@ -8,10 +8,10 @@ import (
|
||||
)
|
||||
|
||||
type App struct {
|
||||
worker Worker
|
||||
worker WorkerInterface
|
||||
}
|
||||
|
||||
func NewApp(worker Worker) *App {
|
||||
func NewApp(worker WorkerInterface) *App {
|
||||
return &App{
|
||||
worker: worker,
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@ const (
|
||||
defCachePassword = "12345678"
|
||||
defDbURL = "postgres://postgres:12345678@db-postgres:5432/egommerce"
|
||||
defMongoDbURL = "mongodb://mongodb:12345678@db-mongo:27017"
|
||||
// defEventBusExchange = "api-events"
|
||||
// defEventBusQueue = "identity-svc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -49,25 +47,11 @@ func NewConfig(name string) *Config {
|
||||
c.CacheUsername = cnf.GetEnv("API_CACHE_USERNAME", defCacheUsername)
|
||||
c.CachePassword = cnf.GetEnv("API_CACHE_PASSWORD", defCachePassword)
|
||||
c.DbURL = cnf.GetEnv("API_DATABASE_URL", defDbURL)
|
||||
c.MongoDbUrl = cnf.GetEnv("API_MONGO_URL", defMongoDbURL)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// 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["netAddr"] = c.NetAddr
|
||||
// arr["cacheAddr"] = c.CacheAddr
|
||||
// arr["cacheUsername"] = c.CacheUsername
|
||||
// arr["cachePassword"] = c.CachePassword
|
||||
// arr["dbURL"] = c.DbURL
|
||||
|
||||
// return arr
|
||||
// }
|
||||
|
||||
func (c *Config) getAppFullName() string {
|
||||
return fmt.Sprintf("%s_%s", c.Name, c.ID)
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Dependencies struct {
|
||||
cache *redis.Client
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func (d *Dependencies) GetCache() *redis.Client {
|
||||
return d.cache
|
||||
}
|
||||
|
||||
func (d *Dependencies) GetDatabase() *pgxpool.Pool {
|
||||
return d.db
|
||||
}
|
||||
@@ -7,7 +7,7 @@ type (
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
Worker interface {
|
||||
WorkerInterface interface {
|
||||
Start() error
|
||||
OnShutdown()
|
||||
|
||||
|
||||
@@ -7,9 +7,32 @@ import (
|
||||
"time"
|
||||
|
||||
redis "github.com/go-redis/redis/v8"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
db "github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type PluginManager struct {
|
||||
plugins map[string]any
|
||||
}
|
||||
|
||||
func NewPluginManager() *PluginManager {
|
||||
return &PluginManager{
|
||||
plugins: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *PluginManager) addPlugin(name string, fn PluginFn) {
|
||||
pm.plugins[name] = fn()
|
||||
}
|
||||
|
||||
func (pm *PluginManager) getCache() *redis.Client {
|
||||
return (pm.plugins["cache"]).(*redis.Client)
|
||||
}
|
||||
|
||||
func (pm *PluginManager) getDatabase() *pgxpool.Pool {
|
||||
return (pm.plugins["database"]).(*pgxpool.Pool)
|
||||
}
|
||||
|
||||
func CachePlugin(cnf *Config) Plugin {
|
||||
return Plugin{
|
||||
name: "cache",
|
||||
|
||||
@@ -1 +1,44 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/onatm/clockwerk"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/infra/repository"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/cli/scheduler"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/service"
|
||||
)
|
||||
|
||||
type Scheduler struct {
|
||||
*PluginManager
|
||||
}
|
||||
|
||||
func NewScheduler(c *Config) *Scheduler {
|
||||
return &Scheduler{
|
||||
PluginManager: NewPluginManager(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Scheduler) Start() error { // STILL NEEDS REFACTORING ;)
|
||||
userRepo := repository.NewUserRepository(c.getDatabase())
|
||||
roleRepo := repository.NewRoleRepository(c.getDatabase())
|
||||
urlRepo := repository.NewURLAccessRepository(c.getDatabase())
|
||||
authSrv := service.NewAuthService(userRepo, c.getCache())
|
||||
guardSrv := service.NewGuardService(authSrv, c.getCache(), userRepo, roleRepo, urlRepo)
|
||||
|
||||
job := scheduler.NewCachePermissionsJob(guardSrv)
|
||||
sch := clockwerk.New()
|
||||
sch.Every(30 * time.Minute).Do(job)
|
||||
sch.Start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Scheduler) OnShutdown() {
|
||||
log.Println("Scheduler is going down...")
|
||||
|
||||
c.getDatabase().Close()
|
||||
c.getCache().Close()
|
||||
}
|
||||
|
||||
@@ -6,12 +6,10 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
jwt "github.com/gofiber/contrib/jwt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
commonDTO "git.ego.freeddns.org/egommerce/api-entities/common/dto"
|
||||
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
|
||||
@@ -35,32 +33,26 @@ var defaultCORS = cors.New(
|
||||
type (
|
||||
Server struct {
|
||||
*fiber.App
|
||||
*PluginManager
|
||||
|
||||
ID string
|
||||
addr string // e.g. "127.0.0.1:443"
|
||||
plugins map[string]any
|
||||
ID string
|
||||
addr string // e.g. "127.0.0.1:443"
|
||||
}
|
||||
|
||||
// HeaderRequestID struct {
|
||||
// RequestID string `reqHeader:"x-request-id"`
|
||||
// }
|
||||
)
|
||||
|
||||
func NewServer(c *Config) *Server {
|
||||
srv := &Server{
|
||||
return &Server{
|
||||
ID: c.ID,
|
||||
App: fiber.New(fiber.Config{
|
||||
AppName: c.ID,
|
||||
ServerHeader: c.Name + ":" + c.ID,
|
||||
ServerHeader: c.getAppFullName(),
|
||||
ReadTimeout: c.ReadTimeout * time.Millisecond,
|
||||
WriteTimeout: c.WriteTimeout * time.Millisecond,
|
||||
IdleTimeout: c.IdleTimeout * time.Millisecond,
|
||||
}),
|
||||
addr: c.NetAddr,
|
||||
plugins: make(map[string]any),
|
||||
PluginManager: NewPluginManager(),
|
||||
addr: c.NetAddr,
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
func (s *Server) Start() error {
|
||||
@@ -88,28 +80,6 @@ func (s *Server) OnShutdown() {
|
||||
s.Shutdown()
|
||||
}
|
||||
|
||||
func (s *Server) addPlugin(name string, fn PluginFn) {
|
||||
s.plugins[name] = fn()
|
||||
}
|
||||
|
||||
// Plugin helper functions - using hardcoded keys because we rely on a specific implementation here...
|
||||
func (s *Server) getCache() *redis.Client {
|
||||
return (s.plugins["cache"]).(*redis.Client)
|
||||
}
|
||||
|
||||
func (s *Server) getDatabase() *pgxpool.Pool {
|
||||
return (s.plugins["database"]).(*pgxpool.Pool)
|
||||
}
|
||||
|
||||
// func GetRequestID(c *fiber.Ctx) (string, error) {
|
||||
// var hdr = new(HeaderRequestID)
|
||||
// if err := c.ReqHeaderParser(hdr); err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
// return hdr.RequestID, nil
|
||||
// }
|
||||
|
||||
func (s *Server) setupRouter() {
|
||||
s.Options("*", defaultCORS)
|
||||
s.Use(defaultCORS)
|
||||
@@ -159,7 +129,3 @@ func JWTProtected() func(c *fiber.Ctx) error {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// func (s *Server) Error(c *fiber.Ctx, code int, msg string) error {
|
||||
// return c.Status(code).JSON(dto.ErrorResponseDTO{Error: msg})
|
||||
// }
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/app"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/cli/scheduler"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -15,13 +14,11 @@ func main() {
|
||||
log.Panicln(cnf.ErrLoadingEnvs)
|
||||
}
|
||||
|
||||
c := app.NewConfig("identity-cronjob")
|
||||
cArr := c.GetArray()
|
||||
|
||||
doer := scheduler.New(c)
|
||||
a := app.NewApp(doer)
|
||||
a.RegisterPlugin(app.CachePlugin(cArr))
|
||||
a.RegisterPlugin(app.DatabasePlugin(cArr))
|
||||
cnf := app.NewConfig("identity-scheduler")
|
||||
w := app.NewScheduler(cnf)
|
||||
a := app.NewApp(w)
|
||||
a.RegisterPlugin(app.CachePlugin(cnf))
|
||||
a.RegisterPlugin(app.DatabasePlugin(cnf))
|
||||
|
||||
while := make(chan struct{})
|
||||
err := a.Start(while)
|
||||
|
||||
@@ -15,14 +15,13 @@ func main() {
|
||||
}
|
||||
|
||||
cnf := app.NewConfig("identity-svc")
|
||||
worker := app.NewServer(cnf)
|
||||
|
||||
srv := app.NewApp(worker)
|
||||
srv.RegisterPlugin(app.CachePlugin(cnf))
|
||||
srv.RegisterPlugin(app.DatabasePlugin(cnf))
|
||||
w := app.NewServer(cnf)
|
||||
a := app.NewApp(w)
|
||||
a.RegisterPlugin(app.CachePlugin(cnf))
|
||||
a.RegisterPlugin(app.DatabasePlugin(cnf))
|
||||
|
||||
while := make(chan struct{})
|
||||
err := srv.Start(while)
|
||||
err := a.Start(while)
|
||||
<-while
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -6,7 +6,7 @@ toolchain go1.24.1
|
||||
|
||||
require (
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.34
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.5.2
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.5.3
|
||||
github.com/georgysavva/scany/v2 v2.1.4
|
||||
github.com/go-pg/migrations/v8 v8.1.0
|
||||
github.com/go-pg/pg/v10 v10.15.0
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.34 h1:WftM9cvV3JmbS1DlHCIiV3tsYIpALj9IXo90mkgZNWQ=
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.34/go.mod h1:IqynARw+06GOm4eZGZuepmbi7bUxWBnOB4jd5cI7jf8=
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.5.2 h1:szfCwZ8S1Yf3b6LwpBs0DZYQZMsVl4Fe6VU1ou4LTOE=
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.5.2/go.mod h1:T3ia8iprzlTRznPVXYCgEzQb/1UvIcdn9FHabE58vy0=
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.5.3 h1:so+OWWVJEg6JZ5XOSmCpfW7Pd7IL6ETH0QsC6zCwndo=
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.5.3/go.mod h1:T3ia8iprzlTRznPVXYCgEzQb/1UvIcdn9FHabE58vy0=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/MicahParks/keyfunc/v2 v2.1.0 h1:6ZXKb9Rp6qp1bDbJefnG7cTH8yMN1IC/4nf+GVjO99k=
|
||||
github.com/MicahParks/keyfunc/v2 v2.1.0/go.mod h1:rW42fi+xgLJ2FRRXAfNx9ZA8WpD4OeE/yHVMteCkw9k=
|
||||
|
||||
@@ -4,25 +4,19 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/infra/repository"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/service"
|
||||
)
|
||||
|
||||
type CachePermissionsJob struct {
|
||||
sch *Scheduler
|
||||
guard *service.GuardService
|
||||
}
|
||||
|
||||
func NewCachePermissionsJob(sch *Scheduler) CachePermissionsJob {
|
||||
return CachePermissionsJob{sch: sch}
|
||||
func NewCachePermissionsJob(guard *service.GuardService) *CachePermissionsJob {
|
||||
return &CachePermissionsJob{guard: guard}
|
||||
}
|
||||
|
||||
func (j CachePermissionsJob) Run() {
|
||||
userRepo := repository.NewUserRepository(j.sch.GetDatabase())
|
||||
roleRepo := repository.NewRoleRepository(j.sch.GetDatabase())
|
||||
urlRepo := repository.NewURLAccessRepository(j.sch.GetDatabase())
|
||||
authSrv := service.NewAuthService(userRepo, j.sch.GetCache())
|
||||
guardSrv := service.NewGuardService(authSrv, j.sch.GetCache(), userRepo, roleRepo, urlRepo)
|
||||
j.guard.CacheAllPermissions()
|
||||
|
||||
guardSrv.CacheAllPermissions()
|
||||
fmt.Println(time.Now().String() + " permission successfully cached")
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package scheduler
|
||||
|
||||
import ( // REFACTOR IT LIKE A SERVER WAS
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ego.freeddns.org/egommerce/identity-service/app"
|
||||
|
||||
// "github.com/go-redis/redis/v8"
|
||||
// "github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/onatm/clockwerk"
|
||||
)
|
||||
|
||||
type Scheduler struct {
|
||||
handlers map[string]any
|
||||
}
|
||||
|
||||
func New(c *app.Config) *Scheduler {
|
||||
return &Scheduler{
|
||||
handlers: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Scheduler) Start() error {
|
||||
job := NewCachePermissionsJob(c)
|
||||
sch := clockwerk.New()
|
||||
sch.Every(30 * time.Minute).Do(job)
|
||||
sch.Start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// func (c *Scheduler) RegisterHandler(name string, fn func() any) {
|
||||
// c.handlers[name] = fn()
|
||||
// }
|
||||
|
||||
func (c *Scheduler) OnShutdown() {
|
||||
log.Println("Chronos is going down...")
|
||||
|
||||
// c.GetDatabase().Close()
|
||||
// c.GetCache().Close()
|
||||
}
|
||||
|
||||
// Plugin helper funcitons - refactor needed cause funcs are duplcated in server.go
|
||||
// TODO: move functions below to some common place
|
||||
// func (c *Scheduler) GetCache() *redis.Client {
|
||||
// return (c.handlers["cache"]).(*redis.Client)
|
||||
// }
|
||||
|
||||
// func (c *Scheduler) GetDatabase() *pgxpool.Pool {
|
||||
// return (c.handlers["database"]).(*pgxpool.Pool)
|
||||
// }
|
||||
Reference in New Issue
Block a user