From c3d1d14d238965799c8024e15ae3c36ca1bcbea6 Mon Sep 17 00:00:00 2001 From: marpio Date: Thu, 14 Feb 2019 20:32:53 +0100 Subject: [PATCH] Use default conf values if file not found (#1022) * default conf if no file provided * move to the config pkg * rm default config path * rm log --- Makefile | 4 +-- cmd/proxy/main.go | 11 +++----- pkg/config/config.go | 26 +++++++++++++++++ pkg/log/buffalo_formatter.go | 55 ------------------------------------ 4 files changed, 32 insertions(+), 64 deletions(-) delete mode 100644 pkg/log/buffalo_formatter.go diff --git a/Makefile b/Makefile index 99c786d8..6bdca9b9 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ build-ver: GO111MODULE=on CGO_ENABLED=0 go build -mod=vendor -ldflags "-X github.com/gomods/athens/pkg/build.version=$(VERSION) -X github.com/gomods/athens/pkg/build.buildDate=$(DATE)" -o athens ./cmd/proxy .PHONY: run -run: build - ./athens +run: + cd ./cmd/proxy && go run . -config_file ../../config.dev.toml .PHONY: docs docs: diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 409344a9..951e09bb 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -8,7 +8,6 @@ import ( "net/http" "os" "os/signal" - "path/filepath" "time" "github.com/gomods/athens/cmd/proxy/actions" @@ -17,7 +16,7 @@ import ( ) var ( - configFile = flag.String("config_file", filepath.Join("..", "..", "config.dev.toml"), "The path to the config file") + configFile = flag.String("config_file", "", "The path to the config file") version = flag.Bool("version", false, "Print version information and exit") ) @@ -27,13 +26,11 @@ func main() { fmt.Println(build.String()) os.Exit(0) } - if configFile == nil { - log.Fatal("Invalid config file path provided") - } - conf, err := config.ParseConfigFile(*configFile) + conf, err := config.Load(*configFile) if err != nil { - log.Fatal(err) + log.Fatalf("could not load config file: %v", err) } + handler, err := actions.App(conf) if err != nil { log.Fatal(err) diff --git a/pkg/config/config.go b/pkg/config/config.go index 488a0716..adac4e88 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "log" "os" "path/filepath" "runtime" @@ -41,6 +42,31 @@ type Config struct { Storage *StorageConfig } +// Load loads the config from a file. +// If file is not present returns default config +func Load(configFile string) (*Config, error) { + if configFile == "" { + log.Print("config file not provided - using default settings") + return createDefault(), nil + } + return ParseConfigFile(configFile) +} + +func createDefault() *Config { + return &Config{ + GoBinary: "go", + GoEnv: "development", + GoGetWorkers: 30, + ProtocolWorkers: 30, + LogLevel: "debug", + CloudRuntime: "none", + TimeoutConf: TimeoutConf{Timeout: 300}, + StorageType: "memory", + Port: ":3000", + GlobalEndpoint: "http://localhost:3001", + } +} + // BasicAuth returns BasicAuthUser and BasicAuthPassword // and ok if neither of them are empty func (c *Config) BasicAuth() (user, pass string, ok bool) { diff --git a/pkg/log/buffalo_formatter.go b/pkg/log/buffalo_formatter.go deleted file mode 100644 index d2a563e7..00000000 --- a/pkg/log/buffalo_formatter.go +++ /dev/null @@ -1,55 +0,0 @@ -package log - -import ( - "fmt" - "net/http" - - "github.com/fatih/color" - "github.com/sirupsen/logrus" -) - -type buffaloFormatter struct{} - -func (buffaloFormatter) Format(entry *logrus.Entry) ([]byte, error) { - if entry.Level == logrus.ErrorLevel { - // buffalo does not pass request params when an error occurs: pass params - // when https://github.com/gobuffalo/buffalo/issues/1171 is resolved. - return fmtBuffaloErr(entry.Message), nil - } - - statusCode, _ := entry.Data["status"].(int) - status := fmt.Sprint(statusCode) - switch { - case statusCode < http.StatusBadRequest: - status = color.GreenString("%v", status) - case statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError: - status = color.HiYellowString("%v", status) - default: - status = color.HiRedString("%v", status) - } - method, hasMethod := entry.Data["method"].(string) - path, hasPath := entry.Data["path"].(string) - if !hasMethod || !hasPath { - return fmtBuffaloMsg(entry.Message), nil - } - - return fmtBuffaloRequest(method, path, status), nil -} - -func fmtBuffaloRequest(method, path, status string) []byte { - return []byte(fmt.Sprintf( - "%v %v %v [%v]\n", - color.CyanString("handler:"), - method, - path, - status, - )) -} - -func fmtBuffaloMsg(msg string) []byte { - return []byte(fmt.Sprintf("%s %s\n", color.CyanString("buffalo:"), msg)) -} - -func fmtBuffaloErr(msg string) []byte { - return []byte(fmt.Sprintf("%s %s\n", color.HiRedString("buffalo:"), msg)) -}