diff --git a/bunrouterhelper/.golangci.yaml b/bunrouterhelper/.golangci.yaml new file mode 100644 index 0000000..d884b2f --- /dev/null +++ b/bunrouterhelper/.golangci.yaml @@ -0,0 +1,141 @@ +## 基于 golangci-lint@v1.52.2 +run: + timeout: 1m + build-tags: [ ] + skip-dirs: [ ] + skip-files: [ ] +linters: + disable-all: true + enable: + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - cyclop + - dupl + - durationcheck + - errname + - errorlint + - exhaustive + - exportloopref + - funlen + - gocheckcompilerdirectives + - gochecknoinits + - goconst + - gocritic + - gocyclo + - goimports + - gomnd + - goprintffuncname + - gosec + - lll + - loggercheck + - makezero + - nakedret + - nestif + - nilnil + - noctx + - nolintlint + - prealloc + - predeclared + - promlinter + - reassign + - revive + - rowserrcheck + - stylecheck + - tenv + - testableexamples + - testpackage + - tparallel + - unconvert + - unparam + - usestdlibvars + - wastedassign + - whitespace +linters-settings: + errcheck: + check-type-assertions: true + exclude-functions: [ ] + govet: + enable-all: true + disable: [ ] + cyclop: + max-complexity: 10 + package-average: 0.0 + dupl: + threshold: 150 + exhaustive: + check: + - switch + - map + funlen: + lines: 100 + statements: 60 + gocritic: + disabled-checks: + - commentFormatting + settings: + captLocal: + paramsOnly: false + underef: + skipRecvDeref: false + gocyclo: + min-complexity: 20 + gomnd: + ignored-functions: + - os.Chmod + - os.Mkdir + - os.MkdirAll + - os.OpenFile + - os.WriteFile + - prometheus.ExponentialBuckets + - prometheus.ExponentialBucketsRange + - prometheus.LinearBuckets + lll: + line-length: 240 + nakedret: + max-func-lines: 10 + nestif: + min-complexity: 5 + predeclared: + ignore: "" + q: false + reassign: + patterns: + - ".*" + rowserrcheck: + packages: + - github.com/jmoiron/sqlx + tenv: + all: true + usestdlibvars: + time-month: true + time-layout: true + crypto-hash: true + default-rpc-path: true + os-dev-null: true + sql-isolation-level: true + tls-signature-scheme: true + constant-kind: true + syslog-priority: true +issues: + max-same-issues: 10 + exclude-rules: + - source: "//noinspection" + linters: [ gocritic ] + - path: "_test\\.go" + linters: + - bodyclose + - dupl + - funlen + - goconst + - gosec + - noctx diff --git a/bunrouterhelper/go.mod b/bunrouterhelper/go.mod new file mode 100644 index 0000000..24a98e0 --- /dev/null +++ b/bunrouterhelper/go.mod @@ -0,0 +1,9 @@ +module git.blauwelle.com/go/crate/bunrouterhelper + +go 1.21.1 + +require ( + git.blauwelle.com/go/crate/httpdata v0.0.1 + git.blauwelle.com/go/crate/log v0.14.0 + github.com/uptrace/bunrouter v1.0.20 +) diff --git a/bunrouterhelper/go.sum b/bunrouterhelper/go.sum new file mode 100644 index 0000000..c9bf7ac --- /dev/null +++ b/bunrouterhelper/go.sum @@ -0,0 +1,14 @@ +git.blauwelle.com/go/crate/httpdata v0.0.1 h1:kje/L7B1rkzxPKEURqORVXjxiCR2F90oVBTwzmdimDY= +git.blauwelle.com/go/crate/httpdata v0.0.1/go.mod h1:/NrE0so/hjc2Djj850I5iBM+Juzc4Cv48V74zj2tALE= +git.blauwelle.com/go/crate/log v0.14.0 h1:y7hJXP+ZPY/wD+wlEzKgakpki8/l0LwZWqxtJ92Wy58= +git.blauwelle.com/go/crate/log v0.14.0/go.mod h1:jfVfpRODZTA70A8IkApVeGsS1zfLk1D77sLWZM/w+L0= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/uptrace/bunrouter v1.0.20 h1:jNvYNcJxF+lSYBQAaQjnE6I11Zs0m+3M5Ek7fq/Tp4c= +github.com/uptrace/bunrouter v1.0.20/go.mod h1:TwT7Bc0ztF2Z2q/ZzMuSVkcb/Ig/d3MQeP2cxn3e1hI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/bunrouterhelper/middleware.go b/bunrouterhelper/middleware.go new file mode 100644 index 0000000..dac6b10 --- /dev/null +++ b/bunrouterhelper/middleware.go @@ -0,0 +1,48 @@ +package bunrouterhelper + +import ( + "errors" + "fmt" + "net/http" + + "git.blauwelle.com/go/crate/httpdata" + "git.blauwelle.com/go/crate/log" + "github.com/uptrace/bunrouter" +) + +func MiddlewareRecover() bunrouter.MiddlewareFunc { + return func(next bunrouter.HandlerFunc) bunrouter.HandlerFunc { + return func(w http.ResponseWriter, req bunrouter.Request) (err error) { + defer func() { + if r := recover(); r != nil { + log.Error(req.Context(), r) + err = httpdata.NewUniverseError(httpdata.CodeUnexpectPanic, fmt.Sprintf("%v", r)) + } + }() + return next(w, req) + } + } +} + +func MiddlewareErrorHandler() bunrouter.MiddlewareFunc { + return func(next bunrouter.HandlerFunc) bunrouter.HandlerFunc { + return func(w http.ResponseWriter, req bunrouter.Request) error { + err := next(w, req) + if err != nil { + var universeError httpdata.UniverseError + if errors.As(err, &universeError) { + _ = bunrouter.JSON(w, httpdata.Response{ + Code: universeError.Code, + Message: universeError.Message, + }) + } else { + _ = bunrouter.JSON(w, httpdata.Response{ + Code: httpdata.CodeUnknown, + Message: err.Error(), + }) + } + } + return err + } + } +}