Go: add context, part10 (#17417)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-07-27 16:14:23 +08:00
committed by GitHub
parent cd846cc9d4
commit 49e6181eca
10 changed files with 94 additions and 79 deletions

View File

@@ -599,10 +599,11 @@ func (h *Handler) RestartService(c *gin.Context) {
// ListVariables handle list variables
func (h *Handler) ListVariables(c *gin.Context) {
ctx := c.Request.Context()
// Check if request has body content
if c.Request.ContentLength == 0 || c.Request.ContentLength == -1 {
// List all variables
variables, err := h.service.ListAllVariables()
variables, err := h.service.ListAllVariables(ctx)
if err != nil {
common.ErrorWithCode(c, common.CodeServerError, err.Error())
return
@@ -625,7 +626,7 @@ func (h *Handler) ListVariables(c *gin.Context) {
return
}
variable, err := h.service.GetVariable(req.VarName)
variable, err := h.service.GetVariable(ctx, req.VarName)
if err != nil {
common.ErrorWithCode(c, common.CodeServerError, err.Error())
return
@@ -647,7 +648,8 @@ func (h *Handler) ShowVariable(c *gin.Context) {
return
}
variable, err := h.service.GetVariable(varName)
ctx := c.Request.Context()
variable, err := h.service.GetVariable(ctx, varName)
if err != nil {
common.ErrorWithCode(c, common.CodeServerError, err.Error())
return
@@ -681,7 +683,8 @@ func (h *Handler) SetVariable(c *gin.Context) {
return
}
if err := h.service.SetVariable(req.VarName, req.VarValue); err != nil {
ctx := c.Request.Context()
if err := h.service.SetVariable(ctx, req.VarName, req.VarValue); err != nil {
common.ErrorWithCode(c, common.CodeServerError, err.Error())
return
}

View File

@@ -1458,14 +1458,14 @@ func NewAdminException(message string) *AdminException {
// GetVariable get variable by name
// Returns the exact system setting with the given name, or settings matching the
// given name prefix when an exact setting does not exist.
func (s *Service) GetVariable(varName string) ([]map[string]interface{}, error) {
settings, err := s.systemSettingsDAO.GetByName(varName)
func (s *Service) GetVariable(ctx context.Context, varName string) ([]map[string]interface{}, error) {
settings, err := s.systemSettingsDAO.GetByName(ctx, dao.DB, varName)
if err != nil {
return nil, err
}
if len(settings) == 0 {
settings, err = s.systemSettingsDAO.GetByNamePrefix(varName)
settings, err = s.systemSettingsDAO.GetByNamePrefix(ctx, dao.DB, varName)
if err != nil {
return nil, err
}
@@ -1478,8 +1478,8 @@ func (s *Service) GetVariable(varName string) ([]map[string]interface{}, error)
// ListAllVariables list all variables
// Returns all system settings from database
func (s *Service) ListAllVariables() ([]map[string]interface{}, error) {
settings, err := s.systemSettingsDAO.GetAll()
func (s *Service) ListAllVariables(ctx context.Context) ([]map[string]interface{}, error) {
settings, err := s.systemSettingsDAO.GetAll(ctx, dao.DB)
if err != nil {
return nil, err
}
@@ -1490,8 +1490,8 @@ func (s *Service) ListAllVariables() ([]map[string]interface{}, error) {
// SetVariable set variable
// Creates or updates a system setting
// If the setting exists, updates it; otherwise creates a new one
func (s *Service) SetVariable(varName, varValue string) error {
settings, err := s.systemSettingsDAO.GetByName(varName)
func (s *Service) SetVariable(ctx context.Context, varName, varValue string) error {
settings, err := s.systemSettingsDAO.GetByName(ctx, dao.DB, varName)
if err != nil {
return err
}
@@ -1502,7 +1502,7 @@ func (s *Service) SetVariable(varName, varValue string) error {
return err
}
setting.Value = varValue
return s.systemSettingsDAO.UpdateByName(varName, setting)
return s.systemSettingsDAO.UpdateByName(ctx, dao.DB, varName, setting)
} else if len(settings) > 1 {
return NewAdminException("Can't update more than 1 setting: " + varName)
}
@@ -1517,7 +1517,7 @@ func (s *Service) SetVariable(varName, varValue string) error {
if err = common.ValidateSystemSettingValue(*newSetting, varValue); err != nil {
return err
}
return s.systemSettingsDAO.Create(newSetting)
return s.systemSettingsDAO.Create(ctx, dao.DB, newSetting)
}
// Config methods