Skip to content

Commit

Permalink
refactor(Lint): 接入golangci-lint, 采用较强配置, 清理旧代码 (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangze-Li authored Oct 18, 2023
1 parent 941feab commit 958a9cd
Show file tree
Hide file tree
Showing 102 changed files with 5,694 additions and 6,015 deletions.
349 changes: 349 additions & 0 deletions .golangci.yml

Large diffs are not rendered by default.

59 changes: 24 additions & 35 deletions api/api_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/labstack/echo/v4"
)

const CODE_ALREADY_EXISTS = 602
const CodeAlreadyExists = 602

var startTime = time.Now().Unix()

Expand All @@ -24,12 +24,6 @@ func baseInfo(c echo.Context) error {

var m runtime.MemStats
runtime.ReadMemStats(&m)
//fmt.Println("!!!!", m.Alloc-m.Frees, m.HeapReleased, m.HeapInuse)
//
//const meg = 1024 * 1024
//fmt.Printf("env: %v, sys: %4d MB, alloc: %4d MB, idel: %4d MB, released: %4d MB, inuse: %4d MB stack:%d\n",
// os.Getenv("GODEBUG"), m.HeapSys/meg, m.HeapAlloc/meg, m.HeapIdle/meg, m.HeapReleased/meg, m.HeapInuse/meg,
// m.StackSys/meg)

var versionNew string
var versionNewNote string
Expand All @@ -40,7 +34,6 @@ func baseInfo(c echo.Context) error {
versionNewCode = dm.AppVersionOnline.VersionLatestCode
}

extraTitle := ""
getName := func() string {
defer func() {
// 防止报错
Expand All @@ -50,8 +43,7 @@ func baseInfo(c echo.Context) error {
ctx := &dice.MsgContext{Dice: myDice, EndPoint: nil, Session: myDice.ImSession}
return dice.DiceFormatTmpl(ctx, "核心:骰子名字")
}

extraTitle = getName()
extraTitle := getName()

return c.JSON(http.StatusOK, struct {
AppName string `json:"appName"`
Expand Down Expand Up @@ -87,9 +79,6 @@ func hello2(c echo.Context) error {
return c.JSON(http.StatusForbidden, nil)
}

//dice.CmdRegister("aaa", "bb");
//a := dice.CmdList();
//b, _ := json.Marshal(a)
return c.JSON(http.StatusOK, nil)
}

Expand Down Expand Up @@ -152,7 +141,7 @@ func logFetchAndClear(c echo.Context) error {
return c.JSON(http.StatusForbidden, nil)
}
info := c.JSON(http.StatusOK, myDice.LogWriter.Items)
//myDice.LogWriter.Items = myDice.LogWriter.Items[:0]
// myDice.LogWriter.Items = myDice.LogWriter.Items[:0]
return info
}

Expand All @@ -164,7 +153,7 @@ func DiceExec(c echo.Context) error {
}

v := struct {
Id string `form:"id" json:"id"`
ID string `form:"id" json:"id"`
Message string `form:"message"`
}{}
err := c.Bind(&v)
Expand All @@ -184,30 +173,30 @@ func DiceExec(c echo.Context) error {
}
lastExecTime = now

//pa := dice.PlatformAdapterHttp{
// RecentMessage: []dice.HttpSimpleMessage{},
//}
//tmpEp := &dice.EndPointInfo{
// EndPointInfoBase: dice.EndPointInfoBase{
// Id: "1",
// Nickname: "海豹核心",
// State: 2,
// UserId: "UI:1000",
// Platform: "UI",
// Enable: true,
// },
// Adapter: &pa,
//}
// pa := dice.PlatformAdapterHttp{
// RecentMessage: []dice.HttpSimpleMessage{},
// }
// tmpEp := &dice.EndPointInfo{
// EndPointInfoBase: dice.EndPointInfoBase{
// Id: "1",
// Nickname: "海豹核心",
// State: 2,
// UserId: "UI:1000",
// Platform: "UI",
// Enable: true,
// },
// Adapter: &pa,
// }
msg := &dice.Message{
MessageType: "private",
Message: v.Message,
Platform: "UI",
Sender: dice.SenderBase{
Nickname: "User",
UserId: "UI:1001",
UserID: "UI:1001",
},
}
//pa := myDice.UIEndpoint.Adapter.(*dice.PlatformAdapterHttp)
// pa := myDice.UIEndpoint.Adapter.(*dice.PlatformAdapterHttp)
myDice.ImSession.Execute(myDice.UIEndpoint, msg, false)
return c.JSON(200, "ok")
}
Expand All @@ -216,9 +205,9 @@ func DiceRecentMessage(c echo.Context) error {
if !doAuth(c) {
return c.JSON(http.StatusForbidden, nil)
}
pa := myDice.UIEndpoint.Adapter.(*dice.PlatformAdapterHttp)
pa := myDice.UIEndpoint.Adapter.(*dice.PlatformAdapterHTTP)
defer func() {
pa.RecentMessage = []dice.HttpSimpleMessage{}
pa.RecentMessage = []dice.HTTPSimpleMessage{}
}()
return c.JSON(200, pa.RecentMessage)
}
Expand Down Expand Up @@ -401,10 +390,10 @@ func Bind(e *echo.Echo, _myDice *dice.DiceManager) {
e.GET(prefix+"/banconfig/list", banMapList)
e.GET(prefix+"/banconfig/get", banConfigGet)
e.POST(prefix+"/banconfig/set", banConfigSet)
//e.GET(prefix+"/banconfig/map_get", banMapGet)
// e.GET(prefix+"/banconfig/map_get", banMapGet)
e.POST(prefix+"/banconfig/map_delete_one", banMapDeleteOne)
e.POST(prefix+"/banconfig/map_add_one", banMapAddOne)
//e.POST(prefix+"/banconfig/map_set", banMapSet)
// e.POST(prefix+"/banconfig/map_set", banMapSet)
e.GET(prefix+"/banconfig/export", banExport)
e.POST(prefix+"/banconfig/import", banImport)

Expand Down
62 changes: 31 additions & 31 deletions api/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func backupDownload(c echo.Context) error {
return c.JSON(http.StatusOK, nil)
}

func backupDelete(c echo.Context) error { //nolint
func backupDelete(c echo.Context) error {
if !doAuth(c) {
return c.JSON(http.StatusForbidden, nil)
}
Expand All @@ -86,7 +86,7 @@ func backupDelete(c echo.Context) error { //nolint
})
}

func backupBatchDelete(c echo.Context) error { //nolint
func backupBatchDelete(c echo.Context) error {
if !doAuth(c) {
return c.JSON(http.StatusForbidden, nil)
}
Expand Down Expand Up @@ -114,11 +114,10 @@ func backupBatchDelete(c echo.Context) error { //nolint

if len(fails) == 0 {
return Success(&c, Response{})
} else {
return Error(&c, "失败列表", Response{
"fails": fails,
})
}
return Error(&c, "失败列表", Response{
"fails": fails,
})
}

// 快速备份
Expand Down Expand Up @@ -173,33 +172,34 @@ func backupConfigSave(c echo.Context) error {

v := backupConfig{}
err := c.Bind(&v)
if err == nil {
dm.AutoBackupEnable = v.AutoBackupEnable
dm.AutoBackupTime = v.AutoBackupTime

if int(dice.BackupCleanStrategyDisabled) <= v.BackupCleanStrategy && v.BackupCleanStrategy <= int(dice.BackupCleanStrategyByTime) {
dm.BackupCleanStrategy = dice.BackupCleanStrategy(v.BackupCleanStrategy)
if dm.BackupCleanStrategy == dice.BackupCleanStrategyByCount && v.BackupCleanKeepCount > 0 {
dm.BackupCleanKeepCount = v.BackupCleanKeepCount
}
if dm.BackupCleanStrategy == dice.BackupCleanStrategyByTime && len(v.BackupCleanKeepDur) > 0 {
if dur, err := time.ParseDuration(v.BackupCleanKeepDur); err == nil {
dm.BackupCleanKeepDur = dur
} else {
myDice.Logger.Errorf("设定的自动清理保留时间有误: %q %v", v.BackupCleanKeepDur, err)
}
if err != nil {
return c.String(430, "")
}

dm.AutoBackupEnable = v.AutoBackupEnable
dm.AutoBackupTime = v.AutoBackupTime

if int(dice.BackupCleanStrategyDisabled) <= v.BackupCleanStrategy && v.BackupCleanStrategy <= int(dice.BackupCleanStrategyByTime) {
dm.BackupCleanStrategy = dice.BackupCleanStrategy(v.BackupCleanStrategy)
if dm.BackupCleanStrategy == dice.BackupCleanStrategyByCount && v.BackupCleanKeepCount > 0 {
dm.BackupCleanKeepCount = v.BackupCleanKeepCount
}
if dm.BackupCleanStrategy == dice.BackupCleanStrategyByTime && len(v.BackupCleanKeepDur) > 0 {
if dur, err := time.ParseDuration(v.BackupCleanKeepDur); err == nil {
dm.BackupCleanKeepDur = dur
} else {
myDice.Logger.Errorf("设定的自动清理保留时间有误: %q %v", v.BackupCleanKeepDur, err)
}
if v.BackupCleanTrigger > 0 {
dm.BackupCleanTrigger = dice.BackupCleanTrigger(v.BackupCleanTrigger)
if dm.BackupCleanTrigger&dice.BackupCleanTriggerCron > 0 {
dm.BackupCleanCron = v.BackupCleanCron
}
}
if v.BackupCleanTrigger > 0 {
dm.BackupCleanTrigger = dice.BackupCleanTrigger(v.BackupCleanTrigger)
if dm.BackupCleanTrigger&dice.BackupCleanTriggerCron > 0 {
dm.BackupCleanCron = v.BackupCleanCron
}
}

dm.ResetAutoBackup()
dm.ResetBackupClean()
return c.String(http.StatusOK, "")
}
return c.String(430, "")

dm.ResetAutoBackup()
dm.ResetBackupClean()
return c.String(http.StatusOK, "")
}
18 changes: 9 additions & 9 deletions api/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package api
import (
"bufio"
"encoding/json"
"github.com/labstack/echo/v4"
"io"
"mime/multipart"
"net/http"
"os"
"sealdice-core/dice"
"strings"
"time"

"github.com/labstack/echo/v4"
)

func banConfigGet(c echo.Context) error {
Expand Down Expand Up @@ -72,7 +73,7 @@ func banMapDeleteOne(c echo.Context) error {
if err != nil {
return c.String(430, err.Error())
}
myDice.BanList.DeleteById(myDice, v.ID)
myDice.BanList.DeleteByID(myDice, v.ID)
return c.JSON(http.StatusOK, nil)
}

Expand Down Expand Up @@ -109,14 +110,14 @@ func banMapAddOne(c echo.Context) error {
}
}
if v.Rank == dice.BanRankTrusted {
myDice.BanList.SetTrustById(v.ID, "海豹后台", "骰主后台设置")
myDice.BanList.SetTrustByID(v.ID, "海豹后台", "骰主后台设置")
}

return c.JSON(http.StatusOK, nil)
}

//
//func banMapSet(c echo.Context) error {
// func banMapSet(c echo.Context) error {
// if !doAuth(c) {
// return c.JSON(http.StatusForbidden, nil)
// }
Expand Down Expand Up @@ -148,13 +149,12 @@ func banExport(c echo.Context) error {
writer := bufio.NewWriter(temp)
err := json.NewEncoder(writer).Encode(&lst)
_ = writer.Flush()
if err == nil {
c.Response().Header().Add("Cache-Control", "no-store")
err := c.Attachment(temp.Name(), "黑白名单.json")
return err
} else {
if err != nil {
return Error(&c, err.Error(), Response{})
}

c.Response().Header().Add("Cache-Control", "no-store")
return c.Attachment(temp.Name(), "黑白名单.json")
}

func banImport(c echo.Context) error {
Expand Down
22 changes: 10 additions & 12 deletions api/censor.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func censorSetConfig(c echo.Context) error {
myDice.CensorMatchPinyin = matchPinyin
}
}
if val, ok := jsonMap["levelConfig"]; ok {
if val, ok := jsonMap["levelConfig"]; ok { //nolint:nestif
levelConfig, ok := val.(map[string]interface{})

stringConvert := func(val interface{}) []string {
Expand Down Expand Up @@ -281,9 +281,8 @@ func (srs SensitiveRelatedWords) Len() int { return len(srs) }
func (srs SensitiveRelatedWords) Less(i, j int) bool {
if srs[i].Reason == srs[j].Reason {
return srs[i].Word < srs[j].Word
} else {
return srs[i].Reason < srs[j].Reason
}
return srs[i].Reason < srs[j].Reason
}
func (srs SensitiveRelatedWords) Swap(i, j int) { srs[i], srs[j] = srs[j], srs[i] }

Expand All @@ -299,9 +298,8 @@ func (sws SensitiveWords) Len() int { return len(sws) }
func (sws SensitiveWords) Less(i, j int) bool {
if sws[i].Level == sws[j].Level {
return sws[i].Main < sws[j].Main
} else {
return sws[i].Level < sws[j].Level
}
return sws[i].Level < sws[j].Level
}
func (sws SensitiveWords) Swap(i, j int) { sws[i], sws[j] = sws[j], sws[i] }

Expand Down Expand Up @@ -467,15 +465,15 @@ func censorGetTomlFileTemplate(c echo.Context) error {
writer := bufio.NewWriter(temp)
err := toml.NewEncoder(writer).SetArraysMultiline(true).Encode(&template)
_ = writer.Flush()
if err == nil {
c.Response().Header().Add("Cache-Control", "no-store")
err := c.Attachment(temp.Name(), "词库模板.toml")
_ = temp.Close()
_ = os.RemoveAll(temp.Name())
return err
} else {
if err != nil {
return Error(&c, err.Error(), Response{})
}

c.Response().Header().Add("Cache-Control", "no-store")
err = c.Attachment(temp.Name(), "词库模板.toml")
_ = temp.Close()
_ = os.RemoveAll(temp.Name())
return err
}

func censorGetTxtFileTemplate(c echo.Context) error {
Expand Down
27 changes: 14 additions & 13 deletions api/checklist.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package api

import (
"github.com/labstack/echo/v4"
"net/http"

"github.com/labstack/echo/v4"
)

// 添加 checklist
func checklistAdd(c echo.Context) error { //nolint
//var err error
//cl := model.CheckList{}
//db := model.GetDB()
//
//if err := c.Bind(cl); err != nil {
// return err
//}
//
//err = db.Create(&cl).Error
//if err != nil {
// return c.String(http.StatusBadRequest, "")
//}
// var err error
// cl := model.CheckList{}
// db := model.GetDB()

// if err := c.Bind(cl); err != nil {
// return err
// }

// err = db.Create(&cl).Error
// if err != nil {
// return c.String(http.StatusBadRequest, "")
// }

return c.JSON(http.StatusCreated, nil)
}
Loading

0 comments on commit 958a9cd

Please sign in to comment.