Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: move profile to config component (#3666)

Danny Xu82be688b1dc5
diff --git a/bin/server/cmd/profile.go b/bin/server/cmd/profile.go
index 9c9c5a0ef2..e1ab801dec 100644
--- a/bin/server/cmd/profile.go
+++ b/bin/server/cmd/profile.go
@@ -2,15 +2,14 @@ package cmd
import (
"fmt"
- "time"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/app/feishu"
- "github.com/bytebase/bytebase/server"
+ "github.com/bytebase/bytebase/server/component/config"
)
-func getBaseProfile() server.Profile {
+func getBaseProfile() config.Profile {
var demoDataDir string
if flags.demo {
demoName := string(common.ReleaseModeDev)
@@ -26,7 +25,7 @@ func getBaseProfile() server.Profile {
// Using flags.port + 1 as our datastore port
datastorePort := flags.port + 1
- return server.Profile{
+ return config.Profile{
ExternalURL: flags.externalURL,
DatastorePort: datastorePort,
Readonly: flags.readonly,
@@ -44,40 +43,3 @@ func getBaseProfile() server.Profile {
FeishuAPIURL: feishu.APIPath,
}
}
-
-// GetTestProfile will return a profile for testing.
-// We require port as an argument of GetTestProfile so that test can run in parallel in different ports.
-func GetTestProfile(dataDir, resourceDirOverride string, port int, feishuAPIURL string) server.Profile {
- // Using flags.port + 1 as our datastore port
- datastorePort := port + 1
- return server.Profile{
- Mode: common.ReleaseModeDev,
- ExternalURL: fmt.Sprintf("http://localhost:%d", port),
- DatastorePort: datastorePort,
- PgUser: "bbtest",
- DataDir: dataDir,
- ResourceDirOverride: resourceDirOverride,
- DemoDataDir: fmt.Sprintf("demo/%s", common.ReleaseModeDev),
- BackupRunnerInterval: 10 * time.Second,
- BackupStorageBackend: api.BackupStorageBackendLocal,
- FeishuAPIURL: feishuAPIURL,
- }
-}
-
-// GetTestProfileWithExternalPg will return a profile for testing with external Postgres.
-// We require port as an argument of GetTestProfile so that test can run in parallel in different ports,
-// pgURL for connect to Postgres.
-func GetTestProfileWithExternalPg(dataDir, resourceDirOverride string, port int, pgUser string, pgURL string, feishuAPIURL string) server.Profile {
- return server.Profile{
- Mode: common.ReleaseModeDev,
- ExternalURL: fmt.Sprintf("http://localhost:%d", port),
- PgUser: pgUser,
- DataDir: dataDir,
- ResourceDirOverride: resourceDirOverride,
- DemoDataDir: fmt.Sprintf("demo/%s", common.ReleaseModeDev),
- BackupRunnerInterval: 10 * time.Second,
- BackupStorageBackend: api.BackupStorageBackendLocal,
- FeishuAPIURL: feishuAPIURL,
- PgURL: pgURL,
- }
-}
diff --git a/bin/server/cmd/profile_dev.go b/bin/server/cmd/profile_dev.go
index b3d811af3e..8b19714620 100644
--- a/bin/server/cmd/profile_dev.go
+++ b/bin/server/cmd/profile_dev.go
@@ -7,10 +7,10 @@ import (
"time"
"github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/server"
+ "github.com/bytebase/bytebase/server/component/config"
)
-func activeProfile(dataDir string) server.Profile {
+func activeProfile(dataDir string) config.Profile {
p := getBaseProfile()
p.Mode = common.ReleaseModeDev
p.PgUser = "bbdev"
diff --git a/bin/server/cmd/profile_release.go b/bin/server/cmd/profile_release.go
index 4fe21632bd..eb377db5dd 100644
--- a/bin/server/cmd/profile_release.go
+++ b/bin/server/cmd/profile_release.go
@@ -7,10 +7,10 @@ import (
"time"
"github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/server"
+ "github.com/bytebase/bytebase/server/component/config"
)
-func activeProfile(dataDir string) server.Profile {
+func activeProfile(dataDir string) config.Profile {
p := getBaseProfile()
p.Mode = common.ReleaseModeProd
p.PgUser = "bb"
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 62795621be..12edd88c6b 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -20,37 +20,6 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/server"
-
- // Register clickhouse driver.
- _ "github.com/bytebase/bytebase/plugin/db/clickhouse"
- // Register mysql driver.
- _ "github.com/bytebase/bytebase/plugin/db/mysql"
- // Register postgres driver.
- _ "github.com/bytebase/bytebase/plugin/db/pg"
- // Register snowflake driver.
- _ "github.com/bytebase/bytebase/plugin/db/snowflake"
- // Register sqlite driver.
- _ "github.com/bytebase/bytebase/plugin/db/sqlite"
-
- // Register pingcap parser driver.
- _ "github.com/pingcap/tidb/types/parser_driver"
- // Register fake advisor.
- _ "github.com/bytebase/bytebase/plugin/advisor/fake"
- // Register mysql advisor.
- _ "github.com/bytebase/bytebase/plugin/advisor/mysql"
- // Register postgresql advisor.
- _ "github.com/bytebase/bytebase/plugin/advisor/pg"
-
- // Register mysql differ driver.
- _ "github.com/bytebase/bytebase/plugin/parser/differ/mysql"
- // Register postgres differ driver.
- _ "github.com/bytebase/bytebase/plugin/parser/differ/pg"
- // Register postgres parser driver.
- _ "github.com/bytebase/bytebase/plugin/parser/engine/pg"
- // Register mysql transform driver.
- _ "github.com/bytebase/bytebase/plugin/parser/transform/mysql"
- // Register mysql edit driver.
- _ "github.com/bytebase/bytebase/plugin/parser/edit/mysql"
)
// -----------------------------------Global constant BEGIN----------------------------------------.
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index fb0a17a10e..5155d37483 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -15,7 +15,7 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/db"
- "github.com/bytebase/bytebase/server/factory/dbfactory"
+ "github.com/bytebase/bytebase/server/component/dbfactory"
"github.com/bytebase/bytebase/store"
)
diff --git a/server/backup_runner.go b/server/backup_runner.go
index 57e9da69f9..9ae3d432dc 100644
--- a/server/backup_runner.go
+++ b/server/backup_runner.go
@@ -20,11 +20,12 @@ import (
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/db/mysql"
"github.com/bytebase/bytebase/plugin/storage/s3"
+ "github.com/bytebase/bytebase/server/component/config"
"github.com/bytebase/bytebase/store"
)
// NewBackupRunner creates a new backup runner.
-func NewBackupRunner(server *Server, store *store.Store, s3Client *s3.Client, profile *Profile) *BackupRunner {
+func NewBackupRunner(server *Server, store *store.Store, s3Client *s3.Client, profile *config.Profile) *BackupRunner {
return &BackupRunner{
server: server,
store: store,
@@ -39,7 +40,7 @@ type BackupRunner struct {
server *Server
store *store.Store
s3Client *s3.Client
- profile *Profile
+ profile *config.Profile
downloadBinlogInstanceIDs map[int]bool
backupWg sync.WaitGroup
downloadBinlogWg sync.WaitGroup
diff --git a/server/component/config/profile.go b/server/component/config/profile.go
new file mode 100644
index 0000000000..74307828d8
--- /dev/null
+++ b/server/component/config/profile.go
@@ -0,0 +1,67 @@
+// Package config includes all the server configurations in a component.
+package config
+
+import (
+ "time"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
+)
+
+// Profile is the configuration to start main server.
+type Profile struct {
+ // Mode can be "prod" or "dev"
+ Mode common.ReleaseMode
+ // ExternalURL is the URL user visits Bytebase.
+ ExternalURL string
+ // DatastorePort is the binding port for database instance for storing Bytebase metadata.
+ // Only applicable when using embedded PG (PgURL is empty).
+ DatastorePort int
+ // PgUser is the user we use to connect to bytebase's Postgres database.
+ // The name of the database storing metadata is the same as pgUser.
+ PgUser string
+ // When we are running in readonly mode:
+ // - The data file will be opened in readonly mode, no applicable migration or seeding will be applied.
+ // - Requests other than GET will be rejected
+ // - Any operations involving mutation will not start (e.g. Background schema syncer, task scheduler)
+ Readonly bool
+ // DataDir is the directory stores the data including Bytebase's own database, backups, etc.
+ DataDir string
+ // ResourceDirOverride is the directory stores the resources including embedded postgres and mysqlutil.
+ ResourceDirOverride string
+ // Debug decides the log level
+ Debug bool
+ // Demo decides that whether load demo data.
+ Demo bool
+ // DemoDataDir points to where to populate the initial data.
+ DemoDataDir string
+ // BackupRunnerInterval is the interval for backup runner.
+ BackupRunnerInterval time.Duration
+ // BackupStorageBackend is the backup storage backend.
+ BackupStorageBackend api.BackupStorageBackend
+
+ // Cloud backup related fields
+ BackupRegion string
+ BackupBucket string
+ BackupCredentialFile string
+
+ // IM integration related fields
+ // FeishuAPIURL is the URL of Feishu API server.
+ FeishuAPIURL string
+
+ // Version is the bytebase's version
+ Version string
+ // Git commit hash of the build
+ GitCommit string
+ // PgURL is the optional external PostgreSQL instance connection url
+ PgURL string
+ // MetricConnectionKey is the connection key for metric.
+ MetricConnectionKey string
+ // DisableMetric will disable the metric collector.
+ DisableMetric bool
+}
+
+// UseEmbedDB returns whether to use embedDB.
+func (prof *Profile) UseEmbedDB() bool {
+ return len(prof.PgURL) == 0
+}
diff --git a/server/factory/dbfactory/dbfactory.go b/server/component/dbfactory/dbfactory.go
similarity index 100%
rename from server/factory/dbfactory/dbfactory.go
rename to server/component/dbfactory/dbfactory.go
diff --git a/server/config.go b/server/config.go
index 79dd7a3fbb..9f63512acb 100644
--- a/server/config.go
+++ b/server/config.go
@@ -1,79 +1,6 @@
package server
-import (
- "time"
-
- "github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/common"
-)
-
const (
// secretLength is the length for the secret used to sign the JWT auto token.
secretLength = 32
)
-
-// retrieved via the SettingService upon startup.
-type config struct {
- // secret used to sign the JWT auth token
- secret string
- // workspaceID used to initial the identify for a new workspace.
- workspaceID string
-}
-
-// Profile is the configuration to start main server.
-type Profile struct {
- // Mode can be "prod" or "dev"
- Mode common.ReleaseMode
- // ExternalURL is the URL user visits Bytebase.
- ExternalURL string
- // DatastorePort is the binding port for database instance for storing Bytebase metadata.
- // Only applicable when using embedded PG (PgURL is empty).
- DatastorePort int
- // PgUser is the user we use to connect to bytebase's Postgres database.
- // The name of the database storing metadata is the same as pgUser.
- PgUser string
- // When we are running in readonly mode:
- // - The data file will be opened in readonly mode, no applicable migration or seeding will be applied.
- // - Requests other than GET will be rejected
- // - Any operations involving mutation will not start (e.g. Background schema syncer, task scheduler)
- Readonly bool
- // DataDir is the directory stores the data including Bytebase's own database, backups, etc.
- DataDir string
- // ResourceDirOverride is the directory stores the resources including embedded postgres and mysqlutil.
- ResourceDirOverride string
- // Debug decides the log level
- Debug bool
- // Demo decides that whether load demo data.
- Demo bool
- // DemoDataDir points to where to populate the initial data.
- DemoDataDir string
- // BackupRunnerInterval is the interval for backup runner.
- BackupRunnerInterval time.Duration
- // BackupStorageBackend is the backup storage backend.
- BackupStorageBackend api.BackupStorageBackend
-
- // Cloud backup related fields
- BackupRegion string
- BackupBucket string
- BackupCredentialFile string
-
- // IM integration related fields
- // FeishuAPIURL is the URL of Feishu API server.
- FeishuAPIURL string
-
- // Version is the bytebase's version
- Version string
- // Git commit hash of the build
- GitCommit string
- // PgURL is the optional external PostgreSQL instance connection url
- PgURL string
- // MetricConnectionKey is the connection key for metric.
- MetricConnectionKey string
- // DisableMetric will disable the metric collector.
- DisableMetric bool
-}
-
-// UseEmbedDB returns whether to use embedDB.
-func (prof *Profile) UseEmbedDB() bool {
- return len(prof.PgURL) == 0
-}
diff --git a/server/rollback_runner.go b/server/rollback_runner.go
index 12beb119bf..e38fcf6e13 100644
--- a/server/rollback_runner.go
+++ b/server/rollback_runner.go
@@ -14,7 +14,7 @@ import (
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/db/mysql"
- "github.com/bytebase/bytebase/server/factory/dbfactory"
+ "github.com/bytebase/bytebase/server/component/dbfactory"
"github.com/bytebase/bytebase/store"
)
diff --git a/server/server.go b/server/server.go
index 5a3d177941..3b81182027 100644
--- a/server/server.go
+++ b/server/server.go
@@ -36,8 +36,40 @@ import (
s3bb "github.com/bytebase/bytebase/plugin/storage/s3"
"github.com/bytebase/bytebase/resources/mysqlutil"
"github.com/bytebase/bytebase/resources/postgres"
- "github.com/bytebase/bytebase/server/factory/dbfactory"
+ "github.com/bytebase/bytebase/server/component/config"
+ "github.com/bytebase/bytebase/server/component/dbfactory"
"github.com/bytebase/bytebase/store"
+
+ // Register clickhouse driver.
+ _ "github.com/bytebase/bytebase/plugin/db/clickhouse"
+ // Register mysql driver.
+ _ "github.com/bytebase/bytebase/plugin/db/mysql"
+ // Register postgres driver.
+ _ "github.com/bytebase/bytebase/plugin/db/pg"
+ // Register snowflake driver.
+ _ "github.com/bytebase/bytebase/plugin/db/snowflake"
+ // Register sqlite driver.
+ _ "github.com/bytebase/bytebase/plugin/db/sqlite"
+
+ // Register pingcap parser driver.
+ _ "github.com/pingcap/tidb/types/parser_driver"
+ // Register fake advisor.
+ _ "github.com/bytebase/bytebase/plugin/advisor/fake"
+ // Register mysql advisor.
+ _ "github.com/bytebase/bytebase/plugin/advisor/mysql"
+ // Register postgresql advisor.
+ _ "github.com/bytebase/bytebase/plugin/advisor/pg"
+
+ // Register mysql differ driver.
+ _ "github.com/bytebase/bytebase/plugin/parser/differ/mysql"
+ // Register postgres differ driver.
+ _ "github.com/bytebase/bytebase/plugin/parser/differ/pg"
+ // Register postgres parser driver.
+ _ "github.com/bytebase/bytebase/plugin/parser/engine/pg"
+ // Register mysql transform driver.
+ _ "github.com/bytebase/bytebase/plugin/parser/transform/mysql"
+ // Register mysql edit driver.
+ _ "github.com/bytebase/bytebase/plugin/parser/edit/mysql"
)
const (
@@ -67,7 +99,7 @@ type Server struct {
LicenseService enterpriseAPI.LicenseService
subscription enterpriseAPI.Subscription
- profile Profile
+ profile config.Profile
e *echo.Echo
metaDB *store.MetadataDB
store *store.Store
@@ -120,7 +152,7 @@ var casbinDeveloperPolicy string
// @schemes http
// NewServer creates a server.
-func NewServer(ctx context.Context, prof Profile) (*Server, error) {
+func NewServer(ctx context.Context, prof config.Profile) (*Server, error) {
s := &Server{
profile: prof,
startedTs: time.Now().Unix(),
@@ -413,7 +445,7 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {
return s, nil
}
-func (s *Server) registerOpenAPIRoutes(e *echo.Echo, ce *casbin.Enforcer, prof Profile) {
+func (s *Server) registerOpenAPIRoutes(e *echo.Echo, ce *casbin.Enforcer, prof config.Profile) {
openAPIGroup := e.Group(openAPIPrefix)
openAPIGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
@@ -460,7 +492,15 @@ func (s *Server) initMetricReporter(workspaceID string) {
}
}
-func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
+// retrieved via the SettingService upon startup.
+type workspaceConfig struct {
+ // secret used to sign the JWT auth token
+ secret string
+ // workspaceID used to initial the identify for a new workspace.
+ workspaceID string
+}
+
+func getInitSetting(ctx context.Context, store *store.Store) (*workspaceConfig, error) {
// initial branding
if _, _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{
CreatorID: api.SystemBotID,
@@ -471,7 +511,7 @@ func getInitSetting(ctx context.Context, store *store.Store) (*config, error) {
return nil, err
}
- conf := &config{}
+ conf := &workspaceConfig{}
// initial JWT token
value, err := common.RandomString(secretLength)
diff --git a/tests/tests.go b/tests/tests.go
index 89b09c672e..d034b1ea0a 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -28,7 +28,6 @@ import (
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/bin/server/cmd"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
enterpriseAPI "github.com/bytebase/bytebase/enterprise/api"
@@ -36,6 +35,7 @@ import (
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/parser"
"github.com/bytebase/bytebase/server"
+ componentConfig "github.com/bytebase/bytebase/server/component/config"
"github.com/bytebase/bytebase/tests/fake"
)
@@ -142,7 +142,7 @@ CREATE TABLE book3 (
type controller struct {
server *server.Server
- profile server.Profile
+ profile componentConfig.Profile
client *http.Client
cookie string
vcsProvider fake.VCSProvider
@@ -223,7 +223,7 @@ func (ctl *controller) StartServerWithExternalPg(ctx context.Context, config *co
pgURL := fmt.Sprintf("postgresql://%s@:%d/%s?host=%s", externalPgUser, externalPgPort, databaseName, common.GetPostgresSocketDir())
serverPort := getTestPort()
- profile := cmd.GetTestProfileWithExternalPg(config.dataDir, resourceDirOverride, serverPort, externalPgUser, pgURL, ctl.feishuProvider.APIURL(ctl.feishuURL))
+ profile := getTestProfileWithExternalPg(config.dataDir, resourceDirOverride, serverPort, externalPgUser, pgURL, ctl.feishuProvider.APIURL(ctl.feishuURL))
server, err := server.NewServer(ctx, profile)
if err != nil {
return err
@@ -241,7 +241,7 @@ func (ctl *controller) StartServer(ctx context.Context, config *config) error {
return err
}
serverPort := getTestPortForEmbeddedPg()
- profile := cmd.GetTestProfile(config.dataDir, resourceDirOverride, serverPort, ctl.feishuProvider.APIURL(ctl.feishuURL))
+ profile := getTestProfile(config.dataDir, resourceDirOverride, serverPort, ctl.feishuProvider.APIURL(ctl.feishuURL))
server, err := server.NewServer(ctx, profile)
if err != nil {
return err
@@ -252,6 +252,43 @@ func (ctl *controller) StartServer(ctx context.Context, config *config) error {
return ctl.start(ctx, serverPort)
}
+// GetTestProfile will return a profile for testing.
+// We require port as an argument of GetTestProfile so that test can run in parallel in different ports.
+func getTestProfile(dataDir, resourceDirOverride string, port int, feishuAPIURL string) componentConfig.Profile {
+ // Using flags.port + 1 as our datastore port
+ datastorePort := port + 1
+ return componentConfig.Profile{
+ Mode: common.ReleaseModeDev,
+ ExternalURL: fmt.Sprintf("http://localhost:%d", port),
+ DatastorePort: datastorePort,
+ PgUser: "bbtest",
+ DataDir: dataDir,
+ ResourceDirOverride: resourceDirOverride,
+ DemoDataDir: fmt.Sprintf("demo/%s", common.ReleaseModeDev),
+ BackupRunnerInterval: 10 * time.Second,
+ BackupStorageBackend: api.BackupStorageBackendLocal,
+ FeishuAPIURL: feishuAPIURL,
+ }
+}
+
+// GetTestProfileWithExternalPg will return a profile for testing with external Postgres.
+// We require port as an argument of GetTestProfile so that test can run in parallel in different ports,
+// pgURL for connect to Postgres.
+func getTestProfileWithExternalPg(dataDir, resourceDirOverride string, port int, pgUser string, pgURL string, feishuAPIURL string) componentConfig.Profile {
+ return componentConfig.Profile{
+ Mode: common.ReleaseModeDev,
+ ExternalURL: fmt.Sprintf("http://localhost:%d", port),
+ PgUser: pgUser,
+ DataDir: dataDir,
+ ResourceDirOverride: resourceDirOverride,
+ DemoDataDir: fmt.Sprintf("demo/%s", common.ReleaseModeDev),
+ BackupRunnerInterval: 10 * time.Second,
+ BackupStorageBackend: api.BackupStorageBackendLocal,
+ FeishuAPIURL: feishuAPIURL,
+ PgURL: pgURL,
+ }
+}
+
func (ctl *controller) startMockServers(vcsProviderCreator fake.VCSProviderCreator, feishuProviderCreator fake.FeishuProviderCreator) error {
// Set up VCS provider.
vcsPort := getTestPort()