This is an automated email from the ASF dual-hosted git repository. alexstocks pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu.git
The following commit(s) were added to refs/heads/develop by this push: new 4ff167d7 1.0.0 (#602) 4ff167d7 is described below commit 4ff167d74bb0eabe31bde375a29464774d5311f5 Author: Mark4z <36187602+mar...@users.noreply.github.com> AuthorDate: Wed Dec 20 10:56:17 2023 +0800 1.0.0 (#602) * V1.0.0 Changes (#588) * [feature] cors support options request (#591) * V1.0.0 Changes * [feature] cors support options request * Move Pixiu cmd files in /cmd/pixiu to pkg/cmd (#596) * remove unused types JTypeMapper check & support default types. (#597) --- cmd/pixiu/gateway.go | 70 ------------ cmd/pixiu/pixiu.go | 146 +------------------------ go.mod | 1 + pixiu/pkg/client/dubbo/option.go | 21 ++-- pixiu/pkg/client/dubbo/option_test.go | 7 +- {cmd/pixiu => pixiu/pkg/cmd}/deployer.go | 2 +- cmd/pixiu/pixiu.go => pixiu/pkg/cmd/gateway.go | 76 ++++++------- {cmd/pixiu => pixiu/pkg/cmd}/sidecar.go | 12 +- pixiu/pkg/filter/failinject/config.go | 4 +- pixiu/pkg/filter/failinject/filter.go | 2 + 10 files changed, 69 insertions(+), 272 deletions(-) diff --git a/cmd/pixiu/gateway.go b/cmd/pixiu/gateway.go deleted file mode 100644 index a7053dc5..00000000 --- a/cmd/pixiu/gateway.go +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "os" -) - -import ( - "github.com/spf13/cobra" -) - -import ( - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" -) - -var ( - gatewayCmd = &cobra.Command{ - Use: "gateway", - Short: "Run dubbo go pixiu in gateway mode", - } - - startGatewayCmd = &cobra.Command{ - Use: "start", - Short: "Start gateway", - Version: Version, - PreRun: func(cmd *cobra.Command, args []string) { - initDefaultValue() - - err := deploy.initialize() - if err != nil { - panic(err) - } - }, - Run: func(cmd *cobra.Command, args []string) { - - err := deploy.start() - if err != nil { - panic(err) - } - }, - } -) - -// init Init startCmd -func init() { - startGatewayCmd.PersistentFlags().StringVarP(&configPath, constant.ConfigPathKey, "c", os.Getenv(constant.EnvDubbogoPixiuConfig), "Load configuration from `FILE`") - startGatewayCmd.PersistentFlags().StringVarP(&apiConfigPath, constant.ApiConfigPathKey, "a", os.Getenv(constant.EnvDubbogoPixiuApiConfig), "Load api configuration from `FILE`") - startGatewayCmd.PersistentFlags().StringVarP(&logConfigPath, constant.LogConfigPathKey, "g", os.Getenv(constant.EnvDubbogoPixiuLogConfig), "Load log configuration from `FILE`") - startGatewayCmd.PersistentFlags().StringVarP(&logLevel, constant.LogLevelKey, "l", os.Getenv(constant.EnvDubbogoPixiuLogLevel), "dubbogo pixiu log level, trace|debug|info|warning|error|critical") - startGatewayCmd.PersistentFlags().StringVarP(&limitCpus, constant.LimitCpusKey, "m", os.Getenv(constant.EnvDubbogoPixiuLimitCpus), "dubbogo pixiu schedule threads count") - startGatewayCmd.PersistentFlags().StringVarP(&logFormat, constant.LogFormatKey, "f", os.Getenv(constant.EnvDubbogoPixiuLogFormat), "dubbogo pixiu log format, currently useless") - - gatewayCmd.AddCommand(startGatewayCmd) -} diff --git a/cmd/pixiu/pixiu.go b/cmd/pixiu/pixiu.go index 94020052..0bb58aed 100644 --- a/cmd/pixiu/pixiu.go +++ b/cmd/pixiu/pixiu.go @@ -18,9 +18,7 @@ package main import ( - "fmt" _ "net/http/pprof" - "runtime" "strconv" "time" ) @@ -30,45 +28,15 @@ import ( ) import ( - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" - pxruntime "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/runtime" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/config" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/logger" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/model" + "github.com/apache/dubbo-go-pixiu/pixiu/pkg/cmd" _ "github.com/apache/dubbo-go-pixiu/pixiu/pkg/pluginregistry" - "github.com/apache/dubbo-go-pixiu/pixiu/pkg/server" ) -var ( +const ( // Version pixiu version - Version = "0.6.0" - - flagToLogLevel = map[string]string{ - "trace": "TRACE", - "debug": "DEBUG", - "info": "INFO", - "warning": "WARN", - "error": "ERROR", - "critical": "FATAL", - } - - configPath string - apiConfigPath string - logConfigPath string - logLevel string - - // CURRENTLY USELESS - logFormat string - - limitCpus string - - // Currently default set to false, wait for up coming support - initFromRemote = false + Version = "1.0.0" ) -// deploy server deployment -var deploy = &DefaultDeployer{} - // main pixiu run method func main() { app := getRootCmd() @@ -88,112 +56,8 @@ func getRootCmd() *cobra.Command { Version: Version, } - rootCmd.AddCommand(gatewayCmd) - rootCmd.AddCommand(sideCarCmd) + rootCmd.AddCommand(cmd.GatewayCmd) + rootCmd.AddCommand(cmd.SideCarCmd) return rootCmd } - -type DefaultDeployer struct { - bootstrap *model.Bootstrap - configManger *config.ConfigManager -} - -func (d *DefaultDeployer) initialize() error { - - err := initLog() - if err != nil { - logger.Warnf("[startGatewayCmd] failed to init logger, %s", err.Error()) - } - - // load Bootstrap config - d.bootstrap = d.configManger.LoadBootConfig(configPath) - if err != nil { - panic(fmt.Errorf("[startGatewayCmd] failed to get api meta config, %s", err.Error())) - } - - err = initLimitCpus() - if err != nil { - logger.Errorf("[startCmd] failed to get limit cpu number, %s", err.Error()) - } - - return err -} - -func (d *DefaultDeployer) start() error { - server.Start(d.bootstrap) - return nil -} - -func (d *DefaultDeployer) stop() error { - //TODO implement me - panic("implement me") -} - -// initDefaultValue If not set both in args and env, set default values -func initDefaultValue() { - if configPath == "" { - configPath = constant.DefaultConfigPath - } - - if apiConfigPath == "" { - apiConfigPath = constant.DefaultApiConfigPath - } - - if logConfigPath == "" { - logConfigPath = constant.DefaultLogConfigPath - } - - if logLevel == "" { - logLevel = constant.DefaultLogLevel - } - - if limitCpus == "" { - limitCpus = constant.DefaultLimitCpus - } - - if logFormat == "" { - logFormat = constant.DefaultLogFormat - } -} - -// initLog -func initLog() error { - err := logger.InitLog(logConfigPath) - if err != nil { - // cause `logger.InitLog` already handle init failed, so just use logger to log - return err - } - - if level, ok := flagToLogLevel[logLevel]; ok { - logger.SetLoggerLevel(level) - } else { - logger.SetLoggerLevel(flagToLogLevel[constant.DefaultLogLevel]) - return fmt.Errorf("logLevel is invalid, set log level to default: %s", constant.DefaultLogLevel) - } - return nil -} - -// initApiConfig return value of the bool is for the judgment of whether is a api meta data error, a kind of silly (?) -func initApiConfig() (*model.Bootstrap, error) { - bootstrap := config.Load(configPath) - return bootstrap, nil -} - -func initLimitCpus() error { - limitCpuNumberFromEnv, err := strconv.ParseInt(limitCpus, 10, 64) - if err != nil { - return err - } - limitCpuNumber := int(limitCpuNumberFromEnv) - if limitCpuNumber <= 0 { - limitCpuNumber = pxruntime.GetCPUNum() - } - runtime.GOMAXPROCS(limitCpuNumber) - logger.Infof("GOMAXPROCS set to %v", limitCpuNumber) - return nil -} - -func init() { - deploy.configManger = config.NewConfigManger() -} diff --git a/go.mod b/go.mod index 211d1582..324297c5 100644 --- a/go.mod +++ b/go.mod @@ -328,3 +328,4 @@ require ( ) replace google.golang.org/protobuf v1.28.1 => google.golang.org/protobuf v1.28.0 + diff --git a/pixiu/pkg/client/dubbo/option.go b/pixiu/pkg/client/dubbo/option.go index 41980f73..1d1aae2e 100644 --- a/pixiu/pkg/client/dubbo/option.go +++ b/pixiu/pkg/client/dubbo/option.go @@ -186,24 +186,19 @@ type paramTypesOpt struct{} // Action for paramTypesOpt override the other param types mapping/config. // The val must be string(e.g. "int, object"), and will then assign to the target.(dubboTarget).Types func (opt *paramTypesOpt) Action(target, val interface{}) error { - v, ok := val.(string) - if !ok { - return errors.New("The val type must be string") - } - types := strings.Split(v, ",") dubboTarget, ok := target.(*dubboTarget) if !ok { return errors.New("Target is not dubboTarget in target parameter") } - for i := range types { - trimType := strings.TrimSpace(types[i]) - if len(trimType) == 0 { - continue - } - if _, ok = constant.JTypeMapper[trimType]; !ok { - return errors.Errorf("Types invalid %s", trimType) + // empty types for func like func() + types := make([]string, 0) + if v, ok := val.(string); ok { + if len(v) > 0 { + types = strings.Split(v, ",") + for i := range types { + types[i] = strings.TrimSpace(types[i]) + } } - types[i] = trimType } dubboTarget.Types = types return nil diff --git a/pixiu/pkg/client/dubbo/option_test.go b/pixiu/pkg/client/dubbo/option_test.go index e8762e59..179ef1b5 100644 --- a/pixiu/pkg/client/dubbo/option_test.go +++ b/pixiu/pkg/client/dubbo/option_test.go @@ -84,14 +84,15 @@ func TestParamTypesOptAction(t *testing.T) { assert.Equal(t, "string", target.Types[1]) err = opt.Action(target, "object,whatsoever") - assert.EqualError(t, err, "Types invalid whatsoever") + assert.Nil(t, err) err = opt.Action("target", []string{}) - assert.EqualError(t, err, "The val type must be string") + assert.EqualError(t, err, "Target is not dubboTarget in target parameter") err = opt.Action(target, "object,") assert.Nil(t, err) assert.Equal(t, "object", target.Types[0]) - err = opt.Action(target, "object") + + err = opt.Action(target, "object ") assert.Nil(t, err) assert.Equal(t, "object", target.Types[0]) } diff --git a/cmd/pixiu/deployer.go b/pixiu/pkg/cmd/deployer.go similarity index 98% rename from cmd/pixiu/deployer.go rename to pixiu/pkg/cmd/deployer.go index a21bcddc..ac166c43 100644 --- a/cmd/pixiu/deployer.go +++ b/pixiu/pkg/cmd/deployer.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package main +package cmd type Deployer interface { initialize() error diff --git a/cmd/pixiu/pixiu.go b/pixiu/pkg/cmd/gateway.go similarity index 70% copy from cmd/pixiu/pixiu.go copy to pixiu/pkg/cmd/gateway.go index 94020052..111a1d96 100644 --- a/cmd/pixiu/pixiu.go +++ b/pixiu/pkg/cmd/gateway.go @@ -15,14 +15,13 @@ * limitations under the License. */ -package main +package cmd import ( "fmt" - _ "net/http/pprof" + "os" "runtime" "strconv" - "time" ) import ( @@ -35,14 +34,10 @@ import ( "github.com/apache/dubbo-go-pixiu/pixiu/pkg/config" "github.com/apache/dubbo-go-pixiu/pixiu/pkg/logger" "github.com/apache/dubbo-go-pixiu/pixiu/pkg/model" - _ "github.com/apache/dubbo-go-pixiu/pixiu/pkg/pluginregistry" "github.com/apache/dubbo-go-pixiu/pixiu/pkg/server" ) var ( - // Version pixiu version - Version = "0.6.0" - flagToLogLevel = map[string]string{ "trace": "TRACE", "debug": "DEBUG", @@ -66,32 +61,47 @@ var ( initFromRemote = false ) -// deploy server deployment -var deploy = &DefaultDeployer{} - -// main pixiu run method -func main() { - app := getRootCmd() - - // ignore error so we don't exit non-zero and break gfmrun README example tests - _ = app.Execute() -} +var ( + GatewayCmd = &cobra.Command{ + Use: "gateway", + Short: "Run dubbo go pixiu in gateway mode", + } -func getRootCmd() *cobra.Command { - rootCmd := &cobra.Command{ - Use: "dubbogo pixiu", - Short: "Dubbogo pixiu is a lightweight gateway.", - Long: "dubbo-go-pixiu is a gateway that mainly focuses on providing gateway solution to your Dubbo and RESTful \n" + - "services. It supports HTTP-to-Dubbo and HTTP-to-HTTP proxy and more protocols will be supported in the near \n" + - "future. \n" + - "(c) " + strconv.Itoa(time.Now().Year()) + " Dubbogo", - Version: Version, + deploy = &DefaultDeployer{ + configManger: config.NewConfigManger(), } - rootCmd.AddCommand(gatewayCmd) - rootCmd.AddCommand(sideCarCmd) + startGatewayCmd = &cobra.Command{ + Use: "start", + Short: "Start gateway", + PreRun: func(cmd *cobra.Command, args []string) { + initDefaultValue() + + err := deploy.initialize() + if err != nil { + panic(err) + } + }, + Run: func(cmd *cobra.Command, args []string) { + + err := deploy.start() + if err != nil { + panic(err) + } + }, + } +) - return rootCmd +// init Init startCmd +func init() { + startGatewayCmd.PersistentFlags().StringVarP(&configPath, constant.ConfigPathKey, "c", os.Getenv(constant.EnvDubbogoPixiuConfig), "Load configuration from `FILE`") + startGatewayCmd.PersistentFlags().StringVarP(&apiConfigPath, constant.ApiConfigPathKey, "a", os.Getenv(constant.EnvDubbogoPixiuApiConfig), "Load api configuration from `FILE`") + startGatewayCmd.PersistentFlags().StringVarP(&logConfigPath, constant.LogConfigPathKey, "g", os.Getenv(constant.EnvDubbogoPixiuLogConfig), "Load log configuration from `FILE`") + startGatewayCmd.PersistentFlags().StringVarP(&logLevel, constant.LogLevelKey, "l", os.Getenv(constant.EnvDubbogoPixiuLogLevel), "dubbogo pixiu log level, trace|debug|info|warning|error|critical") + startGatewayCmd.PersistentFlags().StringVarP(&limitCpus, constant.LimitCpusKey, "m", os.Getenv(constant.EnvDubbogoPixiuLimitCpus), "dubbogo pixiu schedule threads count") + startGatewayCmd.PersistentFlags().StringVarP(&logFormat, constant.LogFormatKey, "f", os.Getenv(constant.EnvDubbogoPixiuLogFormat), "dubbogo pixiu log format, currently useless") + + GatewayCmd.AddCommand(startGatewayCmd) } type DefaultDeployer struct { @@ -100,7 +110,6 @@ type DefaultDeployer struct { } func (d *DefaultDeployer) initialize() error { - err := initLog() if err != nil { logger.Warnf("[startGatewayCmd] failed to init logger, %s", err.Error()) @@ -108,9 +117,6 @@ func (d *DefaultDeployer) initialize() error { // load Bootstrap config d.bootstrap = d.configManger.LoadBootConfig(configPath) - if err != nil { - panic(fmt.Errorf("[startGatewayCmd] failed to get api meta config, %s", err.Error())) - } err = initLimitCpus() if err != nil { @@ -193,7 +199,3 @@ func initLimitCpus() error { logger.Infof("GOMAXPROCS set to %v", limitCpuNumber) return nil } - -func init() { - deploy.configManger = config.NewConfigManger() -} diff --git a/cmd/pixiu/sidecar.go b/pixiu/pkg/cmd/sidecar.go similarity index 93% rename from cmd/pixiu/sidecar.go rename to pixiu/pkg/cmd/sidecar.go index 68d739ec..e0260c2c 100644 --- a/cmd/pixiu/sidecar.go +++ b/pixiu/pkg/cmd/sidecar.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package main +package cmd import ( "fmt" @@ -25,8 +25,12 @@ import ( "github.com/spf13/cobra" ) +func init() { + SideCarCmd.AddCommand(startSideCarCmd) +} + var ( - sideCarCmd = &cobra.Command{ + SideCarCmd = &cobra.Command{ Use: "sidecar", Short: "Run dubbo go pixiu in sidecar mode (implement in the future)", } @@ -39,7 +43,3 @@ var ( }, } ) - -func init() { - sideCarCmd.AddCommand(startSideCarCmd) -} diff --git a/pixiu/pkg/filter/failinject/config.go b/pixiu/pkg/filter/failinject/config.go index d6952fa8..cff0fce1 100644 --- a/pixiu/pkg/filter/failinject/config.go +++ b/pixiu/pkg/filter/failinject/config.go @@ -17,7 +17,9 @@ package failinject -import "time" +import ( + "time" +) type URI string diff --git a/pixiu/pkg/filter/failinject/filter.go b/pixiu/pkg/filter/failinject/filter.go index 8a1dcc77..aff5c9cc 100644 --- a/pixiu/pkg/filter/failinject/filter.go +++ b/pixiu/pkg/filter/failinject/filter.go @@ -20,7 +20,9 @@ package failinject import ( "math/rand" "time" +) +import ( "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant" "github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/extension/filter" contextHttp "github.com/apache/dubbo-go-pixiu/pixiu/pkg/context/http"