mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-26 02:08:34 +02:00
refactor(api): restructured API for type safety, maintainability and docs generation
- 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
This commit is contained in:
72
internal/net/gphttp/loadbalancer/server.go
Normal file
72
internal/net/gphttp/loadbalancer/server.go
Normal file
@@ -0,0 +1,72 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user