diff --git a/src/api/mod_portal_handler.go b/src/api/mod_portal_handler.go index e723c0d..2f6e3be 100644 --- a/src/api/mod_portal_handler.go +++ b/src/api/mod_portal_handler.go @@ -20,15 +20,12 @@ func ModPortalListModsHandler(w http.ResponseWriter, r *http.Request) { var statusCode int resp, err, statusCode = factorio.ModPortalList() - + w.WriteHeader(statusCode) if err != nil { - resp = fmt.Sprintf("Error in listing mods from mod portal: %s", err) + resp = fmt.Sprintf("Error in listing mods from mod portal: %s\nresponse: %+v", err, resp) log.Println(resp) - w.WriteHeader(http.StatusInternalServerError) return } - - w.WriteHeader(statusCode) } // ModPortalModInfoHandler returns JSON response with the mod details @@ -114,19 +111,13 @@ func ModPortalLoginHandler(w http.ResponseWriter, r *http.Request) { return } - loginStatus, err, statusCode := factorio.FactorioLogin(data.Username, data.Password) + err, statusCode := factorio.FactorioLogin(data.Username, data.Password) + w.WriteHeader(statusCode) if err != nil { resp = fmt.Sprintf("Error trying to login into Factorio: %s", err) log.Println(resp) - w.WriteHeader(http.StatusInternalServerError) return } - - if loginStatus == "" { - resp = true - } - - w.WriteHeader(statusCode) } func ModPortalLoginStatusHandler(w http.ResponseWriter, r *http.Request) { diff --git a/src/api/mods_handler_test.go b/src/api/mods_handler_test.go index faeef7c..72566a8 100644 --- a/src/api/mods_handler_test.go +++ b/src/api/mods_handler_test.go @@ -46,7 +46,7 @@ func TestMain(m *testing.M) { } if !load { // no credentials found, login... - _, err, _ = factorio.FactorioLogin(os.Getenv("factorio_username"), os.Getenv("factorio_password")) + err, _ = factorio.FactorioLogin(os.Getenv("factorio_username"), os.Getenv("factorio_password")) if err != nil { log.Fatalf("Error logging in into factorio: %s", err) return diff --git a/src/factorio/mod_portal.go b/src/factorio/mod_portal.go index 10218e1..678b2d1 100644 --- a/src/factorio/mod_portal.go +++ b/src/factorio/mod_portal.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "io/ioutil" - "log" "net/http" "net/url" "time" @@ -40,13 +39,17 @@ func ModPortalList() (interface{}, error, int) { if err != nil { return "error", err, http.StatusInternalServerError } + defer resp.Body.Close() text, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return "error", err, http.StatusInternalServerError } + if resp.StatusCode != http.StatusOK { + return nil, errors.New(string(text)), resp.StatusCode + } + var jsonVal interface{} err = json.Unmarshal(text, &jsonVal) if err != nil { @@ -69,9 +72,9 @@ func ModPortalModDetails(modId string) (ModPortalStruct, error, int) { if err != nil { return mod, err, http.StatusInternalServerError } + defer resp.Body.Close() text, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() if err != nil { return mod, err, http.StatusInternalServerError } @@ -81,6 +84,10 @@ func ModPortalModDetails(modId string) (ModPortalStruct, error, int) { return mod, err, http.StatusInternalServerError } + if resp.StatusCode != http.StatusOK { + return ModPortalStruct{}, errors.New(string(text)), resp.StatusCode + } + server := GetFactorioServer() installedBaseVersion := Version{} @@ -99,37 +106,33 @@ func ModPortalModDetails(modId string) (ModPortalStruct, error, int) { } //Log the user into factorio, so mods can be downloaded -func FactorioLogin(username string, password string) (string, error, int) { +func FactorioLogin(username string, password string) (error, int) { var err error resp, err := http.PostForm("https://auth.factorio.com/api-login", url.Values{"require_game_ownership": {"true"}, "username": {username}, "password": {password}}) if err != nil { - log.Printf("error on logging in: %s", err) - return "", err, resp.StatusCode + return err, http.StatusInternalServerError } defer resp.Body.Close() bodyBytes, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Printf("error on reading resp.Body: %s", err) - return "", err, http.StatusInternalServerError + return err, http.StatusInternalServerError } bodyString := string(bodyBytes) if resp.StatusCode != http.StatusOK { - log.Println("error Statuscode not 200") - return bodyString, errors.New(bodyString), resp.StatusCode + return errors.New(bodyString), resp.StatusCode } var successResponse []string err = json.Unmarshal(bodyBytes, &successResponse) if err != nil { - log.Printf("error on unmarshal body: %s", err) - return err.Error(), err, http.StatusInternalServerError + return err, http.StatusInternalServerError } credentials := Credentials{ @@ -139,9 +142,8 @@ func FactorioLogin(username string, password string) (string, error, int) { err = credentials.Save() if err != nil { - log.Printf("error saving the credentials. %s", err) - return err.Error(), err, http.StatusInternalServerError + return err, http.StatusInternalServerError } - return "", nil, http.StatusOK + return nil, http.StatusOK } diff --git a/src/factorio_testfiles/file_usage.txt b/src/factorio_testfiles/file_usage.txt index 0ce1286..71bc928 100644 --- a/src/factorio_testfiles/file_usage.txt +++ b/src/factorio_testfiles/file_usage.txt @@ -1 +1 @@ -The usage of the belt-balancer mod is allowed without additional licensing by knoxfighter/asdff45 (the creator of the mod and also write of this factorio-server-manager) +The usage of the belt-balancer mod is allowed without additional licensing by knoxfighter/asdff45 (the creator of the mod and also contributor to this factorio-server-manager project)