From 1ccb00215ca12321be8dcbb27ba5ce9078e1ccd6 Mon Sep 17 00:00:00 2001 From: "vforfreedom96@gmail.com" Date: Tue, 18 Jun 2024 00:19:38 +0800 Subject: [PATCH] feat:add auto create database --- cmd/begonia/endpoint.go | 17 +++++++------ cmd/begonia/main.go | 11 +++++--- config/settings.yml | 9 ++++++- gateway/endpoint_test.go | 7 +----- go.mod | 2 +- go.sum | 7 ++++++ internal/middleware/auth/aksk.go | 7 +++++- internal/middleware/auth/apikey.go | 11 +++++--- internal/middleware/auth/auth_test.go | 36 ++++++++++++++++++++++++--- internal/middleware/auth/headers.go | 5 ++-- internal/middleware/auth/jwt.go | 3 +-- internal/middleware/http.go | 2 -- internal/migrate/app.go | 8 +++--- internal/migrate/operator.go | 3 ++- internal/migrate/operator_test.go | 4 +-- internal/service/base_test.go | 3 ++- 16 files changed, 94 insertions(+), 41 deletions(-) diff --git a/cmd/begonia/endpoint.go b/cmd/begonia/endpoint.go index f8764c0..45ae1bb 100644 --- a/cmd/begonia/endpoint.go +++ b/cmd/begonia/endpoint.go @@ -18,14 +18,15 @@ var accessKey string var secret string var addr string -func readInitAPP() { +func readInitAPP(env string) { homeDir, err := os.UserHomeDir() if err != nil { log.Fatalf(err.Error()) return } + path := filepath.Join(homeDir, ".begonia") - path = filepath.Join(path, "admin-app.json") + path = filepath.Join(path, fmt.Sprintf("admin-app.%s.json", env)) file, err := os.Open(path) if err != nil { log.Fatalf(err.Error()) @@ -66,8 +67,8 @@ func readInitAPP() { addr = gw.Addr } -func RegisterEndpoint(name string, endpoints []string, pbFile string, opts ...client.EndpointOption) { - readInitAPP() +func RegisterEndpoint(env,name string, endpoints []string, pbFile string, opts ...client.EndpointOption) { + readInitAPP(env) pb, err := os.ReadFile(pbFile) if err != nil { panic(err) @@ -102,8 +103,8 @@ func RegisterEndpoint(name string, endpoints []string, pbFile string, opts ...cl log.Printf("#####################Add Endpoint Success#####################") log.Printf("#####################ID:%s####################################", rsp.Id) } -func UpdateEndpoint(id string, mask []string, opts ...client.EndpointOption) { - readInitAPP() +func UpdateEndpoint(env,id string, mask []string, opts ...client.EndpointOption) { + readInitAPP(env) apiClient := client.NewEndpointAPI(addr, accessKey, secret) log.Printf("#####################Update Endpoint###########################") patch := &endpoint.EndpointSrvUpdateRequest{} @@ -119,8 +120,8 @@ func UpdateEndpoint(id string, mask []string, opts ...client.EndpointOption) { log.Printf("#####################Update Endpoint %s Success#####################", id) } -func DeleteEndpoint(id string) { - readInitAPP() +func DeleteEndpoint(env,id string) { + readInitAPP(env) apiClient := client.NewEndpointAPI(addr, accessKey, secret) log.Printf("#####################Delete Endpoint:%s#####################", id) _, err := apiClient.DeleteEndpointConfig(context.Background(), id) diff --git a/cmd/begonia/main.go b/cmd/begonia/main.go index 2305c28..8bfbf93 100644 --- a/cmd/begonia/main.go +++ b/cmd/begonia/main.go @@ -75,8 +75,9 @@ func NewEndpointDelCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { id, _ := cmd.Flags().GetString("id") + env, _ := cmd.Flags().GetString("env") - DeleteEndpoint(id) + DeleteEndpoint(env,id) }, } cmd.Flags().StringP("id", "i", "", "ID Of Your Service") @@ -95,8 +96,9 @@ func NewEndpointAddCmd() *cobra.Command { tags, _ := cmd.Flags().GetStringArray("tags") balance, _ := cmd.Flags().GetString("balance") endpoints, _ := cmd.Flags().GetStringArray("endpoint") + env,_:=cmd.Flags().GetString("env") - RegisterEndpoint(name, endpoints, desc, client.WithBalance(strings.ToUpper(balance)), client.WithTags(tags)) + RegisterEndpoint(env,name, endpoints, desc, client.WithBalance(strings.ToUpper(balance)), client.WithTags(tags)) }, } cmd = newWriteEndpointCmd(cmd) @@ -156,7 +158,8 @@ func NewEndpointUpdateCmd() *cobra.Command { options = append(options, client.WithEndpoints(meta)) mask = append(mask, "endpoints") } - UpdateEndpoint(id, mask, options...) + env,_:=cmd.Flags().GetString("env") + UpdateEndpoint(env,id, mask, options...) }, } cmd = newWriteEndpointCmd(cmd) @@ -198,7 +201,7 @@ func main() { rootCmd.AddCommand(cmd) rootCmd.AddCommand(NewBegoniaInfoCmd()) rootCmd.AddCommand(addCommonCommand(NewInitCmd())) - rootCmd.AddCommand(NewEndpointCmd()) + rootCmd.AddCommand(addCommonCommand(NewEndpointCmd())) if err := cmd.Execute(); err != nil { log.Fatalf("failed to start begonia: %v", err) } diff --git a/config/settings.yml b/config/settings.yml index 0288fe7..0a5b32e 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -23,7 +23,7 @@ mysql: port: 3306 user: "test" password: "test" - database: "begonia-test" + database: "begonia-dev" table_prefix: "begonia_" admin: name: "admin" @@ -110,6 +110,13 @@ test: secretKey: "begoniabegonia" - name: "FILE_ENGINE_LOCAL" endpoint: "/tmp/upload" + mysql: + host: "127.0.0.1" + port: 3306 + user: "test" + password: "test" + database: "begonia-test" + table_prefix: "begonia_" etcd: endpoint: prefix: "/begonia/endpoints" diff --git a/gateway/endpoint_test.go b/gateway/endpoint_test.go index eba54f2..cd9522c 100644 --- a/gateway/endpoint_test.go +++ b/gateway/endpoint_test.go @@ -14,12 +14,7 @@ func TestRequest(t *testing.T) { go example.Run(":12148") time.Sleep(time.Second * 3) c.Convey("test request", t, func() { - // request := GrpcRequestImpl{ - // FullMethodName: "helloworld.Greeter/SayHello", - // In: &v1.HelloRequest{Msg: "begonia"}, - // Out: &v1.HelloReply{}, - // Ctx: context.Background(), - // } + request := NewGrpcRequest(context.Background(), nil, nil, "helloworld.Greeter/SayHello", WithIn(&v1.HelloRequest{Msg: "begonia"}), WithOut(&v1.HelloReply{})) pool := NewGrpcConnPool("127.0.0.1:12148") endpoint := NewEndpoint(pool) diff --git a/go.mod b/go.mod index a6c722b..7261fdd 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/cockroachdb/errors v1.11.1 github.com/google/wire v0.6.0 github.com/smartystreets/goconvey v1.8.1 - github.com/spark-lence/tiga v0.0.0-20240617015203-a7e022d3fac4 + github.com/spark-lence/tiga v0.0.0-20240617161215-fd493c5e06ac github.com/spf13/cobra v1.8.0 google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 google.golang.org/grpc v1.64.0 diff --git a/go.sum b/go.sum index c344733..6660940 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,7 @@ github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoa github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk= github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -118,6 +119,7 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= @@ -161,6 +163,7 @@ github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ib github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -202,6 +205,7 @@ github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0= github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I= github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU= github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= @@ -231,6 +235,8 @@ github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9yS github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spark-lence/tiga v0.0.0-20240617015203-a7e022d3fac4 h1:HmuGbn+mhKpi5tM8iM5vpUUx4K+XZZdrPecNu8IeO8c= github.com/spark-lence/tiga v0.0.0-20240617015203-a7e022d3fac4/go.mod h1:MSL8X9t+qvpQ4Tq3vVPKncq9RJcCzF2XGEWkCuNhm6Q= +github.com/spark-lence/tiga v0.0.0-20240617161215-fd493c5e06ac h1:qGLujl2jYtkIeQYa2JEzQIQ5W3grnNcqVc2uuPV3uco= +github.com/spark-lence/tiga v0.0.0-20240617161215-fd493c5e06ac/go.mod h1:MSL8X9t+qvpQ4Tq3vVPKncq9RJcCzF2XGEWkCuNhm6Q= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= @@ -406,6 +412,7 @@ gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UD gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= diff --git a/internal/middleware/auth/aksk.go b/internal/middleware/auth/aksk.go index 1612a4a..006ea6b 100644 --- a/internal/middleware/auth/aksk.go +++ b/internal/middleware/auth/aksk.go @@ -63,8 +63,13 @@ func (a *AccessKeyAuthMiddleware) RequestBefore(ctx context.Context, info *grpc. if !ok { md = metadata.MD{} } - md.Set(gosdk.HeaderXIdentity, owner) + // md.Set(gosdk.HeaderXIdentity, owner) + md = metadata.Join(md, metadata.Pairs(gosdk.HeaderXIdentity, owner)) + ctx = metadata.NewIncomingContext(ctx, md) + // md2, _ := metadata.FromIncomingContext(ctx) + + return ctx, nil } diff --git a/internal/middleware/auth/apikey.go b/internal/middleware/auth/apikey.go index 0b9e9c9..6af3f66 100644 --- a/internal/middleware/auth/apikey.go +++ b/internal/middleware/auth/apikey.go @@ -48,7 +48,10 @@ func (a *ApiKeyAuthImpl) UnaryInterceptor(ctx context.Context, req any, info *gr if err != nil { return nil, gosdk.NewError(fmt.Errorf("query uid base on apikey get error:%w", err), int32(api.UserSvrCode_USER_APIKEY_NOT_MATCH_ERR), codes.Unauthenticated, "authorization_check") } - ctx = metadata.NewIncomingContext(ctx, metadata.Pairs(gosdk.HeaderXIdentity, identity)) + md,_:=metadata.FromIncomingContext(ctx) + + md = metadata.Join(md, metadata.Pairs(gosdk.HeaderXIdentity, identity)) + ctx = metadata.NewIncomingContext(ctx, md) return handler(ctx, req) } return nil, err @@ -86,9 +89,11 @@ func (a *ApiKeyAuthImpl) ValidateStream(ctx context.Context, req interface{}, fu if err != nil { return ctx, gosdk.NewError(fmt.Errorf("query user id base on apikey err:%w", err), int32(api.UserSvrCode_USER_APIKEY_NOT_MATCH_ERR), codes.Unauthenticated, "authorization_check") } - ctx = metadata.NewIncomingContext(ctx, metadata.Pairs(gosdk.HeaderXIdentity, identity)) + md,_:=metadata.FromIncomingContext(ctx) + + md = metadata.Join(md, metadata.Pairs(gosdk.HeaderXIdentity, identity)) headers.Set(strings.ToLower(gosdk.HeaderXIdentity), identity) - return ctx, err + return metadata.NewIncomingContext(ctx,md), err } return ctx, err } diff --git a/internal/middleware/auth/auth_test.go b/internal/middleware/auth/auth_test.go index 78727db..1abb2ae 100644 --- a/internal/middleware/auth/auth_test.go +++ b/internal/middleware/auth/auth_test.go @@ -90,8 +90,12 @@ func readInitAPP() (string, string, string) { log.Fatalf(err.Error()) return "", "", "" } + env := "dev" + if begonia.Env != "" { + env = begonia.Env + } path := filepath.Join(homeDir, ".begonia") - path = filepath.Join(path, "admin-app.json") + path = filepath.Join(path, fmt.Sprintf("admin-app.%s.json", env)) file, err := os.Open(path) if err != nil { log.Fatalf(err.Error()) @@ -241,6 +245,12 @@ func TestUnaryInterceptor(t *testing.T) { if identify:=md.Get(gosdk.HeaderXIdentity);len(identify)==0||identify[0]==""{ return nil,fmt.Errorf("no app identity") } + XAccessKey:=md.Get(gosdk.HeaderXAccessKey) + XApiKey:=md.Get(gosdk.HeaderXApiKey) + XAuthz:=md.Get("authorization") + if len(XAccessKey)==0 && len(XApiKey)==0 && len(XAuthz)==0{ + return nil,fmt.Errorf("no app auth key") + } return nil, nil } R := routers.Get() @@ -342,7 +352,15 @@ func TestStreamInterceptor(t *testing.T) { ctx = metadata.NewIncomingContext(ctx, metadata.Pairs(gosdk.HeaderXApiKey, cnf.GetAdminAPIKey())) err = mid.StreamInterceptor(&v1.Users{}, &greeterSayHelloWebsocketServer{ServerStream: &testStream{ctx: ctx}}, &grpc.StreamServerInfo{FullMethod: "/INTEGRATION.TESTSERVICE/GET"}, func(srv interface{}, ss grpc.ServerStream) error { - return nil + err:= ss.RecvMsg(srv) + md,_:=metadata.FromIncomingContext(ss.Context()) + if identify:=md.Get(gosdk.HeaderXIdentity);len(identify)==0||identify[0]==""{ + return fmt.Errorf("no app identity") + } + if xAppKey:=md.Get(gosdk.HeaderXApiKey);len(xAppKey)==0||xAppKey[0]==""{ + return fmt.Errorf("no app key") + } + return err }) c.So(err, c.ShouldBeNil) ctx = metadata.NewIncomingContext(context.Background(), metadata.Pairs(gosdk.HeaderXApiKey, "cnf.GetAdminAPIKey()")) @@ -356,7 +374,15 @@ func TestStreamInterceptor(t *testing.T) { jwt := getJWT() ctx = metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+jwt)) err = mid.StreamInterceptor(&v1.Users{}, &greeterSayHelloWebsocketServer{ServerStream: &testStream{ctx: ctx}}, &grpc.StreamServerInfo{FullMethod: "/INTEGRATION.TESTSERVICE/GET"}, func(srv interface{}, ss grpc.ServerStream) error { - return ss.RecvMsg(srv) + err:= ss.RecvMsg(srv) + md,_:=metadata.FromIncomingContext(ss.Context()) + if identify:=md.Get(gosdk.HeaderXIdentity);len(identify)==0||identify[0]==""{ + return fmt.Errorf("no app identity") + } + if xAuthorization:=md.Get("authorization");len(xAuthorization)==0||xAuthorization[0]==""{ + return fmt.Errorf("no jwt key") + } + return err }) c.So(err, c.ShouldBeNil) ctx = metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer 123")) @@ -399,6 +425,10 @@ func TestStreamInterceptor(t *testing.T) { if identify:=md.Get(gosdk.HeaderXIdentity);len(identify)==0||identify[0]==""{ return fmt.Errorf("no app identity") } + if xAccessKey:=md.Get(gosdk.HeaderXAccessKey);len(xAccessKey)==0||xAccessKey[0]==""{ + t.Logf("error metadata:%v",md) + return fmt.Errorf("no app access key") + } return err }) c.So(err, c.ShouldBeNil) diff --git a/internal/middleware/auth/headers.go b/internal/middleware/auth/headers.go index f03a1c1..b47c0f1 100644 --- a/internal/middleware/auth/headers.go +++ b/internal/middleware/auth/headers.go @@ -52,8 +52,9 @@ func (g *GrpcHeader) Release() { } func (g *GrpcHeader) Set(key, value string) { g.in.Set(key, value) - newCtx := metadata.NewIncomingContext(g.ctx, g.in) - g.ctx = newCtx + md, _ := metadata.FromIncomingContext(g.ctx) + newMd := metadata.Join(md, g.in) + g.ctx = metadata.NewIncomingContext(g.ctx, newMd) } func (g *GrpcHeader) SendHeader(key, value string) { diff --git a/internal/middleware/auth/jwt.go b/internal/middleware/auth/jwt.go index a90757c..00a6b54 100644 --- a/internal/middleware/auth/jwt.go +++ b/internal/middleware/auth/jwt.go @@ -168,8 +168,7 @@ func (a *JWTAuth) checkJWT(ctx context.Context, authorization string, rspHeader } func (a *JWTAuth) jwtValidator(ctx context.Context, headers Header) (context.Context, error) { - // 获取请求的方法名 - // 获取路由 + md, _ := metadata.FromIncomingContext(ctx) diff --git a/internal/middleware/http.go b/internal/middleware/http.go index c87c4d7..8bf0804 100644 --- a/internal/middleware/http.go +++ b/internal/middleware/http.go @@ -3,7 +3,6 @@ package middleware import ( "context" "fmt" - "log" "strings" "github.com/begonia-org/begonia/internal/pkg/routers" @@ -58,7 +57,6 @@ func (s *HttpStream) SendMsg(m interface{}) error { router := routersList.GetRouteByGrpcMethod(s.FullMethod) // 对内置服务的http响应进行格式化 if routersList.IsLocalSrv(s.FullMethod) || router.UseJsonResponse { - log.Printf("转换fullMethod:%v", s.FullMethod) rsp, _ := grpcToHttpResponse(m, nil) return s.ServerStream.SendMsg(rsp) } diff --git a/internal/migrate/app.go b/internal/migrate/app.go index f63a88f..48d78d2 100644 --- a/internal/migrate/app.go +++ b/internal/migrate/app.go @@ -23,7 +23,7 @@ type APPOperator struct { func NewAPPOperator(mysql *tiga.MySQLDao) *APPOperator { return &APPOperator{mysql: mysql} } -func dumpInitApp(app *api.Apps) error { +func dumpInitApp(app *api.Apps,env string) error { log.Print("########################################admin-app###############################") log.Printf("Init appid:%s", app.Appid) log.Printf("Init accessKey:%s", app.AccessKey) @@ -36,7 +36,7 @@ func dumpInitApp(app *api.Apps) error { if err := os.MkdirAll(path, os.ModePerm); err != nil { return err } - file, err := os.Create(filepath.Join(path, "admin-app.json")) + file, err := os.Create(filepath.Join(path, fmt.Sprintf("admin-app.%s.json",env))) if err != nil { return err } @@ -49,11 +49,11 @@ func dumpInitApp(app *api.Apps) error { log.Print("#################################################################################") return nil } -func (m *APPOperator) InitAdminAPP(owner string) (err error) { +func (m *APPOperator) InitAdminAPP(owner,env string) (err error) { app := &api.Apps{} defer func() { if app.Appid != "" { - if errInit := dumpInitApp(app); errInit != nil { + if errInit := dumpInitApp(app,env); errInit != nil { err = errInit } } diff --git a/internal/migrate/operator.go b/internal/migrate/operator.go index f1722e0..b453808 100644 --- a/internal/migrate/operator.go +++ b/internal/migrate/operator.go @@ -29,11 +29,12 @@ func (m *InitOperator) Init() error { phone := m.config.GetDefaultAdminPhone() aseKey := m.config.GetAesKey() ivKey := m.config.GetAesIv() + env:=m.config.GetEnv() uid, err := m.user.InitAdminUser(adminPasswd, aseKey, ivKey, name, email, phone) if err != nil { log.Printf("failed to init admin user: %v", err) return err } - return m.app.InitAdminAPP(uid) + return m.app.InitAdminAPP(uid,env) } diff --git a/internal/migrate/operator_test.go b/internal/migrate/operator_test.go index 7fa53b6..b41cbc2 100644 --- a/internal/migrate/operator_test.go +++ b/internal/migrate/operator_test.go @@ -126,7 +126,7 @@ func TestAppOperatorFail(t *testing.T) { } patch := gomonkey.ApplyFuncReturn(tiga.MySQLDao.First, fmt.Errorf("first failed")) defer patch.Reset() - err := operator.InitAdminAPP("test") + err := operator.InitAdminAPP("test",env) patch.Reset() c.So(err, c.ShouldNotBeNil) c.So(err.Error(), c.ShouldContainSubstring, "first failed") @@ -137,7 +137,7 @@ func TestAppOperatorFail(t *testing.T) { for _, caseV := range cases { patch2 := gomonkey.ApplyFuncReturn(caseV.patch, caseV.output...) defer patch2.Reset() - err := operator.InitAdminAPP("test") + err := operator.InitAdminAPP("test",env) patch2.Reset() c.So(err, c.ShouldNotBeNil) c.So(err.Error(), c.ShouldContainSubstring, caseV.err.Error()) diff --git a/internal/service/base_test.go b/internal/service/base_test.go index 527bf41..f1c0933 100644 --- a/internal/service/base_test.go +++ b/internal/service/base_test.go @@ -3,6 +3,7 @@ package service_test import ( "context" "encoding/json" + "fmt" "log" "os" "path/filepath" @@ -57,7 +58,7 @@ func readInitAPP() { op := internal.InitOperatorApp(config.ReadConfig(env)) _ = op.Init() path := filepath.Join(homeDir, ".begonia") - path = filepath.Join(path, "admin-app.json") + path = filepath.Join(path, fmt.Sprintf("admin-app.%s.json",env)) file, err := os.Open(path) if err != nil {