MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / SetupServerContext

Function SetupServerContext

rest/config.go:1583–1633  ·  view source on GitHub ↗

SetupServerContext creates a new ServerContext given its configuration and performs the context validation.

(ctx context.Context, config *StartupConfig, persistentConfig bool)

Source from the content-addressed store, hash-verified

1581
1582// SetupServerContext creates a new ServerContext given its configuration and performs the context validation.
1583func SetupServerContext(ctx context.Context, config *StartupConfig, persistentConfig bool) (*ServerContext, error) {
1584 // If SetupServerContext is called while any other go routines that might use logging are running, it will
1585 // cause a data race, therefore only initialize logging and other globals on the first call. From a main
1586 // program, there is only one ServerContext.
1587 if serverContextGlobalsInitialized.CompareAndSwap(false, true) {
1588 // Logging config will now have been loaded from command line
1589 // or from a sync_gateway config file so we can validate the
1590 // configuration and setup logging now
1591
1592 if err := config.SetupAndValidateLogging(ctx); err != nil {
1593 // If we didn't set up logging correctly, we *probably* can't log via normal means...
1594 // as a best-effort, last-ditch attempt, we'll log to stderr as well.
1595 log.Printf("[ERR] Error setting up logging: %v", err)
1596 return nil, fmt.Errorf("error setting up logging: %v", err)
1597 }
1598 base.FlushLoggerBuffers()
1599
1600 if err := setGlobalConfig(ctx, config); err != nil {
1601 return nil, err
1602 }
1603 }
1604
1605 base.InfofCtx(ctx, base.KeyAll, "Logging: Console level: %v", base.ConsoleLogLevel())
1606 base.InfofCtx(ctx, base.KeyAll, "Logging: Console keys: %v", base.ConsoleLogKey().EnabledLogKeys())
1607 base.InfofCtx(ctx, base.KeyAll, "Logging: Redaction level: %s", config.Logging.RedactionLevel)
1608
1609 if err := config.Validate(ctx, base.IsEnterpriseEdition()); err != nil {
1610 return nil, err
1611 }
1612
1613 sc := NewServerContext(ctx, config, persistentConfig)
1614
1615 if !base.ServerIsWalrus(sc.Config.Bootstrap.Server) {
1616 err := sc.CheckSupportedCouchbaseVersion(ctx)
1617 if err != nil {
1618 return nil, err
1619 }
1620 err = sc.initializeGocbAdminConnection(ctx)
1621 if err != nil {
1622 return nil, err
1623 }
1624 }
1625 if err := sc.initializeBootstrapConnection(ctx); err != nil {
1626 return nil, err
1627 }
1628
1629 // On successful server context creation, generate startup audit event
1630 base.Audit(ctx, base.AuditIDSyncGatewayStartup, sc.StartupAuditFields())
1631
1632 return sc, nil
1633}
1634
1635func (sc *ServerContext) StartupAuditFields() base.AuditFields {
1636 return base.AuditFields{

Callers 8

TestSetupServerContextFunction · 0.85
StartServerWithConfigFunction · 0.85
TestBootstrapConfigFunction · 0.85
TestComputeMetadataIDFunction · 0.85
legacyServerMainFunction · 0.85
DevTestFetchConfigManualFunction · 0.85

Calls 15

StartupAuditFieldsMethod · 0.95
FlushLoggerBuffersFunction · 0.92
InfofCtxFunction · 0.92
ConsoleLogLevelFunction · 0.92
ConsoleLogKeyFunction · 0.92
IsEnterpriseEditionFunction · 0.92
ServerIsWalrusFunction · 0.92
AuditFunction · 0.92
setGlobalConfigFunction · 0.85

Tested by 5

TestSetupServerContextFunction · 0.68
TestBootstrapConfigFunction · 0.68
TestComputeMetadataIDFunction · 0.68
DevTestFetchConfigManualFunction · 0.68