Skip to content

Commit 5f7109a

Browse files
authored
Merge pull request #266 from devfeel/aicode-lint-fixes
fix: resolve golangci-lint issues
2 parents 4fcc335 + 96cff54 commit 5f7109a

File tree

8 files changed

+17
-17
lines changed

8 files changed

+17
-17
lines changed

context.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
ItemKeyHandleDuration = "dotweb.HttpContext.HandleDuration"
3030
)
3131

32+
// ctxKey is a custom type for context keys to avoid staticcheck warnings
33+
type ctxKey string
34+
const requestIDKey ctxKey = "RequestID"
35+
3236
const (
3337
innerKeyAddView = "inner_AddView"
3438
)
@@ -200,7 +204,7 @@ func (ctx *HttpContext) Context() context.Context {
200204
// withvalue RequestID
201205
func (ctx *HttpContext) SetTimeoutContext(timeout time.Duration) context.Context {
202206
ctx.context, ctx.cancel = context.WithTimeout(context.Background(), timeout)
203-
ctx.context = context.WithValue(ctx.context, "RequestID", ctx.Request().RequestID())
207+
ctx.context = context.WithValue(ctx.context, requestIDKey, ctx.Request().RequestID())
204208
return ctx.context
205209
}
206210

@@ -210,7 +214,7 @@ func (ctx *HttpContext) WithContext(runCtx context.Context) {
210214
panic("nil context")
211215
}
212216
ctx.context = runCtx
213-
ctx.context = context.WithValue(ctx.context, "RequestID", ctx.Request().RequestID())
217+
ctx.context = context.WithValue(ctx.context, requestIDKey, ctx.Request().RequestID())
214218
}
215219

216220
// HttpServer return HttpServer

