mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-17 10:18:04 +00:00
Dabase setup widget
This commit is contained in:
1 parent
853ef909a7
commit
741c9c982f
25 files changed
+1133
-66
No files matched your search
@@ -13,6 +13,7 @@ type healthCheckResponse struct {
|
||||
Commit string `json:"commit"`
|
||||
AcmeShVersion string `json:"acme.sh"`
|
||||
Healthy bool `json:"healthy"`
|
||||
IsDBSetup bool `json:"db_setup"`
|
||||
IsSetup bool `json:"setup"`
|
||||
}
|
||||
|
||||
@@ -24,6 +25,7 @@ func Health() func(http.ResponseWriter, *http.Request) {
|
||||
Version: config.Version,
|
||||
Commit: config.Commit,
|
||||
Healthy: true,
|
||||
IsDBSetup: config.IsDBSetup,
|
||||
IsSetup: config.IsSetup,
|
||||
AcmeShVersion: acme.GetAcmeShVersion(),
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ func replaceIncomingSchemas(swaggerSchema []byte) []byte {
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateCertificate}}"`, schema.CreateCertificate())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateCertificate}}"`, schema.UpdateCertificate(""))
|
||||
|
||||
str = strings.ReplaceAll(str, `"{{schema.SetupDatabase}}"`, schema.SetupDatabase())
|
||||
|
||||
str = strings.ReplaceAll(str, `"{{schema.CreateSetting}}"`, schema.CreateSetting())
|
||||
str = strings.ReplaceAll(str, `"{{schema.UpdateSetting}}"`, schema.UpdateSetting())
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/config"
|
||||
"npm/internal/database"
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
// SetupDatabase tests the connection parameters provided by the setup
|
||||
// wizard, saves them to the db.conf file and then calls onConfigured to
|
||||
// connect, migrate and finish starting the application.
|
||||
// Route: POST /setup/database
|
||||
func SetupDatabase(onConfigured func() error) func(http.ResponseWriter, *http.Request) {
|
||||
// Serialises submissions so that two browsers can't race to
|
||||
// configure different databases
|
||||
var mu sync.Mutex
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if config.IsDBSetup {
|
||||
h.ResultErrorJSON(w, r, http.StatusForbidden, "Database is already configured", nil)
|
||||
return
|
||||
}
|
||||
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
var newCfg config.DBConfig
|
||||
if err := json.Unmarshal(bodyBytes, &newCfg); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Sqlite lives in the data folder and needs nothing else
|
||||
if newCfg.GetDriver() == config.DatabaseSqlite {
|
||||
newCfg = config.DBConfig{Driver: config.DatabaseSqlite}
|
||||
} else if newCfg.SSLMode == "" {
|
||||
newCfg.SSLMode = "disable"
|
||||
}
|
||||
|
||||
if err := database.TestConnection(newCfg); err != nil {
|
||||
logger.Warn("Database setup connection test failed: %s", err.Error())
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := config.SaveDBConfig(newCfg); err != nil {
|
||||
logger.Error("DatabaseConfigSaveError", err)
|
||||
h.ResultErrorJSON(w, r, http.StatusInternalServerError, "Unable to save database configuration", nil)
|
||||
return
|
||||
}
|
||||
|
||||
prevCfg := config.Configuration.DB
|
||||
config.Configuration.DB = newCfg
|
||||
logger.Info("Database configuration saved to %s", config.GetDBConfigFile())
|
||||
|
||||
if err := onConfigured(); err != nil {
|
||||
logger.Error("DatabaseSetupError", err)
|
||||
// Roll back so the wizard can be attempted again
|
||||
database.Close()
|
||||
config.Configuration.DB = prevCfg
|
||||
if rmErr := config.RemoveDBConfig(); rmErr != nil {
|
||||
logger.Error("DatabaseConfigRemoveError", rmErr)
|
||||
}
|
||||
h.ResultErrorJSON(w, r, http.StatusInternalServerError, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, true)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user