浏览代码

perf: replace multiple if statements with switch

JustSong 2 年之前
父节点
当前提交
c67e639f26
共有 2 个文件被更改,包括 25 次插入23 次删除
  1. 25 18
      controller/option.go
  2. 0 5
      model/option.go

+ 25 - 18
controller/option.go

@@ -40,24 +40,31 @@ func UpdateOption(c *gin.Context) {
 		})
 		return
 	}
-	if option.Key == "GitHubOAuthEnabled" && option.Value == "true" && common.GitHubClientId == "" {
-		c.JSON(http.StatusOK, gin.H{
-			"success": false,
-			"message": "无法启用 GitHub OAuth,请先填入 GitHub Client ID 以及 GitHub Client Secret!",
-		})
-		return
-	} else if option.Key == "WeChatAuthEnabled" && option.Value == "true" && common.WeChatServerAddress == "" {
-		c.JSON(http.StatusOK, gin.H{
-			"success": false,
-			"message": "无法启用微信登录,请先填入微信登录相关配置信息!",
-		})
-		return
-	} else if option.Key == "TurnstileCheckEnabled" && option.Value == "true" && common.TurnstileSiteKey == "" {
-		c.JSON(http.StatusOK, gin.H{
-			"success": false,
-			"message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
-		})
-		return
+	switch option.Key {
+	case "GitHubOAuthEnabled":
+		if option.Value == "true" && common.GitHubClientId == "" {
+			c.JSON(http.StatusOK, gin.H{
+				"success": false,
+				"message": "无法启用 GitHub OAuth,请先填入 GitHub Client ID 以及 GitHub Client Secret!",
+			})
+			return
+		}
+	case "WeChatAuthEnabled":
+		if option.Value == "true" && common.WeChatServerAddress == "" {
+			c.JSON(http.StatusOK, gin.H{
+				"success": false,
+				"message": "无法启用微信登录,请先填入微信登录相关配置信息!",
+			})
+			return
+		}
+	case "TurnstileCheckEnabled":
+		if option.Value == "true" && common.TurnstileSiteKey == "" {
+			c.JSON(http.StatusOK, gin.H{
+				"success": false,
+				"message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
+			})
+			return
+		}
 	}
 	err = model.UpdateOption(option.Key, option.Value)
 	if err != nil {

+ 0 - 5
model/option.go

@@ -1,7 +1,6 @@
 package model
 
 import (
-	"errors"
 	"message-pusher/common"
 	"strconv"
 	"strings"
@@ -55,10 +54,6 @@ func InitOptionMap() {
 }
 
 func UpdateOption(key string, value string) error {
-	if key == "StatEnabled" && value == "true" && !common.RedisEnabled {
-		return errors.New("未启用 Redis,无法启用统计功能")
-	}
-
 	// Save to database first
 	option := Option{
 		Key:   key,