Skip to content

Commit 4dd9db6

Browse files
committed
commit
1 parent 58ad894 commit 4dd9db6

File tree

16 files changed

+675
-0
lines changed

16 files changed

+675
-0
lines changed

Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
GOHOSTOS:=$(shell go env GOHOSTOS)
2+
GOPATH:=$(shell go env GOPATH)
3+
VERSION=$(shell git describe --tags --always)
4+
BUILDTIME=$(shell date "+%Y-%m-%d %H:%M:%S")
5+
GITCOMMITID=$(shell git rev-parse HEAD)
6+
7+
ifeq ($(GOHOSTOS), windows)
8+
#the `find.exe` is different from `find` in bash/shell.
9+
#to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
10+
#changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
11+
#Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
12+
Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git))))
13+
INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal -name *.proto")
14+
API_PROTO_FILES=$(shell $(Git_Bash) -c "find api -name *.proto")
15+
else
16+
INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
17+
API_PROTO_FILES=$(shell find api -name *.proto)
18+
endif
19+
20+
.PHONY: init
21+
# init env
22+
init:
23+
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
24+
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
25+
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
26+
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
27+
go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
28+
29+
.PHONT: errors
30+
errors:
31+
protoc --proto_path=./api \
32+
--proto_path=./third_party \
33+
--go_out=paths=import:. \
34+
--go-errors_out=paths=import:. \
35+
$(API_PROTO_FILES)

api/xerr/proto/xerr.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
syntax = "proto3";
2+
3+
package xerr.v1;
4+
5+
import "errors/errors.proto";
6+
7+
// 多语言特定包名,用于源代码引用
8+
option go_package = "./api/xerr;xerr";
9+
10+
enum Reason {
11+
option (errors.default_code) = 500;
12+
13+
// 鉴权错误包括:token错误、token过期、token无效、无权限访问等
14+
AUTH = 0 [(errors.code) = 401];
15+
16+
// 数据操作错误包括:数据不存在、数据已存在、数据已删除等
17+
DB_OPTION = 4 [(errors.code) = 403];
18+
}

api/xerr/xerr.pb.go

Lines changed: 133 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/xerr/xerr_errors.pb.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/service/info.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package service
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
type Info struct {
9+
Id string
10+
Name string
11+
Version string
12+
Metadata map[string]string
13+
}
14+
15+
func NewServiceInfo(name, version, id string) Info {
16+
if id == "" {
17+
id = fmt.Sprint(time.Now().Unix())
18+
}
19+
return Info{
20+
Name: name,
21+
Version: version,
22+
Id: id,
23+
Metadata: map[string]string{},
24+
}
25+
}
26+
27+
func (this *Info) GetInstanceId() string {
28+
return this.Name + "." + this.Id
29+
}
30+
31+
func (this *Info) SetMataData(k, v string) {
32+
this.Metadata[k] = v
33+
}

pkg/service/tracer.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package service
2+
3+
import (
4+
"go.opentelemetry.io/otel"
5+
"go.opentelemetry.io/otel/attribute"
6+
"go.opentelemetry.io/otel/exporters/jaeger"
7+
"go.opentelemetry.io/otel/sdk/resource"
8+
traceSdk "go.opentelemetry.io/otel/sdk/trace"
9+
semConv "go.opentelemetry.io/otel/semconv/v1.4.0"
10+
)
11+
12+
func NewTracerProvider(endpoint, env string, info *Info) error {
13+
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(endpoint)))
14+
if err != nil {
15+
return err
16+
}
17+
18+
tp := traceSdk.NewTracerProvider(
19+
traceSdk.WithSampler(traceSdk.ParentBased(traceSdk.TraceIDRatioBased(1.0))),
20+
traceSdk.WithBatcher(exp),
21+
traceSdk.WithResource(resource.NewSchemaless(
22+
semConv.ServiceNameKey.String(info.Name),
23+
semConv.ServiceVersionKey.String(info.Version),
24+
semConv.ServiceInstanceIDKey.String(info.Id),
25+
attribute.String("env", env),
26+
)),
27+
)
28+
29+
otel.SetTracerProvider(tp)
30+
31+
return nil
32+
}

pkg/service/writer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package service

pkg/share/auth.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package share
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/aswancen/libs/pkg/xmw"
7+
)
8+
9+
// GetRpcToken 获取Token
10+
func GetRpcToken(ctx context.Context) (*xmw.AuthInfo, error) {
11+
value := ctx.Value(xmw.CtxAuthKey)
12+
if token, IsOk := value.(*xmw.AuthInfo); IsOk {
13+
return token, nil
14+
}
15+
return nil, fmt.Errorf("token not found")
16+
}

pkg/share/gen_id.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package share
2+
3+
import (
4+
"time"
5+
6+
"github.com/sony/sonyflake"
7+
)
8+
9+
type IdGenerator struct {
10+
flake *sonyflake.Sonyflake
11+
}
12+
13+
func NewIdGenerator(baseTime string) *IdGenerator {
14+
loc, _ := time.LoadLocation("Asia/Shanghai")
15+
dt, err := time.ParseInLocation("2006-01-02", baseTime, loc)
16+
if err != nil {
17+
panic(err)
18+
}
19+
settings := sonyflake.Settings{
20+
StartTime: dt,
21+
MachineID: nil,
22+
CheckMachineID: nil,
23+
}
24+
flake := sonyflake.NewSonyflake(settings)
25+
return &IdGenerator{flake: flake}
26+
}
27+
28+
func (this *IdGenerator) GetId() int64 {
29+
nextID, _ := this.flake.NextID()
30+
return int64(nextID)
31+
}

pkg/share/page.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package share
2+
3+
import "gorm.io/gorm"
4+
5+
// Paginate 分页器(offset从1开始)
6+
func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
7+
if pageSize > 10000 {
8+
pageSize = 10000
9+
}
10+
if pageSize < 1 {
11+
pageSize = 10
12+
}
13+
return func(db *gorm.DB) *gorm.DB {
14+
return db.Offset((page - 1) * pageSize).Limit(pageSize)
15+
}
16+
}

0 commit comments

Comments
 (0)