lint: fix code style

This commit is contained in:
Alexander Baryshnikov 2020-10-01 21:13:44 +08:00
parent 68c0c63a9f
commit 034c7d5157
4 changed files with 32 additions and 30 deletions

View File

@ -26,7 +26,7 @@ func Expose(router gin.IRouter, wrk *worker.Worker) {
id, err := wrk.Enqueue(gctx.Request) id, err := wrk.Enqueue(gctx.Request)
if err != nil { if err != nil {
log.Println("failed to enqueue:", err) log.Println("failed to enqueue:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
gctx.Header("X-Correlation-Id", id) gctx.Header("X-Correlation-Id", id)
@ -87,7 +87,7 @@ func (wh *workerHandler) retry(gctx *gin.Context) {
id, err := wh.wrk.Retry(gctx.Request.Context(), requestID) id, err := wh.wrk.Retry(gctx.Request.Context(), requestID)
if err != nil { if err != nil {
log.Println("failed to retry:", err) log.Println("failed to retry:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
gctx.Header("X-Correlation-Id", id) gctx.Header("X-Correlation-Id", id)
@ -106,7 +106,7 @@ func (wh *workerHandler) completeRequest(gctx *gin.Context) {
err = wh.wrk.Meta().Complete(requestID) err = wh.wrk.Meta().Complete(requestID)
if err != nil { if err != nil {
log.Println("failed to mark request as complete:", err) log.Println("failed to mark request as complete:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
} }
@ -154,7 +154,7 @@ func (wh *workerHandler) getAttempt(gctx *gin.Context) {
body, err := wh.wrk.Blobs().Get(attempt.ID) body, err := wh.wrk.Blobs().Get(attempt.ID)
if err != nil { if err != nil {
log.Println("failed to get body:", err) log.Println("failed to get body:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer body.Close() defer body.Close()
@ -183,7 +183,7 @@ func (wh *workerHandler) getRequest(gctx *gin.Context) {
f, err := wh.wrk.Blobs().Get(requestID) f, err := wh.wrk.Blobs().Get(requestID)
if err != nil { if err != nil {
log.Println("failed to get data:", err) log.Println("failed to get data:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer f.Close() defer f.Close()

View File

@ -1,6 +1,7 @@
package ui package ui
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -36,7 +37,7 @@ func (cfg OAuth2) Attach(router gin.IRouter, storage SessionStorage) {
token, err := cfg.Config.Exchange(gctx.Request.Context(), gctx.Query("code")) token, err := cfg.Config.Exchange(gctx.Request.Context(), gctx.Query("code"))
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusForbidden, err) _ = gctx.AbortWithError(http.StatusForbidden, err)
return return
} }
@ -47,9 +48,9 @@ func (cfg OAuth2) Attach(router gin.IRouter, storage SessionStorage) {
sessionID := uuid.New().String() sessionID := uuid.New().String()
session := newOAuthSession(token) session := newOAuthSession(token)
err = session.fetchLogin(cfg.ProfileURL, cfg.LoginField) err = session.fetchLogin(gctx.Request.Context(), cfg.ProfileURL, cfg.LoginField)
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusForbidden, err) _ = gctx.AbortWithError(http.StatusForbidden, err)
return return
} }
@ -86,12 +87,12 @@ func (ss *oauthSession) Valid() bool {
return token.Valid() return token.Valid()
} }
func (ss *oauthSession) GetJSON(url string, response interface{}) error { func (ss *oauthSession) GetJSON(ctx context.Context, url string, response interface{}) error {
t, err := ss.token.Token() t, err := ss.token.Token()
if err != nil { if err != nil {
return err return err
} }
req, err := http.NewRequest(http.MethodGet, url, nil) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil { if err != nil {
return err return err
} }
@ -107,9 +108,9 @@ func (ss *oauthSession) GetJSON(url string, response interface{}) error {
return json.NewDecoder(res.Body).Decode(response) return json.NewDecoder(res.Body).Decode(response)
} }
func (ss *oauthSession) fetchLogin(url string, field string) error { func (ss *oauthSession) fetchLogin(ctx context.Context, url string, field string) error {
var profile = make(map[string]interface{}) var profile = make(map[string]interface{})
err := ss.GetJSON(url, &profile) err := ss.GetJSON(ctx, url, &profile)
if err != nil { if err != nil {
return err return err
} }

View File

@ -81,7 +81,7 @@ func (ui *uiRouter) unitRequestAttemptResult(gctx *gin.Context) {
f, err := info.Worker.Blobs().Get(attemptID) f, err := info.Worker.Blobs().Get(attemptID)
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusNotFound, err) _ = gctx.AbortWithError(http.StatusNotFound, err)
return return
} }
defer f.Close() defer f.Close()
@ -102,10 +102,10 @@ func (ui *uiRouter) unitRequestAttemptInfo(gctx *gin.Context) {
gctx.AbortWithStatus(http.StatusNotFound) gctx.AbortWithStatus(http.StatusNotFound)
return return
} }
requestId := gctx.Param("request") requestID := gctx.Param("request")
request, err := info.Worker.Meta().Get(requestId) request, err := info.Worker.Meta().Get(requestID)
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusNotFound, err) _ = gctx.AbortWithError(http.StatusNotFound, err)
return return
} }
@ -130,7 +130,7 @@ func (ui *uiRouter) unitRequestAttemptInfo(gctx *gin.Context) {
baseResponse: base(gctx), baseResponse: base(gctx),
unitInfo: info, unitInfo: info,
Request: request, Request: request,
RequestID: requestId, RequestID: requestID,
}, },
AttemptID: attemptID, AttemptID: attemptID,
Attempt: attempt, Attempt: attempt,
@ -151,10 +151,10 @@ func (ui *uiRouter) unitRequestInfo(gctx *gin.Context) {
gctx.AbortWithStatus(http.StatusNotFound) gctx.AbortWithStatus(http.StatusNotFound)
return return
} }
requestId := gctx.Param("request") requestID := gctx.Param("request")
request, err := info.Worker.Meta().Get(requestId) request, err := info.Worker.Meta().Get(requestID)
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusNotFound, err) _ = gctx.AbortWithError(http.StatusNotFound, err)
return return
} }
@ -162,7 +162,7 @@ func (ui *uiRouter) unitRequestInfo(gctx *gin.Context) {
baseResponse: base(gctx), baseResponse: base(gctx),
unitInfo: info, unitInfo: info,
Request: request, Request: request,
RequestID: requestId, RequestID: requestID,
}) })
} }
@ -178,7 +178,7 @@ func (ui *uiRouter) unitRequestRetry(gctx *gin.Context) {
id, err := item.Worker.Retry(gctx.Request.Context(), requestID) id, err := item.Worker.Retry(gctx.Request.Context(), requestID)
if err != nil { if err != nil {
log.Println("failed to retry:", err) log.Println("failed to retry:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -195,14 +195,14 @@ func (ui *uiRouter) unitRequestPayload(gctx *gin.Context) {
requestID := gctx.Param("request") requestID := gctx.Param("request")
info, err := item.Worker.Meta().Get(requestID) info, err := item.Worker.Meta().Get(requestID)
if !ok { if !ok {
gctx.AbortWithError(http.StatusNotFound, err) _ = gctx.AbortWithError(http.StatusNotFound, err)
return return
} }
gctx.Header("Last-Modified", info.CreatedAt.Format(time.RFC850)) gctx.Header("Last-Modified", info.CreatedAt.Format(time.RFC850))
f, err := item.Worker.Blobs().Get(requestID) f, err := item.Worker.Blobs().Get(requestID)
if err != nil { if err != nil {
log.Println("failed to get data:", err) log.Println("failed to get data:", err)
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer f.Close() defer f.Close()
@ -242,15 +242,15 @@ func (ui *uiRouter) unitInvoke(gctx *gin.Context) {
return return
} }
data := gctx.PostForm("body") data := gctx.PostForm("body")
req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBufferString(data)) req, err := http.NewRequestWithContext(gctx.Request.Context(), http.MethodPost, "/", bytes.NewBufferString(data))
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
id, err := info.Worker.Enqueue(req) id, err := info.Worker.Enqueue(req)
if err != nil { if err != nil {
gctx.AbortWithError(http.StatusInternalServerError, err) _ = gctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
gctx.Redirect(http.StatusSeeOther, base(gctx).Rel("/unit", name, "request", id)) gctx.Redirect(http.StatusSeeOther, base(gctx).Rel("/unit", name, "request", id))

View File

@ -482,19 +482,20 @@ func (mgr *Worker) restoreRequest(ctx context.Context, requestID string, info *m
return req, nil return req, nil
} }
func encodeID(nsId byte, id uint64) string { func encodeID(nsID byte, id uint64) string {
var data [9]byte var data [9]byte
data[0] = nsId data[0] = nsID
binary.BigEndian.PutUint64(data[1:], id) binary.BigEndian.PutUint64(data[1:], id)
return strings.ToUpper(hex.EncodeToString(data[:])) return strings.ToUpper(hex.EncodeToString(data[:]))
} }
func decodeID(val string) (byte, uint64, error) { func decodeID(val string) (byte, uint64, error) {
const idLen = 1 + 8
hx, err := hex.DecodeString(val) hx, err := hex.DecodeString(val)
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
if len(hx) != 9 { if len(hx) != idLen {
return 0, 0, errors.New("too short") return 0, 0, errors.New("too short")
} }
n := binary.BigEndian.Uint64(hx[1:]) n := binary.BigEndian.Uint64(hx[1:])