replace all schema check with go-playground/validator/v10

This commit is contained in:
yusing
2024-12-18 04:48:29 +08:00
parent 00f60a6e78
commit 6aefe4d5d9
23 changed files with 149 additions and 250 deletions

View File

@@ -2,6 +2,7 @@ package accesslog
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
@@ -37,7 +38,7 @@ const logTimeFormat = "02/Jan/2006:15:04:05 -0700"
func NewFileAccessLogger(parent *task.Task, cfg *Config) (*AccessLogger, error) {
f, err := os.OpenFile(cfg.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
return nil, fmt.Errorf("access log open error: %w", err)
}
return NewAccessLogger(parent, f, cfg), nil
}

View File

@@ -1,6 +1,7 @@
package accesslog_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
@@ -13,25 +14,11 @@ import (
. "github.com/yusing/go-proxy/internal/utils/testing"
)
type testWritter struct {
line string
}
func (w *testWritter) Write(p []byte) (n int, err error) {
w.line = string(p)
return len(p), nil
}
func (w *testWritter) Close() error {
return nil
}
var tw testWritter
const (
remote = "192.168.1.1"
u = "http://example.com/?bar=baz&foo=bar"
uRedacted = "http://example.com/?bar=" + RedactedValue + "&foo=" + RedactedValue
host = "example.com"
uri = "/?bar=baz&foo=bar"
uriRedacted = "/?bar=" + RedactedValue + "&foo=" + RedactedValue
referer = "https://www.google.com/"
proto = "HTTP/1.1"
ua = "Go-http-client/1.1"
@@ -41,7 +28,7 @@ const (
)
var (
testURL = E.Must(url.Parse(u))
testURL = E.Must(url.Parse("http://" + host + uri))
req = &http.Request{
RemoteAddr: remote,
Method: method,
@@ -62,17 +49,21 @@ var (
ContentLength: contentLength,
Header: http.Header{"Content-Type": []string{"text/plain"}},
}
task = taskPkg.GlobalTask("test logger")
)
func fmtLog(cfg *Config) string {
var line bytes.Buffer
logger := NewAccessLogger(taskPkg.GlobalTask("test logger"), nil, cfg)
logger.Format(&line, req, resp)
return line.String()
}
func TestAccessLoggerCommon(t *testing.T) {
config := DefaultConfig
config.Format = FormatCommon
logger := NewAccessLogger(task, &tw, &config)
logger.Log(req, resp)
ExpectEqual(t, tw.line,
fmt.Sprintf("%s - - [%s] \"%s %s %s\" %d %d\n",
remote, TestTimeNow, method, u, proto, status, contentLength,
ExpectEqual(t, fmtLog(&config),
fmt.Sprintf("%s %s - - [%s] \"%s %s %s\" %d %d",
host, remote, TestTimeNow, method, uri, proto, status, contentLength,
),
)
}
@@ -80,11 +71,9 @@ func TestAccessLoggerCommon(t *testing.T) {
func TestAccessLoggerCombined(t *testing.T) {
config := DefaultConfig
config.Format = FormatCombined
logger := NewAccessLogger(task, &tw, &config)
logger.Log(req, resp)
ExpectEqual(t, tw.line,
fmt.Sprintf("%s - - [%s] \"%s %s %s\" %d %d \"%s\" \"%s\"\n",
remote, TestTimeNow, method, u, proto, status, contentLength, referer, ua,
ExpectEqual(t, fmtLog(&config),
fmt.Sprintf("%s %s - - [%s] \"%s %s %s\" %d %d \"%s\" \"%s\"",
host, remote, TestTimeNow, method, uri, proto, status, contentLength, referer, ua,
),
)
}
@@ -93,11 +82,9 @@ func TestAccessLoggerRedactQuery(t *testing.T) {
config := DefaultConfig
config.Format = FormatCommon
config.Fields.Query.DefaultMode = FieldModeRedact
logger := NewAccessLogger(task, &tw, &config)
logger.Log(req, resp)
ExpectEqual(t, tw.line,
fmt.Sprintf("%s - - [%s] \"%s %s %s\" %d %d\n",
remote, TestTimeNow, method, uRedacted, proto, status, contentLength,
ExpectEqual(t, fmtLog(&config),
fmt.Sprintf("%s %s - - [%s] \"%s %s %s\" %d %d",
host, remote, TestTimeNow, method, uriRedacted, proto, status, contentLength,
),
)
}
@@ -105,10 +92,8 @@ func TestAccessLoggerRedactQuery(t *testing.T) {
func getJSONEntry(t *testing.T, config *Config) JSONLogEntry {
t.Helper()
config.Format = FormatJSON
logger := NewAccessLogger(task, &tw, config)
logger.Log(req, resp)
var entry JSONLogEntry
err := json.Unmarshal([]byte(tw.line), &entry)
err := json.Unmarshal([]byte(fmtLog(config)), &entry)
ExpectNoError(t, err)
return entry
}

View File

@@ -13,7 +13,7 @@ func TestNewConfig(t *testing.T) {
labels := map[string]string{
"proxy.buffer_size": "10",
"proxy.format": "combined",
"proxy.file_path": "/tmp/access.log",
"proxy.path": "/tmp/access.log",
"proxy.filters.status_codes.values": "200-299",
"proxy.filters.method.values": "GET, POST",
"proxy.filters.headers.values": "foo=bar, baz",

View File

@@ -1,8 +1,8 @@
package types
type Config struct {
Link string `json:"link" yaml:"link"`
Mode Mode `json:"mode" yaml:"mode"`
Weight Weight `json:"weight" yaml:"weight"`
Options map[string]any `json:"options,omitempty" yaml:"options,omitempty"`
Link string `json:"link"`
Mode Mode `json:"mode"`
Weight Weight `json:"weight"`
Options map[string]any `json:"options,omitempty"`
}