mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
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
This commit is contained in:
committed by
Aaron Schlesinger
parent
2cf10c3ead
commit
c3d1d14d23
@@ -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:
|
||||
|
||||
+4
-7
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user