Update -normalise text, add config.DefaultOpts for clearer call sites (@kegsay review comments)

This commit is contained in:
Neil Alexander 2022-07-12 12:27:58 +01:00
parent 79e20b623a
commit b1ace48c72
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
22 changed files with 126 additions and 77 deletions

View file

@ -243,7 +243,10 @@ func (m *DendriteMonolith) Start() {
prefix := hex.EncodeToString(pk)
cfg := &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
cfg.Global.PrivateKey = sk
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)

View file

@ -82,7 +82,10 @@ func (m *DendriteMonolith) Start() {
m.YggdrasilNode = ygg
cfg := &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(ygg.DerivedServerName())
cfg.Global.PrivateKey = ygg.PrivateKey()
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)

View file

@ -181,7 +181,10 @@ func TestValidationOfApplicationServices(t *testing.T) {
// Set up a config
fakeConfig := &config.Dendrite{}
fakeConfig.Defaults(true, true)
fakeConfig.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
fakeConfig.Global.ServerName = "localhost"
fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService}

View file

@ -127,7 +127,10 @@ func main() {
}()
cfg := &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
cfg.Global.PrivateKey = sk
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)

View file

@ -78,7 +78,10 @@ func main() {
if configFlagSet {
cfg = setup.ParseFlags(true)
} else {
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", *instanceName))
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", *instanceName))
cfg.MediaAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-mediaapi.db", *instanceName))

View file

@ -14,7 +14,7 @@ func main() {
defaultsForCI := flag.Bool("ci", false, "Populate the configuration with sane defaults for use in CI")
serverName := flag.String("server", "", "The domain name of the server if not 'localhost'")
dbURI := flag.String("db", "", "The DB URI to use for all components if not SQLite files")
normalise := flag.String("normalise", "", "Normalise a given configuration file")
normalise := flag.String("normalise", "", "Normalise an existing configuration file by adding new/missing options and defaults")
polylith := flag.Bool("polylith", false, "Generate a config that makes sense for polylith deployments")
flag.Parse()
@ -23,7 +23,10 @@ func main() {
cfg = &config.Dendrite{
Version: config.Version,
}
cfg.Defaults(true, !*polylith)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: !*polylith,
})
} else {
var err error
if cfg, err = config.Load(*normalise, !*polylith); err != nil {

View file

@ -75,7 +75,10 @@ func TestMain(m *testing.M) {
// Draw up just enough Dendrite config for the server key
// API to work.
cfg := &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(s.name)
cfg.Global.PrivateKey = testPriv
cfg.Global.JetStream.InMemory = true

View file

@ -252,7 +252,10 @@ func testFederationAPIJoinThenKeyUpdate(t *testing.T, dbType test.DBType) {
func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
_, privKey, _ := ed25519.GenerateKey(nil)
cfg := &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.KeyID = gomatrixserverlib.KeyID("ed25519:auto")
cfg.Global.ServerName = gomatrixserverlib.ServerName("localhost")
cfg.Global.PrivateKey = privKey

View file

@ -211,7 +211,10 @@ func loadConfig(
monolithic bool,
) (*Dendrite, error) {
var c Dendrite
c.Defaults(false, monolithic)
c.Defaults(DefaultOpts{
Generate: false,
Monolithic: monolithic,
})
c.IsMonolith = monolithic
var err error
@ -292,11 +295,16 @@ func (config *Dendrite) Derive() error {
return nil
}
type DefaultOpts struct {
Generate bool
Monolithic bool
}
// SetDefaults sets default config values if they are not explicitly set.
func (c *Dendrite) Defaults(generate bool, isMonolith bool) {
func (c *Dendrite) Defaults(opts DefaultOpts) {
c.Version = Version
if generate {
if opts.Generate {
c.Logging = []LogrusHook{
{
Type: "file",
@ -308,17 +316,16 @@ func (c *Dendrite) Defaults(generate bool, isMonolith bool) {
}
}
c.Global.Defaults(generate, isMonolith)
c.ClientAPI.Defaults(generate, isMonolith)
c.FederationAPI.Defaults(generate, isMonolith)
c.KeyServer.Defaults(generate, isMonolith)
c.MediaAPI.Defaults(generate, isMonolith)
c.RoomServer.Defaults(generate, isMonolith)
c.SyncAPI.Defaults(generate, isMonolith)
c.UserAPI.Defaults(generate, isMonolith)
c.AppServiceAPI.Defaults(generate, isMonolith)
c.MSCs.Defaults(generate, isMonolith)
c.Global.Defaults(opts)
c.ClientAPI.Defaults(opts)
c.FederationAPI.Defaults(opts)
c.KeyServer.Defaults(opts)
c.MediaAPI.Defaults(opts)
c.RoomServer.Defaults(opts)
c.SyncAPI.Defaults(opts)
c.UserAPI.Defaults(opts)
c.AppServiceAPI.Defaults(opts)
c.MSCs.Defaults(opts)
c.Wiring()
}

View file

@ -40,14 +40,14 @@ type AppServiceAPI struct {
ConfigFiles []string `yaml:"config_files"`
}
func (c *AppServiceAPI) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *AppServiceAPI) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7777"
c.InternalAPI.Connect = "http://localhost:7777"
c.Database.Defaults(5)
}
if generate {
if !isMonolith {
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:appservice.db"
}
}

View file

@ -51,8 +51,8 @@ type ClientAPI struct {
MSCs *MSCs `yaml:"-"`
}
func (c *ClientAPI) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *ClientAPI) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7771"
c.InternalAPI.Connect = "http://localhost:7771"
c.ExternalAPI.Listen = "http://[::]:8071"

View file

@ -30,8 +30,8 @@ type FederationAPI struct {
PreferDirectFetch bool `yaml:"prefer_direct_fetch"`
}
func (c *FederationAPI) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *FederationAPI) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7772"
c.InternalAPI.Connect = "http://localhost:7772"
c.ExternalAPI.Listen = "http://[::]:8072"
@ -39,7 +39,7 @@ func (c *FederationAPI) Defaults(generate bool, isMonolith bool) {
}
c.FederationMaxRetries = 16
c.DisableTLSValidation = false
if generate {
if opts.Generate {
c.KeyPerspectives = KeyPerspectives{
{
ServerName: "matrix.org",
@ -55,7 +55,7 @@ func (c *FederationAPI) Defaults(generate bool, isMonolith bool) {
},
},
}
if !isMonolith {
if !opts.Monolithic {
c.Database.ConnectionString = "file:federationapi.db"
}
}

View file

@ -80,8 +80,8 @@ type Global struct {
Cache Cache `yaml:"cache"`
}
func (c *Global) Defaults(generate bool, isMonolith bool) {
if generate {
func (c *Global) Defaults(opts DefaultOpts) {
if opts.Generate {
c.ServerName = "localhost"
c.PrivateKeyPath = "matrix_key.pem"
_, c.PrivateKey, _ = ed25519.GenerateKey(rand.New(rand.NewSource(0)))
@ -92,16 +92,16 @@ func (c *Global) Defaults(generate bool, isMonolith bool) {
}
}
c.KeyValidityPeriod = time.Hour * 24 * 7
if isMonolith {
if opts.Monolithic {
c.DatabaseOptions.Defaults(90)
}
c.JetStream.Defaults(generate)
c.Metrics.Defaults(generate)
c.JetStream.Defaults(opts)
c.Metrics.Defaults(opts)
c.DNSCache.Defaults()
c.Sentry.Defaults()
c.ServerNotices.Defaults(generate)
c.ServerNotices.Defaults(opts)
c.ReportStats.Defaults()
c.Cache.Defaults(generate)
c.Cache.Defaults()
}
func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) {
@ -145,9 +145,9 @@ type Metrics struct {
} `yaml:"basic_auth"`
}
func (c *Metrics) Defaults(generate bool) {
func (c *Metrics) Defaults(opts DefaultOpts) {
c.Enabled = false
if generate {
if opts.Generate {
c.BasicAuth.Username = "metrics"
c.BasicAuth.Password = "metrics"
}
@ -169,8 +169,8 @@ type ServerNotices struct {
RoomName string `yaml:"room_name"`
}
func (c *ServerNotices) Defaults(generate bool) {
if generate {
func (c *ServerNotices) Defaults(opts DefaultOpts) {
if opts.Generate {
c.Enabled = true
c.LocalPart = "_server"
c.DisplayName = "Server Alert"
@ -186,7 +186,7 @@ type Cache struct {
MaxAge time.Duration `yaml:"max_age"`
}
func (c *Cache) Defaults(generate bool) {
func (c *Cache) Defaults() {
c.EstimatedMaxSize = 1024 * 1024 * 1024 // 1GB
c.MaxAge = time.Hour
}

View file

@ -27,10 +27,10 @@ func (c *JetStream) Durable(name string) string {
return c.Prefixed(name)
}
func (c *JetStream) Defaults(generate bool) {
func (c *JetStream) Defaults(opts DefaultOpts) {
c.Addresses = []string{}
c.TopicPrefix = "Dendrite"
if generate {
if opts.Generate {
c.StoragePath = Path("./")
}
}

View file

@ -8,14 +8,14 @@ type KeyServer struct {
Database DatabaseOptions `yaml:"database,omitempty"`
}
func (c *KeyServer) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *KeyServer) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7779"
c.InternalAPI.Connect = "http://localhost:7779"
c.Database.Defaults(10)
}
if generate {
if !isMonolith {
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:keyserver.db"
}
}

View file

@ -38,8 +38,8 @@ type MediaAPI struct {
// DefaultMaxFileSizeBytes defines the default file size allowed in transfers
var DefaultMaxFileSizeBytes = FileSizeBytes(10485760)
func (c *MediaAPI) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *MediaAPI) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7774"
c.InternalAPI.Connect = "http://localhost:7774"
c.ExternalAPI.Listen = "http://[::]:8074"
@ -47,7 +47,7 @@ func (c *MediaAPI) Defaults(generate bool, isMonolith bool) {
}
c.MaxFileSizeBytes = DefaultMaxFileSizeBytes
c.MaxThumbnailGenerators = 10
if generate {
if opts.Generate {
c.ThumbnailSizes = []ThumbnailSize{
{
Width: 32,
@ -65,7 +65,7 @@ func (c *MediaAPI) Defaults(generate bool, isMonolith bool) {
ResizeMethod: "scale",
},
}
if !isMonolith {
if !opts.Monolithic {
c.Database.ConnectionString = "file:mediaapi.db"
}
c.BasePath = "./media_store"

View file

@ -13,12 +13,12 @@ type MSCs struct {
Database DatabaseOptions `yaml:"database,omitempty"`
}
func (c *MSCs) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *MSCs) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.Database.Defaults(5)
}
if generate {
if !isMonolith {
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:mscs.db"
}
}

View file

@ -8,14 +8,14 @@ type RoomServer struct {
Database DatabaseOptions `yaml:"database,omitempty"`
}
func (c *RoomServer) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *RoomServer) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7770"
c.InternalAPI.Connect = "http://localhost:7770"
c.Database.Defaults(10)
}
if generate {
if !isMonolith {
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:roomserver.db"
}
}

View file

@ -11,15 +11,15 @@ type SyncAPI struct {
RealIPHeader string `yaml:"real_ip_header"`
}
func (c *SyncAPI) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *SyncAPI) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7773"
c.InternalAPI.Connect = "http://localhost:7773"
c.ExternalAPI.Listen = "http://localhost:8073"
c.Database.Defaults(10)
}
if generate {
if !isMonolith {
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:syncapi.db"
}
}

View file

@ -23,16 +23,16 @@ type UserAPI struct {
const DefaultOpenIDTokenLifetimeMS = 3600000 // 60 minutes
func (c *UserAPI) Defaults(generate bool, isMonolith bool) {
if !isMonolith {
func (c *UserAPI) Defaults(opts DefaultOpts) {
if !opts.Monolithic {
c.InternalAPI.Listen = "http://localhost:7781"
c.InternalAPI.Connect = "http://localhost:7781"
c.AccountDatabase.Defaults(10)
}
c.BCryptCost = bcrypt.DefaultCost
c.OpenIDTokenLifetimeMS = DefaultOpenIDTokenLifetimeMS
if generate {
if !isMonolith {
if opts.Generate {
if !opts.Monolithic {
c.AccountDatabase.ConnectionString = "file:userapi_accounts.db"
}
}

View file

@ -544,7 +544,10 @@ func (r *testRoomserverAPI) QueryMembershipForUser(ctx context.Context, req *roo
func injectEvents(t *testing.T, userAPI userapi.UserInternalAPI, rsAPI roomserver.RoomserverInternalAPI, events []*gomatrixserverlib.HeaderedEvent) *mux.Router {
t.Helper()
cfg := &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = "localhost"
cfg.MSCs.Database.ConnectionString = "file:msc2836_test.db"
cfg.MSCs.MSCs = []string{"msc2836"}

View file

@ -30,13 +30,22 @@ import (
func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) {
var cfg config.Dendrite
cfg.Defaults(false, true)
cfg.Defaults(config.DefaultOpts{
Generate: false,
Monolithic: true,
})
cfg.Global.JetStream.InMemory = true
switch dbType {
case test.DBTypePostgres:
cfg.Global.Defaults(true, true) // autogen a signing key
cfg.MediaAPI.Defaults(true, true) // autogen a media path
cfg.Global.Defaults(config.DefaultOpts{ // autogen a signing key
Generate: true,
Monolithic: true,
})
cfg.MediaAPI.Defaults(config.DefaultOpts{ // autogen a media path
Generate: true,
Monolithic: true,
})
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
// the file system event with InMemory=true :(
cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType)
@ -49,7 +58,10 @@ func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, f
}
return base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics), close
case test.DBTypeSQLite:
cfg.Defaults(true, true) // sets a sqlite db per component
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
}) // sets a sqlite db per component
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
// the file system event with InMemory=true :(
cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType)
@ -82,7 +94,10 @@ func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, f
func Base(cfg *config.Dendrite) (*base.BaseDendrite, nats.JetStreamContext, *nats.Conn) {
if cfg == nil {
cfg = &config.Dendrite{}
cfg.Defaults(true, true)
cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
}
cfg.Global.JetStream.InMemory = true
base := base.NewBaseDendrite(cfg, "Tests")