Migrate to K8S stack and fixes before big refactoring
This commit is contained in:
@@ -6,13 +6,14 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
cnf "git.pbiernat.io/egommerce/go-api-pkg/config"
|
||||
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
|
||||
)
|
||||
|
||||
const (
|
||||
defName = "identity-svc"
|
||||
defDomain = "identity-svc"
|
||||
defCacheAddr = "egommerce.local:6379"
|
||||
defCacheAddr = "api-cache:6379"
|
||||
defCacheUsername = "default"
|
||||
defCachePassword = "12345678"
|
||||
defDbURL = "postgres://postgres:12345678@db-postgres:5432/egommerce"
|
||||
defEventBusURL = "amqp://guest:guest@api-eventbus:5672"
|
||||
@@ -21,18 +22,16 @@ const (
|
||||
defNetAddr = ":443"
|
||||
defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017"
|
||||
defPathPrefix = "/identity"
|
||||
defRegistryAddr = "api-registry:8501"
|
||||
defEbEventsExchange = "api-events"
|
||||
defEbEventsQueue = "identity-svc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ID string
|
||||
Name string
|
||||
Domain string
|
||||
NetAddr string
|
||||
RegistryDomainOverIP string
|
||||
PathPrefix string
|
||||
ID string
|
||||
Name string
|
||||
Domain string
|
||||
NetAddr string
|
||||
PathPrefix string
|
||||
|
||||
IdleTimeout time.Duration // miliseconds
|
||||
ReadTimeout time.Duration // miliseconds
|
||||
@@ -41,15 +40,12 @@ type Config struct {
|
||||
LoggerAddr string `json:"logger_addr"`
|
||||
DbURL string `json:"db_url"`
|
||||
CacheAddr string `json:"cache_addr"`
|
||||
CacheUsername string `json:"cache_username"`
|
||||
CachePassword string `json:"cache_password"`
|
||||
MongoDbUrl string `json:"mongodb_url"`
|
||||
EventBusURL string `json:"eventbus_url"`
|
||||
EventBusExchange string `json:"eventbus_exchange"`
|
||||
EventBusQueue string `json:"eventbus_queue"`
|
||||
KVNamespace string
|
||||
RegistryAddr string
|
||||
|
||||
// Fields with JSON mappings are available through Consul KV storage
|
||||
}
|
||||
|
||||
func NewConfig(name string) *Config {
|
||||
@@ -59,17 +55,14 @@ func NewConfig(name string) *Config {
|
||||
c.Name = name
|
||||
c.Domain = cnf.GetEnv("APP_DOMAIN", defDomain)
|
||||
c.NetAddr = cnf.GetEnv("SERVER_ADDR", defNetAddr)
|
||||
c.RegistryDomainOverIP = cnf.GetEnv("REGISTRY_USE_DOMAIN_OVER_IP", "false")
|
||||
c.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix)
|
||||
|
||||
c.CacheAddr = cnf.GetEnv("CACHE_ADDR", defCacheAddr)
|
||||
c.CachePassword = cnf.GetEnv("CACHE_PASSWORD", defCachePassword)
|
||||
c.DbURL = cnf.GetEnv("DATABASE_URL", defDbURL)
|
||||
c.CacheAddr = cnf.GetEnv("API_CACHE_ADDR", defCacheAddr)
|
||||
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.EventBusExchange = defEbEventsExchange
|
||||
c.EventBusURL = cnf.GetEnv("EVENTBUS_URL", defEventBusURL)
|
||||
c.KVNamespace = cnf.GetEnv("APP_KV_NAMESPACE", defKVNmspc)
|
||||
c.LoggerAddr = cnf.GetEnv("LOGGER_ADDR", defLoggerAddr)
|
||||
c.RegistryAddr = cnf.GetEnv("REGISTRY_ADDR", defRegistryAddr)
|
||||
c.EventBusURL = cnf.GetEnv("API_EVENTBUS_URL", defEventBusURL)
|
||||
c.LoggerAddr = cnf.GetEnv("API_LOGGER_ADDR", defLoggerAddr)
|
||||
c.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix)
|
||||
|
||||
return c
|
||||
}
|
||||
@@ -96,16 +89,14 @@ func (c *Config) GetArray() map[string]string { // FIXME fix types etc
|
||||
arr["domain"] = c.Domain
|
||||
arr["ip"] = c.GetIP()
|
||||
arr["netAddr"] = c.NetAddr
|
||||
arr["registryDomainOverIP"] = c.RegistryDomainOverIP
|
||||
arr["pathPrefix"] = c.PathPrefix
|
||||
arr["cacheAddr"] = c.CacheAddr
|
||||
arr["cacheUsername"] = c.CacheUsername
|
||||
arr["cachePassword"] = c.CachePassword
|
||||
arr["dbURL"] = c.DbURL
|
||||
arr["eventBusExchange"] = c.EventBusExchange
|
||||
arr["eventBusURL"] = c.EventBusURL
|
||||
arr["kvNamespace"] = c.KVNamespace
|
||||
arr["loggerAddr"] = c.LoggerAddr
|
||||
arr["registryAddr"] = c.RegistryAddr
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func (s *Server) ConfigHandler(c *fiber.Ctx) error {
|
||||
return c.JSON(s.Config)
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
@@ -9,6 +12,25 @@ type HealthResponse struct {
|
||||
}
|
||||
|
||||
func (s *Server) HealthHandler(c *fiber.Ctx) error {
|
||||
// Only 404 indicate service as non-healthy
|
||||
err := s.GetDatabase().Ping(context.Background())
|
||||
if err != nil {
|
||||
// fmt.Println("db unavailable", err)
|
||||
return c.SendStatus(http.StatusNotFound)
|
||||
}
|
||||
|
||||
err = s.GetLogger().Ping()
|
||||
if err != nil {
|
||||
// fmt.Println("logger unavailable", err)
|
||||
return c.SendStatus(http.StatusNotFound)
|
||||
}
|
||||
|
||||
err = s.GetCache().Ping(context.Background()).Err()
|
||||
if err != nil {
|
||||
// fmt.Println("cache unavailable", err)
|
||||
return c.SendStatus(http.StatusNotFound)
|
||||
}
|
||||
|
||||
return c.JSON(&HealthResponse{
|
||||
Status: "OK",
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.pbiernat.io/egommerce/identity-service/internal/service"
|
||||
"git.ego.freeddns.org/egommerce/identity-service/internal/service"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package server
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"git.pbiernat.io/egommerce/go-api-pkg/fluentd"
|
||||
"git.ego.freeddns.org/egommerce/go-api-pkg/fluentd"
|
||||
)
|
||||
|
||||
// "github.com/gofiber/fiber/v2"
|
||||
|
||||
@@ -18,10 +18,8 @@ func SetupRouter(s *Server) {
|
||||
s.Use(defaultCORS)
|
||||
|
||||
s.Get("/health", s.HealthHandler)
|
||||
s.Get("/config", s.ConfigHandler)
|
||||
|
||||
api := s.Group("/api")
|
||||
v1 := api.Group("/v1")
|
||||
v1.Post("/login", s.LoginHandler)
|
||||
v1.All("/traefik", s.TraefikHandler)
|
||||
}
|
||||
|
||||
@@ -10,9 +10,8 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.pbiernat.io/egommerce/api-entities/http"
|
||||
"git.pbiernat.io/egommerce/go-api-pkg/consul"
|
||||
"git.pbiernat.io/egommerce/go-api-pkg/fluentd"
|
||||
"git.ego.freeddns.org/egommerce/api-entities/common/dto"
|
||||
"git.ego.freeddns.org/egommerce/go-api-pkg/fluentd"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -48,11 +47,11 @@ func (s *Server) Start() error {
|
||||
SetupRouter(s)
|
||||
|
||||
// fmt.Printf("Starting server at: %s...\n", s.addr)
|
||||
cer, err := tls.LoadX509KeyPair("certs/client.crt", "certs/client.key")
|
||||
crt, err := tls.LoadX509KeyPair("certs/identity-svc.crt", "certs/identity-svc.key")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tlsCnf := &tls.Config{Certificates: []tls.Certificate{cer}}
|
||||
tlsCnf := &tls.Config{Certificates: []tls.Certificate{crt}}
|
||||
|
||||
ln, _ := net.Listen("tcp", s.addr)
|
||||
ln = tls.NewListener(ln, tlsCnf)
|
||||
@@ -61,15 +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)
|
||||
// fmt.Printf("Registering plugin(with handler): %s... OK\n", name)
|
||||
s.handlers[name] = fn()
|
||||
}
|
||||
|
||||
func (s *Server) OnShutdown() {
|
||||
// s.GetLogger().Log("Server %s is going down...", s.ID)
|
||||
s.GetLogger().Log("Server %s is going down...", s.ID)
|
||||
|
||||
// s.GetRegistry().Unregister()
|
||||
// a.clearMetadataCache()
|
||||
// s.GetEventBus().Close()
|
||||
s.GetDatabase().Close()
|
||||
s.GetLogger().Log("Gone.")
|
||||
@@ -88,7 +85,7 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) {
|
||||
}
|
||||
|
||||
func (s *Server) Error(c *fiber.Ctx, code int, msg string) error {
|
||||
return c.Status(code).JSON(http.ErrorResponse{Error: msg})
|
||||
return c.Status(code).JSON(dto.ErrorResponseDTO{Error: msg})
|
||||
}
|
||||
|
||||
// Plugin helper funcitons
|
||||
@@ -107,37 +104,3 @@ func (s *Server) GetDatabase() *pgxpool.Pool { // FIXME hardcoded index issue
|
||||
func (s *Server) GetLogger() *fluentd.Logger {
|
||||
return (s.handlers["logger"]).(*fluentd.Logger)
|
||||
}
|
||||
|
||||
func (s *Server) GetRegistry() *consul.Service {
|
||||
return (s.handlers["registry"]).(*consul.Service)
|
||||
}
|
||||
|
||||
// @CHECK: merge s.Config and s.Base.Config to display all config as one array/map
|
||||
// func (s *Server) registerKVUpdater() { // @FIXME: merge duplication in server.go and worker.go
|
||||
// go func() {
|
||||
// ticker := time.NewTicker(time.Second * 10)
|
||||
// for range ticker.C {
|
||||
// config, _, err := s.Registry.KV().Get(s.cnf.KVNamespace, nil)
|
||||
// if err != nil || config == nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// kvCnf := bytes.NewBuffer(config.Value)
|
||||
// decoder := json.NewDecoder(kvCnf)
|
||||
// if err := decoder.Decode(&s.cnf); err != nil {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// }()
|
||||
// }
|
||||
|
||||
// // func (s *Server) clearMetadataCache() {
|
||||
// // ctx := context.Background()
|
||||
// // key, address := s.getMetadataIPsKey(), s.cnf.Base.AppID
|
||||
|
||||
// // s.Cache.LRem(ctx, key, 0, address)
|
||||
// // }
|
||||
|
||||
// // func (s *Server) getMetadataIPsKey() string {
|
||||
// // return "internal__" + s.cnf.Name + "__ips"
|
||||
// // }
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.pbiernat.io/egommerce/identity-service/internal/service"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type TraefikAuthResponse struct {
|
||||
Status string `json:"status,omitempty"`
|
||||
Message string `json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) TraefikHandler(c *fiber.Ctx) error {
|
||||
cookie := service.AuthService.Cookie("traefik", "dummy-traefik-token")
|
||||
|
||||
c.Cookie(cookie)
|
||||
s.GetLogger().Log("Traefik action set cookie. done.")
|
||||
|
||||
c.Response().Header.Add("Server", "identity-service/traefik")
|
||||
reqCookie := c.Request().Header.Cookie("basket_id")
|
||||
s.GetLogger().Log("Request cookie: %s", reqCookie)
|
||||
|
||||
return c.
|
||||
Status(http.StatusOK).
|
||||
JSON(&TraefikAuthResponse{Status: "OK"})
|
||||
// return c.
|
||||
// Status(http.StatusUnauthorized).
|
||||
// JSON(&TraefikAuthResponse{Message: "Access denied mf..."})
|
||||
}
|
||||
Reference in New Issue
Block a user