mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-18 15:34:38 +01:00
- These changes makes the API incombatible with previous versions - Added new types for error handling, success responses, and health checks. - Updated health check logic to utilize the new types for better clarity and structure. - Refactored existing handlers to improve response consistency and error handling. - Updated Makefile to include a new target for generating API types from Swagger. - Updated "new agent" API to respond an encrypted cert pair
73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package loadbalancer
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
|
nettypes "github.com/yusing/go-proxy/internal/net/types"
|
|
"github.com/yusing/go-proxy/internal/types"
|
|
U "github.com/yusing/go-proxy/internal/utils"
|
|
)
|
|
|
|
type server struct {
|
|
_ U.NoCopy
|
|
|
|
name string
|
|
url *nettypes.URL
|
|
weight int
|
|
|
|
http.Handler `json:"-"`
|
|
types.HealthMonitor
|
|
}
|
|
|
|
func NewServer(name string, url *nettypes.URL, weight int, handler http.Handler, healthMon types.HealthMonitor) types.LoadBalancerServer {
|
|
srv := &server{
|
|
name: name,
|
|
url: url,
|
|
weight: weight,
|
|
Handler: handler,
|
|
HealthMonitor: healthMon,
|
|
}
|
|
return srv
|
|
}
|
|
|
|
func TestNewServer[T ~int | ~float32 | ~float64](weight T) types.LoadBalancerServer {
|
|
srv := &server{
|
|
weight: int(weight),
|
|
url: nettypes.MustParseURL("http://localhost"),
|
|
}
|
|
return srv
|
|
}
|
|
|
|
func (srv *server) Name() string {
|
|
return srv.name
|
|
}
|
|
|
|
func (srv *server) URL() *nettypes.URL {
|
|
return srv.url
|
|
}
|
|
|
|
func (srv *server) Key() string {
|
|
return srv.url.Host
|
|
}
|
|
|
|
func (srv *server) Weight() int {
|
|
return srv.weight
|
|
}
|
|
|
|
func (srv *server) SetWeight(weight int) {
|
|
srv.weight = weight
|
|
}
|
|
|
|
func (srv *server) String() string {
|
|
return srv.name
|
|
}
|
|
|
|
func (srv *server) TryWake() error {
|
|
waker, ok := srv.Handler.(idlewatcher.Waker)
|
|
if ok {
|
|
return waker.Wake()
|
|
}
|
|
return nil
|
|
}
|