Skip to content

Commit 0b5649d

Browse files
committed
fix
1 parent a66d563 commit 0b5649d

File tree

12 files changed

+46
-71
lines changed

12 files changed

+46
-71
lines changed

clients/grpcc/grpcc_builder/middleware.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/pubgo/lava/clients/grpcc/grpcc_config"
1414
"github.com/pubgo/lava/middleware"
15+
"github.com/pubgo/lava/pkg/grpcutil"
1516
"github.com/pubgo/lava/pkg/utils"
1617
)
1718

@@ -62,9 +63,9 @@ func unaryInterceptor(middlewares []middleware.Middleware) grpc.UnaryClientInter
6263

6364
// get content type
6465
ct := utils.FirstFnNotEmpty(func() string {
65-
return middleware.HeaderGet(md, "content-type")
66+
return grpcutil.HeaderGet(md, "content-type")
6667
}, func() string {
67-
return middleware.HeaderGet(md, "x-content-type")
68+
return grpcutil.HeaderGet(md, "x-content-type")
6869
}, func() string {
6970
return grpcc_config.DefaultContentType
7071
})
@@ -135,9 +136,9 @@ func streamInterceptor(middlewares []middleware.Middleware) grpc.StreamClientInt
135136

136137
// get content type
137138
ct := utils.FirstFnNotEmpty(func() string {
138-
return middleware.HeaderGet(md, "content-type")
139+
return grpcutil.HeaderGet(md, "content-type")
139140
}, func() string {
140-
return middleware.HeaderGet(md, "x-content-type")
141+
return grpcutil.HeaderGet(md, "x-content-type")
141142
}, func() string {
142143
return grpcc_config.DefaultContentType
143144
})

config/builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package config
22

33
import (
4-
"github.com/pubgo/lava/inject"
54
"github.com/spf13/viper"
65
"go.uber.org/fx"
6+
7+
"github.com/pubgo/lava/inject"
78
)
89

910
var conf Config

config/builder_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func TestName(t *testing.T) {
1717
assert.So(c, should.NotBeNil)
1818

1919
_ = os.Setenv(runtime.Project+"_123", "app.name=hello")
20+
for _, ff := range os.Environ() {
21+
println(ff)
22+
}
2023
c = newCfg()
2124
assert.So(c.GetString("app.name"), should.Equal, "hello")
2225
assert.So(c.GetString("app.home"), should.Equal, c.GetString("app.project"))

config/util.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ import (
66
"path/filepath"
77
"strings"
88

9+
"github.com/iancoleman/strcase"
910
"github.com/pubgo/xerror"
1011
"github.com/spf13/viper"
1112

1213
"github.com/pubgo/lava/consts"
1314
)
1415

15-
const _resIdKey = "name"
16+
const resKey = "name"
1617

1718
func getResId(m map[string]interface{}) string {
1819
if m == nil {
1920
return consts.KeyDefault
2021
}
2122

22-
var val, ok = m[_resIdKey]
23+
var val, ok = m[resKey]
2324
if !ok || val == nil {
2425
return consts.KeyDefault
2526
}
@@ -50,8 +51,8 @@ func strMap(strList []string, fn func(str string) string) []string {
5051
}
5152

5253
func loadEnv(envPrefix string, v *viper.Viper) {
53-
var r = strings.NewReplacer("-", "_", ".", "_", "__", "_", "/", "_")
54-
envPrefix = strings.ReplaceAll(r.Replace(envPrefix)+"_", "__", "_")
54+
var r = strings.NewReplacer("-", "_", ".", "_", "/", "_")
55+
envPrefix = strings.ReplaceAll(r.Replace(strcase.ToSnake(envPrefix))+"_", "__", "_")
5556

5657
for _, env := range os.Environ() {
5758
if !strings.HasPrefix(env, envPrefix) {

example/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
WORKDIR=`pwd`
22
Domain=lava
3-
Project=test-grpc
43
VersionBase=github.com/pubgo/lava
54
Tag=$(shell git describe --abbrev=0 --tags)
65
Version=$(shell git tag --sort=committerdate | tail -n 1)
@@ -14,7 +13,6 @@ LDFLAGS=-ldflags " \
1413
-X '${VersionBase}/version.Version=${Version}' \
1514
-X '${VersionBase}/version.Tag=${Tag}' \
1615
-X '${VersionBase}/version.Domain=${Domain}' \
17-
-X '${VersionBase}/version.Project=${Project}' \
1816
-X '${VersionBase}/version.Data=hello' \
1917
"
2018

logging/log_middleware/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/pubgo/lava/version"
2222
)
2323

24-
const Name = "log-record"
24+
const Name = "accesslog"
2525

2626
var log = logging.Component(Name)
2727

pkg/env/env.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package env
22

33
import (
4+
"fmt"
45
"os"
56
"strconv"
67
"strings"
@@ -18,6 +19,15 @@ func Get(names ...string) string {
1819
return val
1920
}
2021

22+
func MustGet(names ...string) string {
23+
var val string
24+
GetWith(&val, names...)
25+
if val == "" {
26+
panic(fmt.Sprintf("env value is null in %v, all:%v", names, os.Environ()))
27+
}
28+
return val
29+
}
30+
2131
func GetWith(val *string, names ...string) {
2232
for _, name := range names {
2333
env, ok := Lookup(name)
@@ -71,7 +81,7 @@ func GetFloatVal(val *float64, names ...string) {
7181
}
7282

7383
func Lookup(key string) (string, bool) {
74-
return os.LookupEnv(key)
84+
return os.LookupEnv(strings.ToUpper(key))
7585
}
7686

7787
func UnSetenv(key string) error {

middleware/header.go renamed to pkg/grpcutil/header.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package middleware
1+
package grpcutil
22

33
import (
44
"google.golang.org/grpc/metadata"

runtime/flags.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

runtime/runtime.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package runtime
33
import (
44
"io/ioutil"
55
"os"
6+
"strconv"
67
"strings"
78
"syscall"
89

910
"github.com/denisbrodbeck/machineid"
1011
"github.com/google/uuid"
11-
dir "github.com/mitchellh/go-homedir"
1212
"github.com/pubgo/xerror"
13-
"k8s.io/client-go/util/homedir"
1413

14+
"github.com/pubgo/lava/pkg/env"
1515
"github.com/pubgo/lava/pkg/utils"
1616
"github.com/pubgo/lava/version"
1717
)
@@ -22,7 +22,7 @@ var (
2222
Block = true
2323
Trace = false
2424
Addr = ":8080"
25-
Project = version.Project
25+
Project = ""
2626
Level = "debug"
2727
Mode = RunModeLocal
2828

@@ -60,15 +60,16 @@ var (
6060
return ""
6161
},
6262
)
63-
64-
// Homedir the home directory for the current user
65-
Homedir = utils.FirstFnNotEmpty(
66-
homedir.HomeDir,
67-
func() string {
68-
var h, err = dir.Dir()
69-
xerror.Exit(err)
70-
return h
71-
},
72-
func() string { return "." },
73-
)
7463
)
64+
65+
func init() {
66+
Project = env.MustGet("lava_project", "app_name", "project_name", "service_name")
67+
mode := env.Get("lava_mode", "app_mode")
68+
if mode != "" {
69+
var i, err = strconv.Atoi(mode)
70+
xerror.Panic(err)
71+
72+
Mode = RunMode(i)
73+
xerror.Assert(Mode.String() == "", "unknown mode, mode=%s", mode)
74+
}
75+
}

0 commit comments

Comments
 (0)