Files
1Panel/core/cmd/server/cmd/reset.go
T
KOMATA 5209275f28 feat: Implement caching for settings retrieval and update logic (#11886)
* feat: Implement caching for settings retrieval and update logic

- Introduced a caching mechanism for settings using go-cache to improve performance.
- Updated the Create, Update, and UpdateOrCreate methods to cache values after database operations.
- Modified the Get and GetValueByKey methods to utilize the cache for faster access.
- Adjusted middleware to use the new GetValueByKey method for retrieving settings, enhancing code consistency.

* feat: Enhance setting cache management with dynamic TTL

- Introduced a function to determine cache TTL based on setting keys, allowing critical settings to have shorter cache durations.
- Updated cache logic in Create, Update, Get, and GetValueByKey methods to utilize the new TTL management, improving performance and consistency in settings retrieval.

* refactor: Simplify setting cache TTL management

- Removed the dynamic TTL function and standardized the cache TTL to a fixed duration for all settings.
- Updated cache logic in Create, Update, Get, and GetValueByKey methods to use the new fixed TTL, enhancing code clarity and maintainability.
- Added a new function to restart the core service after resetting settings, improving the reset command's functionality.

* refactor: Remove core restart functionality from reset commands

- Eliminated the restartCoreAfterReset function to simplify the reset command logic.
- Updated reset commands to return nil after setting changes, enhancing clarity and reducing unnecessary complexity.
2026-02-25 16:40:20 +08:00

157 lines
4.1 KiB
Go

package cmd
import (
"fmt"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/i18n"
"github.com/1Panel-dev/1Panel/core/utils/passkey"
"github.com/spf13/cobra"
)
func init() {
resetCmd.SetHelpFunc(func(c *cobra.Command, s []string) {
i18n.UseI18nForCmd(language)
loadResetHelper()
})
RootCmd.AddCommand(resetCmd)
resetCmd.AddCommand(resetMFACmd)
resetCmd.AddCommand(resetSSLCmd)
resetCmd.AddCommand(resetEntranceCmd)
resetCmd.AddCommand(resetBindIpsCmd)
resetCmd.AddCommand(resetDomainCmd)
resetCmd.AddCommand(resetPasskeyCmd)
}
var resetCmd = &cobra.Command{
Use: "reset",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
loadResetHelper()
return nil
},
}
var resetMFACmd = &cobra.Command{
Use: "mfa",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
if !isRoot() {
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset mfa"}))
return nil
}
db, err := loadDBConn("core.db")
if err != nil {
return err
}
return setSettingByKey(db, "MFAStatus", constant.StatusDisable)
},
}
var resetSSLCmd = &cobra.Command{
Use: "https",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
if !isRoot() {
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset https"}))
return nil
}
db, err := loadDBConn("core.db")
if err != nil {
return err
}
if err := setSettingByKey(db, "SSL", constant.StatusDisable); err != nil {
return err
}
return nil
},
}
var resetEntranceCmd = &cobra.Command{
Use: "entrance",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
if !isRoot() {
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset entrance"}))
return nil
}
db, err := loadDBConn("core.db")
if err != nil {
return err
}
return setSettingByKey(db, "SecurityEntrance", "")
},
}
var resetBindIpsCmd = &cobra.Command{
Use: "ips",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
if !isRoot() {
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset ips"}))
return nil
}
db, err := loadDBConn("core.db")
if err != nil {
return err
}
if err := setSettingByKey(db, "AllowIPs", ""); err != nil {
return err
}
return nil
},
}
var resetDomainCmd = &cobra.Command{
Use: "domain",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
if !isRoot() {
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset domain"}))
return nil
}
db, err := loadDBConn("core.db")
if err != nil {
return err
}
if err := setSettingByKey(db, "BindDomain", ""); err != nil {
return err
}
return nil
},
}
var resetPasskeyCmd = &cobra.Command{
Use: "passkey",
RunE: func(cmd *cobra.Command, args []string) error {
i18n.UseI18nForCmd(language)
if !isRoot() {
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset passkey"}))
return nil
}
db, err := loadDBConn("core.db")
if err != nil {
return err
}
if err := setSettingByKey(db, passkey.PasskeyUserIDSettingKey, ""); err != nil {
return err
}
return setSettingByKey(db, passkey.PasskeyCredentialSettingKey, "")
},
}
func loadResetHelper() {
fmt.Println(i18n.GetMsgByKeyForCmd("ResetCommands"))
fmt.Println("\nUsage:\n 1panel reset [command]\n\nAvailable Commands:")
fmt.Println("\n domain " + i18n.GetMsgByKeyForCmd("ResetDomain"))
fmt.Println(" entrance " + i18n.GetMsgByKeyForCmd("ResetEntrance"))
fmt.Println(" https " + i18n.GetMsgByKeyForCmd("ResetHttps"))
fmt.Println(" ips " + i18n.GetMsgByKeyForCmd("ResetIPs"))
fmt.Println(" mfa " + i18n.GetMsgByKeyForCmd("ResetMFA"))
fmt.Println(" passkey " + i18n.GetMsgByKeyForCmd("ResetPasskey"))
fmt.Println("\nFlags:\n -h, --help help for reset")
fmt.Println("\nUse \"1panel reset [command] --help\" for more information about a command.")
}