Drone CI and linting (#14)

This commit is contained in:
2019-10-16 18:32:21 +00:00
committed by Gitea
parent ef597063cf
commit 5acc64de59
12 changed files with 51 additions and 11 deletions

View File

@@ -62,6 +62,7 @@ func (fc *fileConfig) DeletePackage(req *structs.PackageDeleteRequest) []structs
func (fc *fileConfig) GetAllowedIPs() []string {
var allowedIPs []string
fc.HTTP.allowedipsmutex.RLock()
allowedIPs = append(allowedIPs, fc.HTTP.AllowedIPs...)
fc.HTTP.allowedipsmutex.RUnlock()
@@ -83,6 +84,7 @@ func (fc *fileConfig) GetAllPackagesInfo() map[string]*structs.Package {
func (fc *fileConfig) GetPackagesInfo(packages []string) (map[string]*structs.Package, []structs.Error) {
pkgs := make(map[string]*structs.Package)
var errors []structs.Error
fc.packagesMutex.Lock()
@@ -135,6 +137,7 @@ func (fc *fileConfig) Initialize() {
// Ensure that localhost (127.0.0.1) are defined in AllowedIPs.
var localhostIsAllowed bool
for _, ip := range fc.HTTP.AllowedIPs {
if strings.Contains(ip, "127.0.0.1") {
localhostIsAllowed = true
@@ -144,6 +147,7 @@ func (fc *fileConfig) Initialize() {
if !localhostIsAllowed {
cfgLoadLog.Warn().Msg("Localhost (127.0.0.1) wasn't allowed to access configuration API, adding it to list of allowed IP addresses")
fc.HTTP.AllowedIPs = append(fc.HTTP.AllowedIPs, "127.0.0.1")
} else {
cfgLoadLog.Debug().Msg("Localhost (127.0.0.1) is allowed to access configuration API")
@@ -161,6 +165,7 @@ func (fc *fileConfig) normalizePath(configPath string) (string, error) {
if err != nil {
return "", err
}
configPath = strings.Replace(configPath, "~", homeDir, 1)
}

View File

@@ -44,10 +44,12 @@ func Initialize() {
// Shutdown stops HTTP server. Returns true on success and false on failure.
func Shutdown() {
log.Info().Msg("Shutting down HTTP server...")
err := Srv.Shutdown(context.Background())
if err != nil {
log.Fatal().Err(err).Msg("Failed to stop HTTP server")
}
log.Info().Msg("HTTP server shutted down")
}
@@ -59,19 +61,23 @@ func Start() {
go func() {
err := Srv.Start(configuration.Cfg.HTTP.Listen)
if !strings.Contains(err.Error(), "Server closed") {
log.Fatal().Err(err).Msg("HTTP server critial error occurred")
log.Fatal().Err(err).Msg("HTTP server critical error occurred")
}
}()
// Check that HTTP server was started.
httpc := &http.Client{Timeout: time.Second * 1}
checks := 0
for {
checks++
if checks >= configuration.Cfg.HTTP.WaitForSeconds {
log.Fatal().Int("seconds passed", checks).Msg("HTTP server isn't up")
}
time.Sleep(time.Second * 1)
resp, err := httpc.Get("http://" + configuration.Cfg.HTTP.Listen + "/_internal/waitForOnline")
if err != nil {
log.Debug().Err(err).Msg("HTTP error occurred, HTTP server isn't ready, waiting...")
@@ -80,10 +86,12 @@ func Start() {
response, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Debug().Err(err).Msg("Failed to read response body, HTTP server isn't ready, waiting...")
continue
}
log.Debug().Str("status", resp.Status).Int("body length", len(response)).Msg("HTTP response received")
if resp.StatusCode == http.StatusOK {
@@ -91,7 +99,9 @@ func Start() {
log.Debug().Msg("Response is empty, HTTP server isn't ready, waiting...")
continue
}
log.Debug().Int("status code", resp.StatusCode).Msgf("Response: %+v", string(response))
if len(response) == 17 {
break
}
@@ -104,5 +114,6 @@ func waitForHTTPServerToBeUpHandler(ec echo.Context) error {
response := map[string]string{
"error": "None",
}
return ec.JSON(200, response)
}

View File

@@ -24,6 +24,7 @@ func (sjb *StrictJSONBinder) Bind(i interface{}, c echo.Context) error {
// Decode it.
decoder := json.NewDecoder(req.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(i); err != nil {
if ute, ok := err.(*json.UnmarshalTypeError); ok {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset))

View File

@@ -23,6 +23,7 @@ func Initialize() {
loggerLevel, loggerLevelFound := os.LookupEnv("LOGGER_LEVEL")
if loggerLevelFound {
fmt.Println("Setting logger level to:", loggerLevel)
switch strings.ToUpper(loggerLevel) {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
@@ -36,7 +37,7 @@ func Initialize() {
zerolog.SetGlobalLevel(zerolog.FatalLevel)
default:
fmt.Println("Invalid logger level passed:", loggerLevel)
fmt.Println("Fofcing INFO")
fmt.Println("Forcing INFO")
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
} else {
@@ -47,6 +48,7 @@ func Initialize() {
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
var v string
if ii, ok := i.(string); ok {
ii = strings.ToUpper(ii)
switch ii {
@@ -66,6 +68,7 @@ func Initialize() {
v = ii
}
}
return fmt.Sprintf("| %s |", v)
}

View File

@@ -52,6 +52,7 @@ func execRequest(method string, url string, data interface{}) ([]byte, error) {
if err2 != nil {
return nil, err2
}
response.Body.Close()
log.Debug().Int("response body length (bytes)", len(bodyBytes)).Msg("Got response")