Compare commits
4 Commits
exegroup/v
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
| dd36810af7 | |||
| e2f234c17e | |||
| 8671f2826d | |||
| 5cf4430bc8 |
8
cdata/go.mod
Normal file
8
cdata/go.mod
Normal file
@@ -0,0 +1,8 @@
|
||||
module git.blauwelle.com/go/crate/cdata
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require (
|
||||
git.blauwelle.com/go/crate/cerrors v0.1.0
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0
|
||||
)
|
||||
6
cdata/go.sum
Normal file
6
cdata/go.sum
Normal file
@@ -0,0 +1,6 @@
|
||||
git.blauwelle.com/go/crate/cerrors v0.0.0-20240507062518-e2f234c17eb2 h1:h3WnMe7tQNbh6WARbixrf8wTUrWCDNc3F6ikb1HQhj4=
|
||||
git.blauwelle.com/go/crate/cerrors v0.0.0-20240507062518-e2f234c17eb2/go.mod h1:vIWWWVAEwattbg1eVOUjnz5dEG7q7v/Ve0oR1HFsblM=
|
||||
git.blauwelle.com/go/crate/cerrors v0.1.0 h1:+9TdG54+AUG1NmUgrEPsAPY8n8RIUAQy9qx4drU9RtE=
|
||||
git.blauwelle.com/go/crate/cerrors v0.1.0/go.mod h1:vIWWWVAEwattbg1eVOUjnz5dEG7q7v/Ve0oR1HFsblM=
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0 h1:W19wipPCyFSGHOWzqtfouNJu7MDeJobP+iRM4bPiJpM=
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0/go.mod h1:yVMA0GkO9AS7iuPmalHKeWyv9en0JWj25rY1vpTuHhk=
|
||||
64
cdata/response.go
Normal file
64
cdata/response.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package cdata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.blauwelle.com/go/crate/cerrors"
|
||||
"git.blauwelle.com/go/crate/runtimehelper"
|
||||
)
|
||||
|
||||
const maximumFrames = 16
|
||||
|
||||
type Response[T any] struct {
|
||||
Data T `json:"data,omitempty"`
|
||||
Code cerrors.Code `json:"code"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Debug string `json:"debug,omitempty"`
|
||||
Traceback []runtimehelper.Frame `json:"traceback,omitempty"`
|
||||
}
|
||||
|
||||
type PageData[T any] struct {
|
||||
List []T `json:"list"`
|
||||
PageIndex int `json:"pageIndex"` // >=1
|
||||
PageSize int `json:"pageSize"` // >=1
|
||||
Total int `json:"total"` // maybe 0
|
||||
}
|
||||
|
||||
func NewOkResponse[T any](data T) Response[T] {
|
||||
return Response[T]{
|
||||
Code: cerrors.CodeOK,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func NewResponseFromError(err error) Response[struct{}] {
|
||||
e := &cerrors.Error{}
|
||||
if !errors.As(err, &e) {
|
||||
response := Response[struct{}]{
|
||||
Code: cerrors.CodeUnknown,
|
||||
Message: "未知错误",
|
||||
}
|
||||
if cerrors.FlagDebug {
|
||||
response.Debug = err.Error()
|
||||
response.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||||
}
|
||||
return response
|
||||
}
|
||||
return Response[struct{}]{
|
||||
Code: e.Code,
|
||||
Message: e.Message,
|
||||
Debug: e.Debug,
|
||||
Traceback: e.Traceback,
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorResponse(code cerrors.Code, message string) Response[struct{}] {
|
||||
response := Response[struct{}]{
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
if cerrors.FlagDebug {
|
||||
response.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||||
}
|
||||
return response
|
||||
}
|
||||
141
cerrors/.golangci.yaml
Normal file
141
cerrors/.golangci.yaml
Normal file
@@ -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
|
||||
14
cerrors/code.go
Normal file
14
cerrors/code.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package cerrors
|
||||
|
||||
type Code string
|
||||
|
||||
const (
|
||||
CodeOK Code = "ok"
|
||||
CodeError Code = "error"
|
||||
CodeBadRequest Code = "bad_request"
|
||||
CodeInternal Code = "internal"
|
||||
CodeInternalUpstream Code = "internal.upstream"
|
||||
CodeUnexpect Code = "unexpect"
|
||||
CodeUnexpectPanic Code = "unexpect.panic"
|
||||
CodeUnknown Code = "unknown"
|
||||
)
|
||||
47
cerrors/error.go
Normal file
47
cerrors/error.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package cerrors
|
||||
|
||||
import (
|
||||
"git.blauwelle.com/go/crate/runtimehelper"
|
||||
)
|
||||
|
||||
const maximumFrames = 16
|
||||
|
||||
type Error struct {
|
||||
Code Code
|
||||
Message string
|
||||
Debug string
|
||||
Traceback []runtimehelper.Frame
|
||||
}
|
||||
|
||||
func (err *Error) Error() string {
|
||||
if err.Message == "" {
|
||||
return string(err.Code)
|
||||
}
|
||||
return string(err.Code) + ": " + err.Message
|
||||
}
|
||||
|
||||
func New(code Code, message string) error {
|
||||
e := &Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
if FlagDebug {
|
||||
e.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func Wrap(err error, code Code, message string) error {
|
||||
e2 := &Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
if e1, ok := err.(*Error); ok { //nolint:errorlint
|
||||
e2.Debug = e1.Debug
|
||||
e2.Traceback = e1.Traceback
|
||||
} else if FlagDebug {
|
||||
e2.Debug = err.Error()
|
||||
e2.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||||
}
|
||||
return e2
|
||||
}
|
||||
7
cerrors/flag.go
Normal file
7
cerrors/flag.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package cerrors
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var FlagDebug = os.Getenv("DEBUG") != ""
|
||||
5
cerrors/go.mod
Normal file
5
cerrors/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module git.blauwelle.com/go/crate/cerrors
|
||||
|
||||
go 1.21.1
|
||||
|
||||
require git.blauwelle.com/go/crate/runtimehelper v0.2.0
|
||||
2
cerrors/go.sum
Normal file
2
cerrors/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0 h1:W19wipPCyFSGHOWzqtfouNJu7MDeJobP+iRM4bPiJpM=
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0/go.mod h1:yVMA0GkO9AS7iuPmalHKeWyv9en0JWj25rY1vpTuHhk=
|
||||
8
cerrors/std.go
Normal file
8
cerrors/std.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package cerrors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var Is = errors.Is
|
||||
var As = errors.As
|
||||
@@ -11,4 +11,5 @@ const (
|
||||
CodeUnexpect Code = "unexpect"
|
||||
CodeUnexpectPanic Code = "unexpect.panic"
|
||||
CodeUnknown Code = "unknown"
|
||||
CodeUnauthorized Code = "unauthorized"
|
||||
)
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
package httpdata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.blauwelle.com/go/crate/runtimehelper"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Data any `json:"data,omitempty"`
|
||||
Code Code `json:"code"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Data any `json:"data,omitempty"`
|
||||
Code Code `json:"code"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Debug string `json:"debug,omitempty"`
|
||||
Traceback []runtimehelper.Frame `json:"traceback,omitempty"`
|
||||
}
|
||||
|
||||
type PageData struct {
|
||||
List []any `json:"list"`
|
||||
PageIndex int `json:"pageIndex"` // >=1
|
||||
PageSize int `json:"pageSize"` // >=1
|
||||
Total int `json:"total"` // maybe 0
|
||||
List any `json:"list"`
|
||||
PageIndex int64 `json:"pageIndex"` // >=1
|
||||
PageSize int64 `json:"pageSize"` // >=1
|
||||
Total int64 `json:"total"` // maybe 0
|
||||
}
|
||||
|
||||
func NewOkResponse(data any) Response {
|
||||
@@ -20,3 +28,24 @@ func NewOkResponse(data any) Response {
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorResponse(err error) Response {
|
||||
ue := UniverseError{}
|
||||
if !errors.As(err, &ue) {
|
||||
response := Response{
|
||||
Code: CodeUnknown,
|
||||
Message: "未知错误",
|
||||
}
|
||||
if FlagDebug {
|
||||
response.Debug = err.Error()
|
||||
response.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||||
}
|
||||
return response
|
||||
}
|
||||
return Response{
|
||||
Code: ue.Code,
|
||||
Message: ue.Message,
|
||||
Debug: ue.Debug,
|
||||
Traceback: ue.Traceback,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,24 @@ package httpdata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.blauwelle.com/go/crate/runtimehelper"
|
||||
)
|
||||
|
||||
const maximumFrames = 8
|
||||
|
||||
type UniverseError struct {
|
||||
Code Code
|
||||
Message string
|
||||
Code Code
|
||||
Message string
|
||||
Debug string
|
||||
Traceback []runtimehelper.Frame
|
||||
}
|
||||
|
||||
func (err UniverseError) Error() string {
|
||||
if err.Message == "" {
|
||||
return string(err.Code)
|
||||
}
|
||||
return string(err.Code) + ": " + err.Message
|
||||
}
|
||||
|
||||
func NewUniverseError(code Code, message string) error {
|
||||
@@ -23,13 +36,6 @@ func NewBadRequestError(message string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (err UniverseError) Error() string {
|
||||
if err.Message == "" {
|
||||
return string(err.Code)
|
||||
}
|
||||
return string(err.Code) + ": " + err.Message
|
||||
}
|
||||
|
||||
func IsUniverseError(err error) bool {
|
||||
return errors.As(err, &UniverseError{})
|
||||
}
|
||||
|
||||
7
httpdata/flag.go
Normal file
7
httpdata/flag.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package httpdata
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var FlagDebug = os.Getenv("DEBUG") == "true"
|
||||
@@ -1,3 +1,5 @@
|
||||
module git.blauwelle.com/go/crate/httpdata
|
||||
|
||||
go 1.21.1
|
||||
go 1.21
|
||||
|
||||
require git.blauwelle.com/go/crate/runtimehelper v0.2.0
|
||||
|
||||
2
httpdata/go.sum
Normal file
2
httpdata/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0 h1:W19wipPCyFSGHOWzqtfouNJu7MDeJobP+iRM4bPiJpM=
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.2.0/go.mod h1:yVMA0GkO9AS7iuPmalHKeWyv9en0JWj25rY1vpTuHhk=
|
||||
@@ -2,6 +2,7 @@ package uptracehelper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/uptrace-go/uptrace"
|
||||
)
|
||||
@@ -10,6 +11,7 @@ type Config struct {
|
||||
ServiceName string
|
||||
ServiceVersion string
|
||||
DeploymentEnvironment string
|
||||
ShutdownTimeout time.Duration
|
||||
}
|
||||
|
||||
func Setup(cfg Config) {
|
||||
@@ -23,19 +25,21 @@ func Setup(cfg Config) {
|
||||
uptrace.ConfigureOpentelemetry(opts...)
|
||||
}
|
||||
|
||||
// GoStop 初始化并等待 uptrace
|
||||
// Bootstrap 初始化并等待 uptrace
|
||||
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用
|
||||
// env
|
||||
// - UPTRACE_DISABLED 存在就不再初始化
|
||||
// - UPTRACE_DSN 服务端地址
|
||||
func GoStop(cfg Config) (func(ctx context.Context) error, func(ctx context.Context)) {
|
||||
func Bootstrap(cfg Config) func(ctx context.Context) error {
|
||||
Setup(cfg)
|
||||
shutdownErr := make(chan error, 1)
|
||||
goFunc := func(context.Context) error {
|
||||
return <-shutdownErr
|
||||
return func(ctx context.Context) error {
|
||||
<-ctx.Done()
|
||||
shutdownCtx := context.Background()
|
||||
if cfg.ShutdownTimeout >= 0 {
|
||||
var cancel func()
|
||||
shutdownCtx, cancel = context.WithTimeout(shutdownCtx, cfg.ShutdownTimeout)
|
||||
defer cancel()
|
||||
}
|
||||
return uptrace.Shutdown(shutdownCtx)
|
||||
}
|
||||
stopFunc := func(ctx context.Context) {
|
||||
shutdownErr <- uptrace.Shutdown(ctx)
|
||||
}
|
||||
return goFunc, stopFunc
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user