Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrks committed Jul 8, 2024
1 parent 201c773 commit b21f5a8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
6 changes: 0 additions & 6 deletions rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1362,12 +1362,6 @@ func (sc *ServerContext) Serve(ctx context.Context, config *StartupConfig, t ser
return serveFn()
}

func (sc *ServerContext) addHTTPServer(t serverType, s *serverInfo) {
sc.lock.Lock()
defer sc.lock.Unlock()
sc._httpServers[t] = s
}

// Validate returns errors errors if invalid config is present
func (sc *StartupConfig) Validate(ctx context.Context, isEnterpriseEdition bool) (errorMessages error) {
var multiError *base.MultiError
Expand Down
10 changes: 9 additions & 1 deletion rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,23 @@ func newHandler(server *ServerContext, privs handlerPrivs, serverType serverType
func (h *handler) ctx() context.Context {
if h.rqCtx == nil {
ctx := base.CorrelationIDLogCtx(h.rq.Context(), h.formatSerialNumber())
serverAddr, err := h.getServerAddr()
if err != nil {
base.AssertfCtx(ctx, "Error getting server address: %v", err)
}
ctx = base.RequestLogCtx(ctx, base.RequestData{
RequestHost: h.rq.Host, // FIXME: This is client-supplied data (Host header); replace with actual server listener address for API serving request!
RequestHost: serverAddr,
RequestRemoteAddr: h.rq.RemoteAddr,
})
h.rqCtx = ctx
}
return h.rqCtx
}

func (h *handler) getServerAddr() (string, error) {
return h.server.getServerAddr(h.serverType)
}

func (h *handler) addDatabaseLogContext(dbName string, logConfig *base.DbLogConfig) {
if dbName != "" {
h.rqCtx = base.DatabaseLogCtx(h.ctx(), dbName, logConfig)
Expand Down
26 changes: 26 additions & 0 deletions rest/server_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,32 @@ func (sc *ServerContext) WaitForRESTAPIs(ctx context.Context) error {
return err
}

// getServerAddr returns the address as assigned by the listener. This will return an addressable address, whereas ":0" is a valid value to pass to server.
func (sc *ServerContext) getServerAddr(s serverType) (string, error) {
server, err := sc.getHTTPServer(s)
if err != nil {
return "", err
}
return server.addr.String(), nil
}

func (sc *ServerContext) addHTTPServer(t serverType, s *serverInfo) {
sc.lock.Lock()
defer sc.lock.Unlock()
sc._httpServers[t] = s
}

// getHTTPServer returns information about the given HTTP server.
func (sc *ServerContext) getHTTPServer(t serverType) (*serverInfo, error) {
sc.lock.RLock()
defer sc.lock.RUnlock()
s, ok := sc._httpServers[t]
if !ok {
return nil, fmt.Errorf("server type %q not found running in server context", t)
}
return s, nil
}

// PostStartup runs anything that relies on SG being fully started (i.e. sgreplicate)
func (sc *ServerContext) PostStartup() {
// Delay DatabaseContext processes starting up, e.g. to avoid replication reassignment churn when a Sync Gateway Cluster is being initialized
Expand Down
15 changes: 6 additions & 9 deletions rest/utilities_testing_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,14 @@ func BootstrapAdminRequestWithHeaders(t *testing.T, sc *ServerContext, method, p
return doBootstrapAdminRequest(t, sc, method, path, body, headers)
}

// getServerAddr returns the address as assigned by the listener. This will return an addressable address, whereas ":0" is a valid value to pass to server.
func (sc *ServerContext) getServerAddr(t *testing.T, s serverType) string {
sc.lock.RLock()
defer sc.lock.RUnlock()
server := sc._httpServers[s]
require.NotNil(t, server, "Server %s not found in server context", s)
return server.addr.String()
func mustGetServerAddr(t *testing.T, sc *ServerContext, s serverType) string {
addr, err := sc.getServerAddr(s)
require.NoError(t, err, "Server %s not found in server context", s)
return addr
}

func doBootstrapAdminRequest(t *testing.T, sc *ServerContext, method, path, body string, headers map[string]string) boostrapResponse {
host := "http://" + sc.getServerAddr(t, adminServer)
host := "http://" + mustGetServerAddr(t, sc, adminServer)
url := host + path

buf := bytes.NewBufferString(body)
Expand Down Expand Up @@ -134,7 +131,7 @@ func doBootstrapAdminRequest(t *testing.T, sc *ServerContext, method, path, body
}

func doBootstrapRequest(t *testing.T, sc *ServerContext, method, path, body string, headers map[string]string, server serverType) boostrapResponse {
host := "http://" + sc.getServerAddr(t, server)
host := "http://" + mustGetServerAddr(t, sc, server)
url := host + path

buf := bytes.NewBufferString(body)
Expand Down

0 comments on commit b21f5a8

Please sign in to comment.