mirror of
https://github.com/netbirdio/management-refactor.git
synced 2026-05-22 17:12:59 -07:00
export base server
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package server
|
||||
|
||||
// @note this file includes all the lower level dependencies, db, http and grpc server, metrics, logger, etc.
|
||||
// @note this file includes all the lower level dependencies, db, http and grpc BaseServer, metrics, logger, etc.
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/netbirdio/management-refactor/internals/shared/metrics"
|
||||
)
|
||||
|
||||
func (s *server) Store() *db.Store {
|
||||
func (s *BaseServer) Store() *db.Store {
|
||||
return Create(s, func() *db.Store {
|
||||
ctx := context.Background()
|
||||
dbConn, err := db.NewDatabaseConn(ctx)
|
||||
@@ -27,7 +27,7 @@ func (s *server) Store() *db.Store {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) HttpServer() *http.Server {
|
||||
func (s *BaseServer) HttpServer() *http.Server {
|
||||
return Create(s, func() *http.Server {
|
||||
router := s.Router()
|
||||
|
||||
@@ -38,7 +38,7 @@ func (s *server) HttpServer() *http.Server {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) Metrics() *metrics.AppMetrics {
|
||||
func (s *BaseServer) Metrics() *metrics.AppMetrics {
|
||||
return Create(s, func() *metrics.AppMetrics {
|
||||
appMetrics, err := metrics.NewAppMetrics()
|
||||
if err != nil {
|
||||
@@ -48,13 +48,13 @@ func (s *server) Metrics() *metrics.AppMetrics {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) Router() *mux.Router {
|
||||
func (s *BaseServer) Router() *mux.Router {
|
||||
return Create(s, func() *mux.Router {
|
||||
return rest.NewRouter()
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) EventStore() activity.Store {
|
||||
func (s *BaseServer) EventStore() activity.Store {
|
||||
return Create(s, func() activity.Store {
|
||||
ctx := context.Background()
|
||||
store, err := sqlite.NewSQLiteStore(ctx, "dataDir", "encryptionKey")
|
||||
|
||||
@@ -86,7 +86,7 @@ type Relay struct {
|
||||
Secret string
|
||||
}
|
||||
|
||||
// HttpServerConfig is a config of the HTTP Management service server
|
||||
// HttpServerConfig is a config of the HTTP Management service BaseServer
|
||||
type HttpServerConfig struct {
|
||||
LetsEncryptDomain string
|
||||
// CertFile is the location of the certificate
|
||||
@@ -173,13 +173,13 @@ type ReverseProxy struct {
|
||||
TrustedHTTPProxies []netip.Prefix
|
||||
|
||||
// TrustedHTTPProxiesCount specifies the count of trusted HTTP proxies between the internet
|
||||
// and the server. When using the trusted proxy count method to extract the real IP address,
|
||||
// and the BaseServer. When using the trusted proxy count method to extract the real IP address,
|
||||
// the middleware will search the X-Forwarded-For IP list from the rightmost by this count
|
||||
// minus one.
|
||||
TrustedHTTPProxiesCount uint
|
||||
|
||||
// TrustedPeers represents a list of trusted peers by their IP prefixes.
|
||||
// These peers are considered trustworthy by the gRPC server operator,
|
||||
// These peers are considered trustworthy by the gRPC BaseServer operator,
|
||||
// and the middleware will attempt to extract the real IP address from
|
||||
// request headers if the peer's address falls within one of these
|
||||
// trusted IP prefixes.
|
||||
|
||||
@@ -2,7 +2,7 @@ package server
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Create a dependency and add it to the server's container. A string key identifier will be based on its type definition.
|
||||
// Create a dependency and add it to the BaseServer's container. A string key identifier will be based on its type definition.
|
||||
func Create[T any](s Server, createFunc func() T) T {
|
||||
result, _ := maybeCreate(s, createFunc)
|
||||
|
||||
@@ -17,7 +17,7 @@ func CreateNamed[T any](s Server, name string, createFunc func() T) T {
|
||||
return result
|
||||
}
|
||||
|
||||
// Inject lets you override a specific service from outside the server itself.
|
||||
// Inject lets you override a specific service from outside the BaseServer itself.
|
||||
// This is useful for tests
|
||||
func Inject[T any](c Server, thing T) {
|
||||
_, _ = maybeCreate(c, func() T {
|
||||
|
||||
@@ -2,7 +2,7 @@ package server
|
||||
|
||||
import "github.com/netbirdio/management-refactor/internals/controllers/network_map"
|
||||
|
||||
func (s *server) NetworkMapController() *network_map.Controller {
|
||||
func (s *BaseServer) NetworkMapController() *network_map.Controller {
|
||||
return Create(s, func() *network_map.Controller {
|
||||
store := s.Store()
|
||||
metrics := s.Metrics()
|
||||
|
||||
@@ -9,25 +9,25 @@ import (
|
||||
"github.com/netbirdio/management-refactor/internals/shared/permissions"
|
||||
)
|
||||
|
||||
func (s *server) NetworksManager() networks.Manager {
|
||||
func (s *BaseServer) NetworksManager() networks.Manager {
|
||||
return Create(s, func() networks.Manager {
|
||||
return manager.NewManager(s.Store(), s.Router(), s.PermissionsManager())
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) ResourcesManager() resources.Manager {
|
||||
func (s *BaseServer) ResourcesManager() resources.Manager {
|
||||
return Create(s, func() resources.Manager {
|
||||
return resourcesManager.NewManager(s.Store(), s.Router(), s.NetworksManager())
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) PermissionsManager() permissions.Manager {
|
||||
func (s *BaseServer) PermissionsManager() permissions.Manager {
|
||||
return Create(s, func() permissions.Manager {
|
||||
return permissions.NewManager()
|
||||
})
|
||||
}
|
||||
|
||||
func (s *server) PeersManager() *peers.Manager {
|
||||
func (s *BaseServer) PeersManager() *peers.Manager {
|
||||
return Create(s, func() *peers.Manager {
|
||||
store := s.Store()
|
||||
router := s.Router()
|
||||
|
||||
@@ -14,9 +14,9 @@ type Server interface {
|
||||
SetContainer(key string, container any)
|
||||
}
|
||||
|
||||
// Server holds the HTTP server instance.
|
||||
// Server holds the HTTP BaseServer instance.
|
||||
// Add any additional fields you need, such as database connections, config, etc.
|
||||
type server struct {
|
||||
type BaseServer struct {
|
||||
// container of dependencies, each dependency is identified by a unique string.
|
||||
container map[string]any
|
||||
}
|
||||
@@ -25,14 +25,14 @@ var log = logging.LoggerForThisPackage()
|
||||
|
||||
// NewServer initializes and configures a new Server instance
|
||||
func NewServer() Server {
|
||||
return &server{
|
||||
return &BaseServer{
|
||||
// @todo shared config
|
||||
container: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
// Start begins listening for HTTP requests on the configured address
|
||||
func (s *server) Start() error {
|
||||
func (s *BaseServer) Start() error {
|
||||
// @todo instead of specifically starting httpserver
|
||||
// have a supervised start/stop of dependencies instead.
|
||||
// e.g. http, grpc, metrics, crons, etc
|
||||
@@ -40,21 +40,21 @@ func (s *server) Start() error {
|
||||
}
|
||||
|
||||
// Stop attempts a graceful shutdown, waiting up to 5 seconds for active connections to finish
|
||||
func (s *server) Stop() error {
|
||||
func (s *BaseServer) Stop() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return s.HttpServer().Shutdown(ctx)
|
||||
}
|
||||
|
||||
// GetContainer retrieves a dependency from the server's container by its key
|
||||
func (s *server) GetContainer(key string) (any, bool) {
|
||||
// GetContainer retrieves a dependency from the BaseServer's container by its key
|
||||
func (s *BaseServer) GetContainer(key string) (any, bool) {
|
||||
container, exists := s.container[key]
|
||||
return container, exists
|
||||
}
|
||||
|
||||
// SetContainer stores a dependency in the server's container with the specified key
|
||||
func (s *server) SetContainer(key string, container any) {
|
||||
// SetContainer stores a dependency in the BaseServer's container with the specified key
|
||||
func (s *BaseServer) SetContainer(key string, container any) {
|
||||
if _, exists := s.container[key]; exists {
|
||||
log.Errorf("container with key %s already exists", key)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user