mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
embed: support registering user handlers
This commit is contained in:
parent
ec5c5d9ddf
commit
86de0797e1
@ -17,6 +17,7 @@ package embed
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
@ -98,6 +99,12 @@ type Config struct {
|
||||
|
||||
// ForceNewCluster starts a new cluster even if previously started; unsafe.
|
||||
ForceNewCluster bool `json:"force-new-cluster"`
|
||||
|
||||
// UserHandlers is for registering users handlers and only used for
|
||||
// embedding etcd into other applications.
|
||||
// The map key is the route path for the handler, and
|
||||
// you must ensure it can't be conflicted with etcd's.
|
||||
UserHandlers map[string]http.Handler `json:"-"`
|
||||
}
|
||||
|
||||
// configYAML holds the config suitable for yaml parsing
|
||||
|
@ -277,6 +277,7 @@ func startClientListeners(cfg *Config) (sctxs map[string]*serveCtx, err error) {
|
||||
plog.Info("stopping listening for client requests on ", u.Host)
|
||||
}
|
||||
}()
|
||||
sctx.userHandlers = cfg.UserHandlers
|
||||
sctxs[u.Host] = sctx
|
||||
}
|
||||
return sctxs, nil
|
||||
|
@ -42,6 +42,8 @@ type serveCtx struct {
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
userHandlers map[string]http.Handler
|
||||
}
|
||||
|
||||
func newServeCtx() *serveCtx {
|
||||
@ -72,9 +74,8 @@ func (sctx *serveCtx) serve(s *etcdserver.EtcdServer, tlscfg *tls.Config, handle
|
||||
return err
|
||||
}
|
||||
|
||||
httpmux := http.NewServeMux()
|
||||
httpmux.Handle("/v3alpha/", gwmux)
|
||||
httpmux.Handle("/", handler)
|
||||
httpmux := sctx.createMux(gwmux, handler)
|
||||
|
||||
srvhttp := &http.Server{
|
||||
Handler: httpmux,
|
||||
ErrorLog: logger, // do not log user error
|
||||
@ -100,9 +101,8 @@ func (sctx *serveCtx) serve(s *etcdserver.EtcdServer, tlscfg *tls.Config, handle
|
||||
|
||||
tlsl := tls.NewListener(m.Match(cmux.Any()), tlscfg)
|
||||
// TODO: add debug flag; enable logging when debug flag is set
|
||||
httpmux := http.NewServeMux()
|
||||
httpmux.Handle("/v3alpha/", gwmux)
|
||||
httpmux.Handle("/", handler)
|
||||
httpmux := sctx.createMux(gwmux, handler)
|
||||
|
||||
srv := &http.Server{
|
||||
Handler: httpmux,
|
||||
TLSConfig: tlscfg,
|
||||
@ -170,3 +170,14 @@ func (sctx *serveCtx) registerGateway(opts []grpc.DialOption) (*gw.ServeMux, err
|
||||
}
|
||||
return gwmux, nil
|
||||
}
|
||||
|
||||
func (sctx *serveCtx) createMux(gwmux *gw.ServeMux, handler http.Handler) *http.ServeMux {
|
||||
httpmux := http.NewServeMux()
|
||||
for path, h := range sctx.userHandlers {
|
||||
httpmux.Handle(path, h)
|
||||
}
|
||||
|
||||
httpmux.Handle("/v3alpha/", gwmux)
|
||||
httpmux.Handle("/", handler)
|
||||
return httpmux
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user