Skip to content

Commit

Permalink
升级依赖版本&&重构代码
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyTony committed Mar 24, 2024
1 parent fbe94cf commit b2be0d8
Show file tree
Hide file tree
Showing 40 changed files with 1,955 additions and 120 deletions.
259 changes: 164 additions & 95 deletions api/config/v1/config.pb.go

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions api/config/v1/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ message GRPCClient {

// logger config
message Logger {
string path = 1;
string level = 2;
bool console = 3;
bool file = 4;
map<string, string> metadata = 5;
string file_name = 1; // 日志文件名,默认按照日志级别命名
string path = 2; // 设置日志路径,默认为 /data/logs/${项目名}/projlogs
string level = 3; // 用于过滤日志的日志级别。默认为 info
bool console = 4; // 是否输出到控制台,默认为 true
bool file = 5; // 模式将日志写到 Path 指定目录的文件中
bool compress = 6; // 是否压缩日志文件,只在 file 模式下工作
int32 keep_days = 7; // 日志文件被保留多少天,在给定的天数之后,过期的文件将被自动删除。对 console 模式没有影响
int32 keep_hours = 8; // 日志文件被保留多少小时,在给定的小时数之后,过期的文件将被自动删除。对 console 模式没有影响
int32 max_backups = 9; // 多少个日志文件备份将被保存。0代表所有备份都被保存。当Rotation被设置为size时才会起作用。注意:KeepDays选项的优先级会比MaxBackups高,即使MaxBackups被设置为0,当达到KeepDays上限时备份文件同样会被删除。
int32 max_size = 10; // 当前被写入的日志文件最大可占用多少空间。0代表没有上限。单位为MB。当Rotation被设置为size时才会起作用
string rotation = 11; // 日志轮转策略类型。默认为daily(按天轮转),可选值为: daily 按天轮转、size 按日志大小轮转、hour 按小时轮转
map<string, string> metadata = 20; // 日志元数据
}

// Broker config
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/go-kratos/aegis v0.2.0
github.com/go-kratos/kratos/v2 v2.7.2
github.com/go-kratos/kratos/v2 v2.7.3
github.com/nextmicro/gokit/timex v1.0.1
github.com/nextmicro/gokit/trace v1.0.11
github.com/stretchr/testify v1.8.4
Expand All @@ -16,13 +16,13 @@ require (
require (
github.com/IBM/sarama v1.42.1
github.com/go-kratos/kratos/contrib/metrics/prometheus/v2 v2.0.0-20240311125537-f566bdc2e6ff
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20240311125537-f566bdc2e6ff
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20240311125537-f566bdc2e6ff
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20240322155018-41971ffa647a
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20240322155018-41971ffa647a
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/hashicorp/consul/api v1.26.1
github.com/nacos-group/nacos-sdk-go/v2 v2.2.5
github.com/nextmicro/logger v1.0.5
github.com/nextmicro/logger v1.0.7
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
go.etcd.io/etcd/client/v3 v3.5.11
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ github.com/go-kratos/aegis v0.2.0 h1:dObzCDWn3XVjUkgxyBp6ZeWtx/do0DPZ7LY3yNSJLUQ
github.com/go-kratos/aegis v0.2.0/go.mod h1:v0R2m73WgEEYB3XYu6aE2WcMwsZkJ/Rzuf5eVccm7bI=
github.com/go-kratos/kratos/contrib/metrics/prometheus/v2 v2.0.0-20240311125537-f566bdc2e6ff h1:cP9gjOGwvcK606sWY5fU7RO8A0X+62VfJ5Dh+KV5ZVE=
github.com/go-kratos/kratos/contrib/metrics/prometheus/v2 v2.0.0-20240311125537-f566bdc2e6ff/go.mod h1:6Zk5kARC1SimKz1xrP27S0TM66ZR0WjFV8DXsbAdd8Q=
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20240311125537-f566bdc2e6ff h1:ih+sN6DsBHAgDnX+XqAZDisGIjtWXLKWftO6P1e4wQw=
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20240311125537-f566bdc2e6ff/go.mod h1:yb9i4gq3jCW5zMFqzi50J0Xh3n4MRbcMlnuDBXYpvjs=
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20240311125537-f566bdc2e6ff h1:KovF/irdzZ+7DgNqf89mF1h8DjOwMsQGqUxATyg2Y3s=
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20240311125537-f566bdc2e6ff/go.mod h1:CiTe7H5Lj8WB6dGvBwS4HFukcWnobfq+vFWedIGYftA=
github.com/go-kratos/kratos/v2 v2.7.2 h1:WVPGFNLKpv+0odMnCPxM4ZHa2hy9I5FOnwpG3Vv4w5c=
github.com/go-kratos/kratos/v2 v2.7.2/go.mod h1:rppuc8+pGL2UtXA29bgFHWKqaaF6b6GB2XIYiDvFBRk=
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20240322155018-41971ffa647a h1:ZbpNtLdiJQVVNnJDorZYxDSJHIFCfiisBxFzmUB0K9A=
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20240322155018-41971ffa647a/go.mod h1:OmpJHKzWjvcqR0vacj9tugOdzAlQH7FdYCuBbf2IE+k=
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20240322155018-41971ffa647a h1:AMVOV0sQc6gHrlM9VqTVzA0rLpyoyIkRRYEitWRyhjc=
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20240322155018-41971ffa647a/go.mod h1:AEuEF0euI3R0JgxYZFCn5k621kSM89JSt+10Hulg0fY=
github.com/go-kratos/kratos/v2 v2.7.3 h1:T9MS69qk4/HkVUuHw5GS9PDVnOfzn+kxyF0CL5StqxA=
github.com/go-kratos/kratos/v2 v2.7.3/go.mod h1:CQZ7V0qyVPwrotIpS5VNNUJNzEbcyRUl5pRtxLOIvn4=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -299,8 +299,8 @@ github.com/nextmicro/gokit/timex v1.0.1 h1:MncdiGzp7eOmR5YvBkyeZ6S1Dc/e6f5d/O7a/
github.com/nextmicro/gokit/timex v1.0.1/go.mod h1:BwFB3XRGWjNU4uKirHnI9gsPL8ExM4LiUYlt0UOj/Io=
github.com/nextmicro/gokit/trace v1.0.11 h1:vb5oHeQ/veKnkRxRIGPLk2E1oWeCd1pmDOrebxvNELA=
github.com/nextmicro/gokit/trace v1.0.11/go.mod h1:CWM26HGU5jVdsx6mbH2gsVfGSB/3TbZjiKV8JYDEWkU=
github.com/nextmicro/logger v1.0.5 h1:f9+sYxn03t9wDXJ3XEbcxOU5lHpajHJI7qFyruheDc8=
github.com/nextmicro/logger v1.0.5/go.mod h1:A+cFz+/6qaatOXztSi8LDv1SuJlsL26pXbEgd0BQUxs=
github.com/nextmicro/logger v1.0.7 h1:pkWVdlkEyM8Gd2E4jBZDEokRym+SJYW7NZyqS+vQU1w=
github.com/nextmicro/logger v1.0.7/go.mod h1:hRmEO3C1TcFGY/Bh0jYC2i54ZU7ZW0WXCo9tcrcyuw8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:FfH+VrHHk6Lxt9HdVS0PXzSXFyS2NbZKXv33FYPol0A=
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b/go.mod h1:AC62GU6hc0BrNm+9RK9VSiwa/EUe1bkIeFORAMcHvJU=
Expand Down
51 changes: 45 additions & 6 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,60 @@ package env
import (
"os"
"strconv"
"sync"

"github.com/nextmicro/logger"
)

var (
envs = make(map[string]string)
envLock sync.RWMutex
)

// Env returns the value of the given environment variable.
func Env(name string) string {
envLock.RLock()
val, ok := envs[name]
envLock.RUnlock()

if ok {
return val
}

val = os.Getenv(name)
envLock.Lock()
envs[name] = val
envLock.Unlock()

return val
}

// EnvInt returns an int value of the given environment variable.
func EnvInt(name string) (int, bool) {
val := Env(name)
if len(val) == 0 {
return 0, false
}

n, err := strconv.Atoi(val)
if err != nil {
return 0, false
}

return n, true
}

// IntEnvOr returns the int value of the environment variable with name key if
// it exists and the value is an int. Otherwise, defaultValue is returned.
func IntEnvOr(key string, defaultValue int) int {
value, ok := os.LookupEnv(key)
if !ok {
val := Env(key)
if len(val) == 0 {
return defaultValue
}

intValue, err := strconv.Atoi(value)
intValue, err := strconv.Atoi(val)
if err != nil {
logger.Warn("got invalid value, number value expected.", key, value)
logger.Warn("got invalid value, number value expected.", key, val)
return defaultValue
}

Expand All @@ -27,8 +66,8 @@ func IntEnvOr(key string, defaultValue int) int {
// StringEnvOr returns the string value of the environment variable with name key if
// it exists and the value is an string. Otherwise, defaultValue is returned.
func StringEnvOr(key string, defaultValue string) string {
value, ok := os.LookupEnv(key)
if !ok {
value := Env(key)
if len(value) == 0 {
return defaultValue
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package env

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnv(t *testing.T) {
assert.True(t, len(Env("any")) == 0)
envLock.RLock()
val, ok := envs["any"]
envLock.RUnlock()
assert.True(t, len(val) == 0)
assert.True(t, ok)
assert.True(t, len(Env("any")) == 0)
}

func TestEnvInt(t *testing.T) {
val, ok := EnvInt("any")
assert.Equal(t, 0, val)
assert.False(t, ok)
t.Setenv("anyInt", "10")
val, ok = EnvInt("anyInt")
assert.Equal(t, 10, val)
assert.True(t, ok)
t.Setenv("anyString", "a")
val, ok = EnvInt("anyString")
assert.Equal(t, 0, val)
assert.False(t, ok)
}
78 changes: 78 additions & 0 deletions pkg/lang/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package lang

import (
"fmt"
"reflect"
"strconv"
)

// Placeholder is a placeholder object that can be used globally.
var Placeholder PlaceholderType

type (
// AnyType can be used to hold any type.
AnyType = any
// PlaceholderType represents a placeholder type.
PlaceholderType = struct{}
)

// Repr returns the string representation of v.
func Repr(v any) string {
if v == nil {
return ""
}

// if func (v *Type) String() string, we can't use Elem()
switch vt := v.(type) {
case fmt.Stringer:
return vt.String()
}

val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr && !val.IsNil() {
val = val.Elem()
}

return reprOfValue(val)
}

func reprOfValue(val reflect.Value) string {
switch vt := val.Interface().(type) {
case bool:
return strconv.FormatBool(vt)
case error:
return vt.Error()
case float32:
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
case float64:
return strconv.FormatFloat(vt, 'f', -1, 64)
case fmt.Stringer:
return vt.String()
case int:
return strconv.Itoa(vt)
case int8:
return strconv.Itoa(int(vt))
case int16:
return strconv.Itoa(int(vt))
case int32:
return strconv.Itoa(int(vt))
case int64:
return strconv.FormatInt(vt, 10)
case string:
return vt
case uint:
return strconv.FormatUint(uint64(vt), 10)
case uint8:
return strconv.FormatUint(uint64(vt), 10)
case uint16:
return strconv.FormatUint(uint64(vt), 10)
case uint32:
return strconv.FormatUint(uint64(vt), 10)
case uint64:
return strconv.FormatUint(vt, 10)
case []byte:
return string(vt)
default:
return fmt.Sprint(val.Interface())
}
}
Loading

0 comments on commit b2be0d8

Please sign in to comment.