@@ -16,10 +16,16 @@ import (
1616 "gpustack.ai/gpustack/pkg/utils/netx"
1717)
1818
19+ // Server is a web server that can serve webhooks and other HTTP handlers.
1920type Server interface {
2021 ctrlwebhook.Server
2122
23+ // HostPort returns the host and port that the server is listening on.
2224 HostPort () (string , int , error )
25+
26+ // NotFoundHandler sets the handler for requests that do not match any registered path.
27+ // If not set, the default http.NotFoundHandler will be used.
28+ NotFoundHandler (http.Handler )
2329}
2430
2531// New creates a Server with the given configuration.
@@ -32,7 +38,7 @@ func New(c *Config) (Server, error) {
3238 return nil , errors .New ("listener must be a TCP listener" )
3339 }
3440
35- return server {
41+ return & server {
3642 listener : c .Listener ,
3743 host : tcpAddr .IP .String (),
3844 port : tcpAddr .Port ,
@@ -42,23 +48,24 @@ func New(c *Config) (Server, error) {
4248}
4349
4450type server struct {
45- listener net.Listener
46- host string
47- port int
48- runners []manager.Runnable
49- mux * http.ServeMux
51+ listener net.Listener
52+ host string
53+ port int
54+ runners []manager.Runnable
55+ mux * http.ServeMux
56+ notFoundHandler http.Handler
5057}
5158
52- func (server ) NeedLeaderElection () bool {
59+ func (* server ) NeedLeaderElection () bool {
5360 return false
5461}
5562
56- func (s server ) Register (path string , hook http.Handler ) {
63+ func (s * server ) Register (path string , hook http.Handler ) {
5764 s .mux .Handle (path , hook )
5865}
5966
60- func (s server ) Start (ctx context.Context ) error {
61- srv := newHttpServer ( s . mux )
67+ func (s * server ) Start (ctx context.Context ) error {
68+ srv := s . getServer ( )
6269
6370 gp := gox .GroupWithContextIn (ctx )
6471 for i := range s .runners {
@@ -84,17 +91,43 @@ func (s server) Start(ctx context.Context) error {
8491 return gp .Wait ()
8592}
8693
87- func (s server ) StartedChecker () healthz.Checker {
94+ func (s * server ) StartedChecker () healthz.Checker {
8895 addr := net .JoinHostPort (s .host , strconv .Itoa (s .port ))
8996 return func (req * http.Request ) error {
9097 return netx .IsConnected (req .Context (), "tls" , addr , 10 * time .Second )
9198 }
9299}
93100
94- func (s server ) WebhookMux () * http.ServeMux {
101+ func (s * server ) WebhookMux () * http.ServeMux {
95102 return s .mux
96103}
97104
98- func (s server ) HostPort () (string , int , error ) {
105+ func (s * server ) HostPort () (string , int , error ) {
99106 return s .host , s .port , nil
100107}
108+
109+ func (s * server ) NotFoundHandler (notFoundHandler http.Handler ) {
110+ s .notFoundHandler = notFoundHandler
111+ }
112+
113+ func (s * server ) getNotFoundHandler () http.Handler {
114+ if s .notFoundHandler == nil {
115+ return http .NotFoundHandler ()
116+ }
117+ return s .notFoundHandler
118+ }
119+
120+ func (s * server ) getServer () * http.Server {
121+ nf := s .getNotFoundHandler ()
122+
123+ h := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
124+ h , pattern := s .mux .Handler (r )
125+ if pattern == "" {
126+ nf .ServeHTTP (w , r )
127+ return
128+ }
129+ h .ServeHTTP (w , r )
130+ })
131+
132+ return newHttpServer (h )
133+ }
0 commit comments