SetupServerContext creates a new ServerContext given its configuration and performs the context validation.
(ctx context.Context, config *StartupConfig, persistentConfig bool)
| 1581 | |
| 1582 | // SetupServerContext creates a new ServerContext given its configuration and performs the context validation. |
| 1583 | func 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 | |
| 1635 | func (sc *ServerContext) StartupAuditFields() base.AuditFields { |
| 1636 | return base.AuditFields{ |