From d2d5dc1c1d6374d484d3bec611fee6a98537637b Mon Sep 17 00:00:00 2001 From: Imran Ismail Date: Thu, 14 Apr 2022 07:46:27 +0800 Subject: [PATCH] Setup build flags --- .github/workflows/goreleaser.yaml | 2 ++ .goreleaser.yml | 4 ++- body/json_resource.go | 3 ++- cmd/root.go | 41 ++++--------------------------- logger/log.go | 39 +++++++++++++++++++++++++++++ meta/meta.go | 3 +++ proxy/proxy.go | 17 +++++++++---- 7 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 logger/log.go create mode 100644 meta/meta.go diff --git a/.github/workflows/goreleaser.yaml b/.github/workflows/goreleaser.yaml index ad6615d..6721d5c 100644 --- a/.github/workflows/goreleaser.yaml +++ b/.github/workflows/goreleaser.yaml @@ -33,6 +33,7 @@ jobs: args: release --rm-dist --snapshot env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RELEASE_VERSION: ${{ github.ref_name }} - name: Release if: startsWith(github.ref, 'refs/tags/v') uses: goreleaser/goreleaser-action@v2 @@ -41,3 +42,4 @@ jobs: args: release --rm-dist env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RELEASE_VERSION: ${{ github.ref_name }} diff --git a/.goreleaser.yml b/.goreleaser.yml index 5ebfbc9..2d4c174 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -7,7 +7,9 @@ before: # you may remove this if you don't need go generate - go generate ./... builds: - - env: + - ldflags: + - -s -w -X "meta.Version={{ .Env.RELEASE_VERSION }}" + env: - CGO_ENABLED=0 goos: - linux diff --git a/body/json_resource.go b/body/json_resource.go index 3f9d639..8089114 100644 --- a/body/json_resource.go +++ b/body/json_resource.go @@ -15,6 +15,7 @@ import ( "github.com/google/martian/v3/verify" "github.com/imranismail/bff/bffurl" "github.com/imranismail/bff/jsonpatch" + "github.com/imranismail/bff/meta" ) var httpClient = &http.Client{ @@ -115,7 +116,7 @@ func (m *JSONResource) FetchResource(downstreamReq *http.Request) (martian.Respo return nil, err } - upstreamReq.Header.Set("User-Agent", "bff/v0.x.x") + upstreamReq.Header.Set("User-Agent", fmt.Sprintf("bff/%s", meta.Version)) upstreamReq.Header.Set("Accept", "application/json") for _, allowed := range m.allowedHeaders { diff --git a/cmd/root.go b/cmd/root.go index 4973e3e..6174023 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,17 +6,17 @@ import ( "path" "strings" - mlog "github.com/google/martian/v3/log" + "github.com/imranismail/bff/meta" "github.com/imranismail/bff/proxy" "github.com/spf13/cobra" "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/adrg/xdg" "github.com/spf13/viper" ) +var log = zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger() var cfgFile string var cfgPath = path.Join(xdg.ConfigHome, "bff") @@ -27,7 +27,8 @@ var rootCmd = &cobra.Command{ Long: ` An API-aware proxy that is cabaple of routing, filtering, verifying and modifing HTTP request and response.`, - Run: proxy.Serve, + Run: proxy.Serve, + Version: meta.Version, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -70,34 +71,6 @@ func init() { } } -type Logger struct { - zlog zerolog.Logger -} - -func (logger *Logger) Infof(format string, args ...interface{}) { - log.Info().Msgf(format, args...) -} - -func (logger *Logger) Debugf(format string, args ...interface{}) { - log.Debug().Msgf(format, args...) -} - -func (logger *Logger) Errorf(format string, args ...interface{}) { - log.Error().Msgf(format, args...) -} - -func NewLogger() Logger { - level, err := zerolog.ParseLevel(viper.GetString("verbosity")) - - if err != nil { - log.Fatal().Msgf("Failed to parse verbosity: %v", err) - } - - zerolog.SetGlobalLevel(level) - - return Logger{} -} - // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { @@ -114,16 +87,12 @@ func initConfig() { viper.AutomaticEnv() err := viper.ReadInConfig() - logger := NewLogger() - mlog.SetLogger(&logger) // If a config file is found, read it in if err == nil { log.Info().Msgf("Using config file: %v", viper.ConfigFileUsed()) + viper.WatchConfig() } - - // watch config file for changes - viper.WatchConfig() } func hasPipedInput() bool { diff --git a/logger/log.go b/logger/log.go new file mode 100644 index 0000000..df9d01d --- /dev/null +++ b/logger/log.go @@ -0,0 +1,39 @@ +package logger + +import ( + "github.com/rs/zerolog" + "github.com/spf13/viper" +) + +type Logger struct { + zlog zerolog.Logger +} + +func (logger *Logger) Infof(format string, args ...interface{}) { + logger.zlog.Info().Msgf(format, args...) +} + +func (logger *Logger) Debugf(format string, args ...interface{}) { + logger.zlog.Debug().Msgf(format, args...) +} + +func (logger *Logger) Errorf(format string, args ...interface{}) { + logger.zlog.Error().Msgf(format, args...) +} + +func (logger *Logger) Configure() { + level, err := zerolog.ParseLevel(viper.GetString("verbosity")) + + if err != nil { + logger.zlog.Fatal().Msgf("Failed to parse verbosity: %v", err) + } + + logger.zlog = logger.zlog.Level(level) +} + +func NewLogger() Logger { + zlog := zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger() + logger := Logger{zlog} + logger.Configure() + return logger +} diff --git a/meta/meta.go b/meta/meta.go new file mode 100644 index 0000000..6a4288a --- /dev/null +++ b/meta/meta.go @@ -0,0 +1,3 @@ +package meta + +var Version = "development" diff --git a/proxy/proxy.go b/proxy/proxy.go index 2627477..071f2da 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -42,10 +42,14 @@ import ( _ "github.com/imranismail/bff/bffstatus" _ "github.com/imranismail/bff/bffurl" _ "github.com/imranismail/bff/body" + "github.com/imranismail/bff/logger" ) // Serve start the webserver func Serve(cmd *cobra.Command, args []string) { + logger := logger.NewLogger() + log.SetLogger(&logger) + proxy := martian.NewProxy() defer proxy.Close() @@ -70,6 +74,8 @@ func Serve(cmd *cobra.Command, args []string) { viper.OnConfigChange(func(evt fsnotify.Event) { log.Infof("proxy.Serve: Reconfiguring: %v", evt.Name) configureProxy(proxy) + log.Infof("logger: Reconfiguring: %v", evt.Name) + logger.Configure() }) listener, err := net.Listen("tcp", fmt.Sprintf(":%s", viper.GetString("port"))) @@ -92,19 +98,20 @@ func Serve(cmd *cobra.Command, args []string) { } func configureProxy(proxy *martian.Proxy) { - outer, inner := httpspec.NewStack("martian") + outer, inner := httpspec.NewStack("bff") main := NewErrorBoundary() + proxy.SetRequestModifier(main) + proxy.SetResponseModifier(main) + main.SetRequestModifier(outer) main.SetResponseModifier(outer) main.SetRequestVerifier(outer) main.SetResponseVerifier(outer) - proxy.SetRequestModifier(main) - proxy.SetResponseModifier(main) - logger := martianlog.NewLogger() - logger.SetDecode(true) + logger.SetDecode(false) + logger.SetHeadersOnly(true) outer.AddRequestModifier(logger) outer.AddResponseModifier(logger)