Resolve golangcibot issues

This commit is contained in:
ErickSkrauch 2020-01-05 20:39:17 +03:00
parent 9946eae73b
commit 17f82ec6d3
8 changed files with 48 additions and 36 deletions

View File

@ -3,7 +3,9 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -35,3 +37,10 @@ func initConfig() {
replacer := strings.NewReplacer(".", "_") replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer) viper.SetEnvKeyReplacer(replacer)
} }
func waitForExitSignal() os.Signal {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
return <-ch
}

View File

@ -83,9 +83,21 @@ var serveCmd = &cobra.Command{
Auth: &auth.JwtAuth{Key: []byte(viper.GetString("chrly.secret"))}, Auth: &auth.JwtAuth{Key: []byte(viper.GetString("chrly.secret"))},
} }
finishChan := make(chan bool)
go func() {
if err := cfg.Run(); err != nil { if err := cfg.Run(); err != nil {
logger.Error(fmt.Sprintf("Error in main(): %v", err)) logger.Error(fmt.Sprintf("Error in main(): %v", err))
finishChan <- true
} }
}()
go func() {
s := waitForExitSignal()
logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s))
finishChan <- true
}()
<-finishChan
}, },
} }

View File

@ -34,9 +34,21 @@ var workerCmd = &cobra.Command{
Logger: logger, Logger: logger,
} }
finishChan := make(chan bool)
go func() {
if err := cfg.Run(); err != nil { if err := cfg.Run(); err != nil {
logger.Error(fmt.Sprintf("Error in main(): %v", err)) logger.Error(fmt.Sprintf("Error in main(): %v", err))
finishChan <- true
} }
}()
go func() {
s := waitForExitSignal()
logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s))
finishChan <- true
}()
<-finishChan
}, },
} }

View File

@ -49,13 +49,13 @@ type filesStorage struct {
func (repository *filesStorage) FindByUsername(username string) (*model.Cape, error) { func (repository *filesStorage) FindByUsername(username string) (*model.Cape, error) {
if username == "" { if username == "" {
return nil, &http.CapeNotFoundError{username} return nil, &http.CapeNotFoundError{Who: username}
} }
capePath := path.Join(repository.path, strings.ToLower(username)+".png") capePath := path.Join(repository.path, strings.ToLower(username)+".png")
file, err := os.Open(capePath) file, err := os.Open(capePath)
if err != nil { if err != nil {
return nil, &http.CapeNotFoundError{username} return nil, &http.CapeNotFoundError{Who: username}
} }
return &model.Cape{ return &model.Cape{

View File

@ -148,13 +148,13 @@ func (db *redisDb) StoreUuid(username string, uuid string) error {
func findByUsername(username string, conn util.Cmder) (*model.Skin, error) { func findByUsername(username string, conn util.Cmder) (*model.Skin, error) {
if username == "" { if username == "" {
return nil, &http.SkinNotFoundError{username} return nil, &http.SkinNotFoundError{Who: username}
} }
redisKey := buildUsernameKey(username) redisKey := buildUsernameKey(username)
response := conn.Cmd("GET", redisKey) response := conn.Cmd("GET", redisKey)
if !response.IsType(redis.Str) { if !response.IsType(redis.Str) {
return nil, &http.SkinNotFoundError{username} return nil, &http.SkinNotFoundError{Who: username}
} }
encodedResult, err := response.Bytes() encodedResult, err := response.Bytes()
@ -181,7 +181,7 @@ func findByUsername(username string, conn util.Cmder) (*model.Skin, error) {
func findByUserId(id int, conn util.Cmder) (*model.Skin, error) { func findByUserId(id int, conn util.Cmder) (*model.Skin, error) {
response := conn.Cmd("HGET", accountIdToUsernameKey, id) response := conn.Cmd("HGET", accountIdToUsernameKey, id)
if !response.IsType(redis.Str) { if !response.IsType(redis.Str) {
return nil, &http.SkinNotFoundError{"unknown"} return nil, &http.SkinNotFoundError{Who: "unknown"}
} }
username, _ := response.Str() username, _ := response.Str()

View File

@ -3,9 +3,6 @@ package http
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"os"
"os/signal"
"syscall"
) )
func NotFound(response http.ResponseWriter, _ *http.Request) { func NotFound(response http.ResponseWriter, _ *http.Request) {
@ -19,13 +16,6 @@ func NotFound(response http.ResponseWriter, _ *http.Request) {
_, _ = response.Write(data) _, _ = response.Write(data)
} }
func waitForSignal() os.Signal {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
return <-ch
}
func apiBadRequest(resp http.ResponseWriter, errorsPerField map[string][]string) { func apiBadRequest(resp http.ResponseWriter, errorsPerField map[string][]string) {
resp.WriteHeader(http.StatusBadRequest) resp.WriteHeader(http.StatusBadRequest)
resp.Header().Set("Content-Type", "application/json") resp.Header().Set("Content-Type", "application/json")

View File

@ -111,12 +111,7 @@ func (ctx *Skinsystem) Run() error {
Handler: ctx.CreateHandler(), Handler: ctx.CreateHandler(),
} }
go server.Serve(listener) return server.Serve(listener)
s := waitForSignal()
ctx.Logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s))
return nil
} }
func (ctx *Skinsystem) CreateHandler() *mux.Router { func (ctx *Skinsystem) CreateHandler() *mux.Router {
@ -274,7 +269,7 @@ func (ctx *Skinsystem) Textures(response http.ResponseWriter, request *http.Requ
responseData, _ := json.Marshal(textures) responseData, _ := json.Marshal(textures)
response.Header().Set("Content-Type", "application/json") response.Header().Set("Content-Type", "application/json")
response.Write(responseData) _, _ = response.Write(responseData)
} }
func (ctx *Skinsystem) SignedTextures(response http.ResponseWriter, request *http.Request) { func (ctx *Skinsystem) SignedTextures(response http.ResponseWriter, request *http.Request) {
@ -315,7 +310,7 @@ func (ctx *Skinsystem) SignedTextures(response http.ResponseWriter, request *htt
responseJson, _ := json.Marshal(responseData) responseJson, _ := json.Marshal(responseData)
response.Header().Set("Content-Type", "application/json") response.Header().Set("Content-Type", "application/json")
response.Write(responseJson) _, _ = response.Write(responseJson)
} }
func (ctx *Skinsystem) PostSkin(resp http.ResponseWriter, req *http.Request) { func (ctx *Skinsystem) PostSkin(resp http.ResponseWriter, req *http.Request) {

View File

@ -40,13 +40,7 @@ func (ctx *UUIDsWorker) Run() error {
Handler: ctx.CreateHandler(), Handler: ctx.CreateHandler(),
} }
// noinspection GoUnhandledErrorResult return server.Serve(listener)
go server.Serve(listener)
s := waitForSignal()
ctx.Logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s))
return nil
} }
func (ctx *UUIDsWorker) CreateHandler() http.Handler { func (ctx *UUIDsWorker) CreateHandler() http.Handler {