Skip to content

Commit

Permalink
Merge pull request #11 from imranismail/chore/build-flags
Browse files Browse the repository at this point in the history
Setup build flags
  • Loading branch information
imranismail authored Apr 13, 2022
2 parents 46ed32a + d2d5dc1 commit 6d2a6d2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,3 +42,4 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_VERSION: ${{ github.ref_name }}
4 changes: 3 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion body/json_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
41 changes: 5 additions & 36 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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.
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions logger/log.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions meta/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package meta

var Version = "development"
17 changes: 12 additions & 5 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")))
Expand All @@ -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)
Expand Down

0 comments on commit 6d2a6d2

Please sign in to comment.