Compare commits
2 Commits
exegroup/v
...
httpdata/v
| Author | SHA1 | Date | |
|---|---|---|---|
| 8671f2826d | |||
| 5cf4430bc8 |
@@ -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