fix: models api return models in deactivate channels

- Enhance logging functionality by adding context support and improving debugging options.
- Standardize function naming conventions across middleware to ensure consistency.
- Optimize data retrieval and handling in the model controller, including caching and error management.
- Simplify the bug report template to streamline the issue reporting process.
This commit is contained in:
Laisky.Cai
2025-02-26 11:22:03 +00:00
parent f5d4ff05dc
commit 5905a7f295
8 changed files with 119 additions and 61 deletions

View File

@@ -102,34 +102,34 @@ func TokenAuth() func(c *gin.Context) {
key = parts[0]
token, err := model.ValidateUserToken(key)
if err != nil {
abortWithError(c, http.StatusUnauthorized, err)
AbortWithError(c, http.StatusUnauthorized, err)
return
}
if token.Subnet != nil && *token.Subnet != "" {
if !network.IsIpInSubnets(ctx, c.ClientIP(), *token.Subnet) {
abortWithError(c, http.StatusForbidden, errors.Errorf("This API key can only be used in the specified subnet: %s, current IP: %s", *token.Subnet, c.ClientIP()))
AbortWithError(c, http.StatusForbidden, errors.Errorf("This API key can only be used in the specified subnet: %s, current IP: %s", *token.Subnet, c.ClientIP()))
return
}
}
userEnabled, err := model.CacheIsUserEnabled(token.UserId)
if err != nil {
abortWithError(c, http.StatusInternalServerError, err)
AbortWithError(c, http.StatusInternalServerError, err)
return
}
if !userEnabled || blacklist.IsUserBanned(token.UserId) {
abortWithError(c, http.StatusForbidden, errors.New("User has been banned"))
AbortWithError(c, http.StatusForbidden, errors.New("User has been banned"))
return
}
requestModel, err := getRequestModel(c)
if err != nil && shouldCheckModel(c) {
abortWithError(c, http.StatusBadRequest, err)
AbortWithError(c, http.StatusBadRequest, err)
return
}
c.Set(ctxkey.RequestModel, requestModel)
if token.Models != nil && *token.Models != "" {
c.Set(ctxkey.AvailableModels, *token.Models)
if requestModel != "" && !isModelInList(requestModel, *token.Models) {
abortWithError(c, http.StatusForbidden, errors.Errorf("This API key does not have permission to use the model: %s", requestModel))
AbortWithError(c, http.StatusForbidden, errors.Errorf("This API key does not have permission to use the model: %s", requestModel))
return
}
}
@@ -144,7 +144,7 @@ func TokenAuth() func(c *gin.Context) {
if model.IsAdmin(token.UserId) {
c.Set(ctxkey.SpecificChannelId, parts[1])
} else {
abortWithError(c, http.StatusForbidden, errors.New("Ordinary users do not support specifying channels"))
AbortWithError(c, http.StatusForbidden, errors.New("Ordinary users do not support specifying channels"))
return
}
}

View File

@@ -32,16 +32,16 @@ func Distribute() func(c *gin.Context) {
if ok {
id, err := strconv.Atoi(channelId.(string))
if err != nil {
abortWithError(c, http.StatusBadRequest, errors.New("Invalid Channel Id"))
AbortWithError(c, http.StatusBadRequest, errors.New("Invalid Channel Id"))
return
}
channel, err = model.GetChannelById(id, true)
if err != nil {
abortWithError(c, http.StatusBadRequest, errors.New("Invalid Channel Id"))
AbortWithError(c, http.StatusBadRequest, errors.New("Invalid Channel Id"))
return
}
if channel.Status != model.ChannelStatusEnabled {
abortWithError(c, http.StatusForbidden, errors.New("The channel has been disabled"))
AbortWithError(c, http.StatusForbidden, errors.New("The channel has been disabled"))
return
}
} else {
@@ -54,7 +54,7 @@ func Distribute() func(c *gin.Context) {
logger.SysError(fmt.Sprintf("Channel does not exist: %d", channel.Id))
message = "Database consistency has been broken, please contact the administrator"
}
abortWithError(c, http.StatusServiceUnavailable, errors.New(message))
AbortWithError(c, http.StatusServiceUnavailable, errors.New(message))
return
}
}

View File

@@ -23,7 +23,8 @@ func abortWithMessage(c *gin.Context, statusCode int, message string) {
logger.Error(c.Request.Context(), message)
}
func abortWithError(c *gin.Context, statusCode int, err error) {
// AbortWithError aborts the request with an error message
func AbortWithError(c *gin.Context, statusCode int, err error) {
logger := gmw.GetLogger(c)
logger.Error("server abort", zap.Error(err))
c.JSON(statusCode, gin.H{