fix golint issues in rest (#529)

This commit is contained in:
Kevin Wan
2021-03-01 19:15:35 +08:00
committed by GitHub
parent d894b88c3e
commit 655ae8034c
29 changed files with 132 additions and 32 deletions

View File

@@ -7,6 +7,7 @@ import (
var pathVars = contextKey("pathVars")
// Vars parses path variables and returns a map.
func Vars(r *http.Request) map[string]string {
vars, ok := r.Context().Value(pathVars).(map[string]string)
if ok {
@@ -16,6 +17,7 @@ func Vars(r *http.Request) map[string]string {
return nil
}
// WithPathVars writes params into given r and returns a new http.Request.
func WithPathVars(r *http.Request, params map[string]string) *http.Request {
return r.WithContext(context.WithValue(r.Context(), pathVars, params))
}

View File

@@ -10,19 +10,23 @@ import (
"github.com/tal-tech/go-zero/rest/httpx"
)
// LogContext is a context key.
var LogContext = contextKey("request_logs")
// A LogCollector is used to collect logs.
type LogCollector struct {
Messages []string
lock sync.Mutex
}
// Append appends msg into log context.
func (lc *LogCollector) Append(msg string) {
lc.lock.Lock()
lc.Messages = append(lc.Messages, msg)
lc.lock.Unlock()
}
// Flush flushes collected logs.
func (lc *LogCollector) Flush() string {
var buffer bytes.Buffer
@@ -48,18 +52,22 @@ func (lc *LogCollector) takeAll() []string {
return messages
}
// Error logs the given v along with r in error log.
func Error(r *http.Request, v ...interface{}) {
logx.ErrorCaller(1, format(r, v...))
}
// Errorf logs the given v with format along with r in error log.
func Errorf(r *http.Request, format string, v ...interface{}) {
logx.ErrorCaller(1, formatf(r, format, v...))
}
// Info logs the given v along with r in access log.
func Info(r *http.Request, v ...interface{}) {
appendLog(r, format(r, v...))
}
// Infof logs the given v with format along with r in access log.
func Infof(r *http.Request, format string, v ...interface{}) {
appendLog(r, formatf(r, format, v...))
}

View File

@@ -25,13 +25,19 @@ const (
)
var (
// ErrInvalidContentType is an error that indicates invalid content type.
ErrInvalidContentType = errors.New("invalid content type")
ErrInvalidHeader = errors.New("invalid X-Content-Security header")
ErrInvalidKey = errors.New("invalid key")
ErrInvalidPublicKey = errors.New("invalid public key")
ErrInvalidSecret = errors.New("invalid secret")
// ErrInvalidHeader is an error that indicates invalid X-Content-Security header.
ErrInvalidHeader = errors.New("invalid X-Content-Security header")
// ErrInvalidKey is an error that indicates invalid key.
ErrInvalidKey = errors.New("invalid key")
// ErrInvalidPublicKey is an error that indicates invalid public key.
ErrInvalidPublicKey = errors.New("invalid public key")
// ErrInvalidSecret is an error that indicates invalid secret.
ErrInvalidSecret = errors.New("invalid secret")
)
// A ContentSecurityHeader is a content security header.
type ContentSecurityHeader struct {
Key []byte
Timestamp string
@@ -39,10 +45,12 @@ type ContentSecurityHeader struct {
Signature string
}
// Encrypted checks if it's a crypted request.
func (h *ContentSecurityHeader) Encrypted() bool {
return h.ContentType == httpx.CryptionType
}
// ParseContentSecurity parses content security settings in give r.
func ParseContentSecurity(decrypters map[string]codec.RsaDecrypter, r *http.Request) (
*ContentSecurityHeader, error) {
contentSecurity := r.Header.Get(httpx.ContentSecurity)
@@ -88,6 +96,7 @@ func ParseContentSecurity(decrypters map[string]codec.RsaDecrypter, r *http.Requ
}, nil
}
// VerifySignature verifies the signature in given r.
func VerifySignature(r *http.Request, securityHeader *ContentSecurityHeader, tolerance time.Duration) int {
seconds, err := strconv.ParseInt(securityHeader.Timestamp, 10, 64)
if err != nil {

View File

@@ -2,25 +2,30 @@ package security
import "net/http"
// A WithCodeResponseWriter is a helper to delay sealing a http.ResponseWriter on writing code.
type WithCodeResponseWriter struct {
Writer http.ResponseWriter
Code int
}
// Flush flushes the response writer.
func (w *WithCodeResponseWriter) Flush() {
if flusher, ok := w.Writer.(http.Flusher); ok {
flusher.Flush()
}
}
// Header returns the http header.
func (w *WithCodeResponseWriter) Header() http.Header {
return w.Writer.Header()
}
// Write writes bytes into w.
func (w *WithCodeResponseWriter) Write(bytes []byte) (int, error) {
return w.Writer.Write(bytes)
}
// WriteHeader writes code into w, and not sealing the writer.
func (w *WithCodeResponseWriter) WriteHeader(code int) {
w.Writer.WriteHeader(code)
w.Code = code

View File

@@ -8,12 +8,14 @@ import (
"github.com/tal-tech/go-zero/core/proc"
)
// StartHttp starts a http server.
func StartHttp(host string, port int, handler http.Handler) error {
return start(host, port, handler, func(srv *http.Server) error {
return srv.ListenAndServe()
})
}
// StartHttps starts a https server.
func StartHttps(host string, port int, certFile, keyFile string, handler http.Handler) error {
return start(host, port, handler, func(srv *http.Server) error {
// certFile and keyFile are set in buildHttpsServer