From 13c4eb04a39426ca56ec956635769d0952a0762a Mon Sep 17 00:00:00 2001 From: Jacky Date: Mon, 6 May 2024 21:45:16 +0800 Subject: [PATCH] feat: add env to skip installation #357 --- api/system/install.go | 2 +- internal/kernal/boot.go | 10 ++++++++++ settings/server.go | 1 + settings/settings_test.go | 2 ++ settings/user.go | 8 ++++++++ 5 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 settings/user.go diff --git a/api/system/install.go b/api/system/install.go index d9729015..9130ef3f 100644 --- a/api/system/install.go +++ b/api/system/install.go @@ -13,7 +13,7 @@ import ( ) func installLockStatus() bool { - return "" != settings.ServerSettings.JwtSecret + return settings.ServerSettings.SkipInstallation || "" != settings.ServerSettings.JwtSecret } func InstallLockCheck(c *gin.Context) { diff --git a/internal/kernal/boot.go b/internal/kernal/boot.go index dba91d07..5f331188 100644 --- a/internal/kernal/boot.go +++ b/internal/kernal/boot.go @@ -60,6 +60,16 @@ func recovery() { } func InitDatabase() { + + // Skip installation + if settings.ServerSettings.SkipInstallation && settings.ServerSettings.JwtSecret == "" { + settings.ServerSettings.JwtSecret = uuid.New().String() + err := settings.Save() + if err != nil { + logger.Error(err) + } + } + if "" != settings.ServerSettings.JwtSecret { db := model.Init() query.Init(db) diff --git a/settings/server.go b/settings/server.go index a31effb1..5c827f33 100644 --- a/settings/server.go +++ b/settings/server.go @@ -20,6 +20,7 @@ type Server struct { GithubProxy string `json:"github_proxy" binding:"omitempty,url"` CertRenewalInterval int `json:"cert_renewal_interval" binding:"min=7,max=21"` RecursiveNameservers []string `json:"recursive_nameservers" binding:"omitempty,dive,hostname_port"` + SkipInstallation bool `json:"skip_installation"` } func (s *Server) GetCADir() string { diff --git a/settings/settings_test.go b/settings/settings_test.go index bb2d73e2..b50f3730 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -25,6 +25,7 @@ func TestSetup(t *testing.T) { _ = os.Setenv("NGINX_UI_SERVER_HTTP_HOST", "127.0.0.1") _ = os.Setenv("NGINX_UI_SERVER_CERT_RENEWAL_INTERVAL", "14") _ = os.Setenv("NGINX_UI_SERVER_RECURSIVE_NAMESERVERS", "8.8.8.8") + _ = os.Setenv("NGINX_UI_SERVER_SKIP_INSTALLATION", "true") _ = os.Setenv("NGINX_UI_NGINX_ACCESS_LOG_PATH", "/tmp/nginx/access.log") _ = os.Setenv("NGINX_UI_NGINX_ERROR_LOG_PATH", "/tmp/nginx/error.log") @@ -67,6 +68,7 @@ func TestSetup(t *testing.T) { assert.Equal(t, "127.0.0.1", ServerSettings.HttpHost) assert.Equal(t, 14, ServerSettings.CertRenewalInterval) assert.Equal(t, []string{"8.8.8.8"}, ServerSettings.RecursiveNameservers) + assert.Equal(t, true, ServerSettings.SkipInstallation) assert.Equal(t, "/tmp/nginx/access.log", NginxSettings.AccessLogPath) assert.Equal(t, "/tmp/nginx/error.log", NginxSettings.ErrorLogPath) diff --git a/settings/user.go b/settings/user.go new file mode 100644 index 00000000..1cf99137 --- /dev/null +++ b/settings/user.go @@ -0,0 +1,8 @@ +package settings + +type PredefinedUser struct { + User string `json:"user"` + Password string `json:"password"` +} + +var PredefinedUserSettings = &PredefinedUser{}