Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: add util function HasPrefixes (#180)
satoru7d06934f812b
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.goindex 329740e15a..ea1b78bca3 100644--- a/bin/server/cmd/root.go+++ b/bin/server/cmd/root.go@@ -160,9 +160,8 @@ type main struct {}func preStart() error {- if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {- error := fmt.Errorf("--host %s must start with http:// or https://", host)- return error+ if !common.HasPrefixes(host, "http://", "https://") {+ return fmt.Errorf("--host %s must start with http:// or https://", host)}// Convert to absolute path if relative path is supplied.diff --git a/common/util.go b/common/util.goindex 4b0ed25387..ec453ae34c 100644--- a/common/util.go+++ b/common/util.go@@ -27,3 +27,13 @@ func RandomString(n int) string {}return sb.String()}++// HasPrefixes returns true if the string s has any of the given prefixes.+func HasPrefixes(src string, prefixes ...string) bool {+ for _, prefix := range prefixes {+ if strings.HasPrefix(src, prefix) {+ return true+ }+ }+ return false+}diff --git a/common/util_test.go b/common/util_test.gonew file mode 100644index 0000000000..9eec344661--- /dev/null+++ b/common/util_test.go@@ -0,0 +1,39 @@+package common++import "testing"++func TestHasPrefixes(t *testing.T) {+ type args struct {+ src string+ prefixes []string+ }+ tests := []struct {+ name string+ args args+ want bool+ }{+ {+ name: "has prefixes",+ args: args{+ src: "abc",+ prefixes: []string{"a", "b", "c"},+ },+ want: true,+ },+ {+ name: "has no matching prefix",+ args: args{+ src: "this is a sentence",+ prefixes: []string{"that", "x", "y"},+ },+ want: false,+ },+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := HasPrefixes(tt.args.src, tt.args.prefixes...); got != tt.want {+ t.Fatalf("HasPrefixes() = %v, want %v", got, tt.want)+ }+ })+ }+}diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 45cf5e619d..6dcbfaa1b8 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -10,6 +10,8 @@ import ("strings""time"+ "github.com/bytebase/bytebase/common"+// embed will embeds the migration schema._ "embed"@@ -1225,10 +1227,7 @@ func getPgSchemas(txn *sql.Tx) ([]*pgSchema, error) {// pgSystemSchema returns whether the schema is a system or user defined schema.func pgSystemSchema(s string) bool {- if strings.HasPrefix(s, "pg_toast") {- return true- }- if strings.HasPrefix(s, "pg_temp") {+ if common.HasPrefixes(s, "pg_toast", "pg_temp") {return true}switch s {diff --git a/server/acl.go b/server/acl.goindex 243f5cf737..e61687a913 100644--- a/server/acl.go+++ b/server/acl.go@@ -27,7 +27,7 @@ func aclMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.Handreturn func(c echo.Context) error {ctx := context.Background()// Skips auth, actuator, plan- if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") || strings.HasPrefix(c.Path(), "/api/plan") {+ if common.HasPrefixes(c.Path(), "/api/auth", "/api/actuator", "/api/plan") {return next(c)}diff --git a/server/jwt.go b/server/jwt.goindex ee0c0a4ee7..aa4c391500 100644--- a/server/jwt.go+++ b/server/jwt.go@@ -6,7 +6,6 @@ import ("fmt""net/http""strconv"- "strings""time""github.com/bytebase/bytebase/api"@@ -168,7 +167,7 @@ func removeUserCookie(c echo.Context) {func JWTMiddleware(l *zap.Logger, p api.PrincipalService, next echo.HandlerFunc, mode string, secret string) echo.HandlerFunc {return func(c echo.Context) error {// Skips auth, actuator, plan- if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") || strings.HasPrefix(c.Path(), "/api/plan") {+ if common.HasPrefixes(c.Path(), "/api/auth", "/api/actuator", "/api/plan") {return next(c)}diff --git a/server/server.go b/server/server.goindex bc313753a1..a2a2b1186e 100644--- a/server/server.go+++ b/server/server.go@@ -7,6 +7,8 @@ import ("strings""time"+ "github.com/bytebase/bytebase/common"+// embed will embeds the acl policy._ "embed"@@ -174,7 +176,7 @@ func NewServer(logger *zap.Logger, version string, host string, port int, fronteif mode == "dev" || debug {e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{Skipper: func(c echo.Context) bool {- return !strings.HasPrefix(c.Path(), "/api") && !strings.HasPrefix(c.Path(), "/hook")+ return !common.HasPrefixes(c.Path(), "/api", "/hook")},Format: `{"time":"${time_rfc3339}",` +`"method":"${method}","uri":"${uri}",` +