diff --git a/cmd/root.go b/cmd/root.go index 038161c..eb17903 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,7 +3,9 @@ package cmd import ( "fmt" "os" + "os/signal" "strings" + "syscall" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -35,3 +37,10 @@ func initConfig() { replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) } + +func waitForExitSignal() os.Signal { + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + + return <-ch +} diff --git a/cmd/serve.go b/cmd/serve.go index da05120..01b7c75 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -83,9 +83,21 @@ var serveCmd = &cobra.Command{ Auth: &auth.JwtAuth{Key: []byte(viper.GetString("chrly.secret"))}, } - if err := cfg.Run(); err != nil { - logger.Error(fmt.Sprintf("Error in main(): %v", err)) - } + finishChan := make(chan bool) + go func() { + if err := cfg.Run(); err != nil { + 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 }, } diff --git a/cmd/worker.go b/cmd/worker.go index 13c1adb..79ef7ed 100644 --- a/cmd/worker.go +++ b/cmd/worker.go @@ -34,9 +34,21 @@ var workerCmd = &cobra.Command{ Logger: logger, } - if err := cfg.Run(); err != nil { - logger.Error(fmt.Sprintf("Error in main(): %v", err)) - } + finishChan := make(chan bool) + go func() { + if err := cfg.Run(); err != nil { + 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 }, } diff --git a/db/filesystem.go b/db/filesystem.go index a9c9030..d94e53f 100644 --- a/db/filesystem.go +++ b/db/filesystem.go @@ -49,13 +49,13 @@ type filesStorage struct { func (repository *filesStorage) FindByUsername(username string) (*model.Cape, error) { if username == "" { - return nil, &http.CapeNotFoundError{username} + return nil, &http.CapeNotFoundError{Who: username} } capePath := path.Join(repository.path, strings.ToLower(username)+".png") file, err := os.Open(capePath) if err != nil { - return nil, &http.CapeNotFoundError{username} + return nil, &http.CapeNotFoundError{Who: username} } return &model.Cape{ diff --git a/db/redis.go b/db/redis.go index d07e5b6..ec218b6 100644 --- a/db/redis.go +++ b/db/redis.go @@ -148,13 +148,13 @@ func (db *redisDb) StoreUuid(username string, uuid string) error { func findByUsername(username string, conn util.Cmder) (*model.Skin, error) { if username == "" { - return nil, &http.SkinNotFoundError{username} + return nil, &http.SkinNotFoundError{Who: username} } redisKey := buildUsernameKey(username) response := conn.Cmd("GET", redisKey) if !response.IsType(redis.Str) { - return nil, &http.SkinNotFoundError{username} + return nil, &http.SkinNotFoundError{Who: username} } 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) { response := conn.Cmd("HGET", accountIdToUsernameKey, id) if !response.IsType(redis.Str) { - return nil, &http.SkinNotFoundError{"unknown"} + return nil, &http.SkinNotFoundError{Who: "unknown"} } username, _ := response.Str() diff --git a/http/http.go b/http/http.go index 8286b28..aec1182 100644 --- a/http/http.go +++ b/http/http.go @@ -3,9 +3,6 @@ package http import ( "encoding/json" "net/http" - "os" - "os/signal" - "syscall" ) func NotFound(response http.ResponseWriter, _ *http.Request) { @@ -19,13 +16,6 @@ func NotFound(response http.ResponseWriter, _ *http.Request) { _, _ = 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) { resp.WriteHeader(http.StatusBadRequest) resp.Header().Set("Content-Type", "application/json") diff --git a/http/skinsystem.go b/http/skinsystem.go index 2e2d80f..659db29 100644 --- a/http/skinsystem.go +++ b/http/skinsystem.go @@ -111,12 +111,7 @@ func (ctx *Skinsystem) Run() error { Handler: ctx.CreateHandler(), } - go server.Serve(listener) - - s := waitForSignal() - ctx.Logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s)) - - return nil + return server.Serve(listener) } func (ctx *Skinsystem) CreateHandler() *mux.Router { @@ -274,7 +269,7 @@ func (ctx *Skinsystem) Textures(response http.ResponseWriter, request *http.Requ responseData, _ := json.Marshal(textures) response.Header().Set("Content-Type", "application/json") - response.Write(responseData) + _, _ = response.Write(responseData) } 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) response.Header().Set("Content-Type", "application/json") - response.Write(responseJson) + _, _ = response.Write(responseJson) } func (ctx *Skinsystem) PostSkin(resp http.ResponseWriter, req *http.Request) { diff --git a/http/uuids_worker.go b/http/uuids_worker.go index 4765343..f983a5e 100644 --- a/http/uuids_worker.go +++ b/http/uuids_worker.go @@ -40,13 +40,7 @@ func (ctx *UUIDsWorker) Run() error { Handler: ctx.CreateHandler(), } - // noinspection GoUnhandledErrorResult - go server.Serve(listener) - - s := waitForSignal() - ctx.Logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s)) - - return nil + return server.Serve(listener) } func (ctx *UUIDsWorker) CreateHandler() http.Handler {