dotweb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (app *DotWeb) initPlugins() {
535535
func (app *DotWeb) initBindMiddleware() {
536536
router := app.HttpServer.Router().(*router)
537537
// bind app middlewares
538-
for fullExpress, _ := range router.allRouterExpress {
538+
for fullExpress := range router.allRouterExpress {
539539
expresses := strings.Split(fullExpress, routerExpressSplit)
540540
if len(expresses) < 2 {
541541
continue
@@ -567,7 +567,7 @@ func (app *DotWeb) initBindMiddleware() {
567567
if len(xg.middlewares) <= 0 {
568568
continue
569569
}
570-
for fullExpress, _ := range xg.allRouterExpress {
570+
for fullExpress := range xg.allRouterExpress {
571571
expresses := strings.Split(fullExpress, routerExpressSplit)
572572
if len(expresses) < 2 {
573573
continue

dotweb_sysgroup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func showQuery(ctx Context) error {
104104
func showRouters(ctx Context) error {
105105
data := ""
106106
routerCount := len(ctx.HttpServer().router.GetAllRouterExpress())
107-
for k, _ := range ctx.HttpServer().router.GetAllRouterExpress() {
107+
for k := range ctx.HttpServer().router.GetAllRouterExpress() {
108108
method := strings.Split(k, routerExpressSplit)[0]
109109
router := strings.Split(k, routerExpressSplit)[1]
110110
data += "<tr><td>" + method + "</td><td>" + router + "</td></tr>"

framework/crypto/des/des.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ECBDecrypt(crypted, key []byte) ([]byte, error) {
6767

6868
// [golang ECB 3DES Encrypt]
6969
func TripleEcbDesEncrypt(origData, key []byte) ([]byte, error) {
70-
tkey := make([]byte, 24, 24)
70+
tkey := make([]byte, 24)
7171
copy(tkey, key)
7272
k1 := tkey[:8]
7373
k2 := tkey[8:16]
@@ -97,7 +97,7 @@ func TripleEcbDesEncrypt(origData, key []byte) ([]byte, error) {
9797

9898
// [golang ECB 3DES Decrypt]
9999
func TripleEcbDesDecrypt(crypted, key []byte) ([]byte, error) {
100-
tkey := make([]byte, 24, 24)
100+
tkey := make([]byte, 24)
101101
copy(tkey, key)
102102
k1 := tkey[:8]
103103
k2 := tkey[8:16]

logger/xlog.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ func (l *xLog) writeLog(chanLog chanLog, level string) {
129129
switch level {
130130
case "custom":
131131
filePath = filePath + "_" + time.Now().Format(defaultDateFormatForFileName) + ".log"
132-
break
133132
}
134133
log := chanLog.Content
135134
if !chanLog.isRaw {
@@ -157,10 +156,10 @@ func writeFile(logFile string, log string) {
157156
mode = 0666
158157
logstr := log + "\r\n"
159158
file, err := os.OpenFile(logFile, flag, mode)
160-
defer file.Close()
161159
if err != nil {
162160
fmt.Println(logFile, err)
163161
return
164162
}
163+
defer file.Close()
165164
file.WriteString(logstr)
166165
}

middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (m *RequestLogMiddleware) Handle(ctx Context) error {
168168
} else {
169169
begin = beginVal.(time.Time)
170170
}
171-
timeTaken = uint64(time.Now().Sub(begin) / time.Millisecond)
171+
timeTaken = uint64(time.Since(begin) / time.Millisecond)
172172
}
173173
log := ctx.Request().Url() + " " + logContext(ctx, timeTaken)
174174
ctx.HttpServer().Logger().Debug(log, LogTarget_HttpRequest)
@@ -217,7 +217,7 @@ func (m *TimeoutHookMiddleware) Handle(ctx Context) error {
217217
// Do next
218218
err := m.Next(ctx)
219219
if m.HookHandle != nil {
220-
realDuration := time.Now().Sub(begin)
220+
realDuration := time.Since(begin)
221221
ctx.Items().Set(ItemKeyHandleDuration, realDuration)
222222
if realDuration > m.TimeoutDuration {
223223
m.HookHandle(ctx)

request.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,10 @@ func (req *Request) PostBody() []byte {
153153
if req.httpCtx != nil {
154154
switch req.httpCtx.HttpServer().DotApp.Config.Server.MaxBodySize {
155155
case -1:
156-
break
157156
case 0:
158157
req.Body = http.MaxBytesReader(req.httpCtx.Response().Writer(), req.Body, maxBodySize)
159-
break
160158
default:
161159
req.Body = http.MaxBytesReader(req.httpCtx.Response().Writer(), req.Body, req.httpApp().Config.Server.MaxBodySize)
162-
break
163160
}
164161
}
165162
bts, err := ioutil.ReadAll(req.Body)

router.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (r *router) RegisterServerFile(routeMethod string, path string, fileRoot st
416416
var root http.FileSystem
417417
root = http.Dir(fileRoot)
418418
if !r.server.ServerConfig().EnabledListDir {
419-
root = &core.HideReaddirFS{root}
419+
root = &core.HideReaddirFS{FileSystem: root}
420420
}
421421
fileServer := http.FileServer(root)
422422
r.add(routeMethod, realPath, r.wrapFileHandle(fileServer, excludeExtension))
@@ -607,7 +607,7 @@ func (r *router) wrapFileHandle(fileHandler http.Handler, excludeExtension []str
607607
httpCtx.Handler()(httpCtx)
608608
}
609609
if r.server.Logger().IsEnabledLog() {
610-
timetaken := int64(time.Now().Sub(startTime) / time.Millisecond)
610+
timetaken := int64(time.Since(startTime) / time.Millisecond)
611611
r.server.Logger().Debug(httpCtx.Request().Url()+" "+logRequest(httpCtx.Request().Request, timetaken), LogTarget_HttpRequest)
612612
}
613613
}
@@ -645,7 +645,7 @@ func (r *router) wrapWebSocketHandle(handler HttpHandle) websocket.Handler {
645645
// increment error count
646646
r.server.StateInfo().AddErrorCount(httpCtx.Request().Path(), fmt.Errorf("%v", err), 1)
647647
}
648-
timetaken := int64(time.Now().Sub(startTime) / time.Millisecond)
648+
timetaken := int64(time.Since(startTime) / time.Millisecond)
649649
// HttpServer Logging
650650
r.server.Logger().Debug(httpCtx.Request().Url()+" "+logWebsocketContext(httpCtx, timetaken), LogTarget_HttpRequest)
651651

0 commit comments

Comments
 (0)