changed mod_portal.go to new response design

This commit is contained in:
knoxfighter 2020-10-16 21:44:41 +02:00
parent a03bbffb2e
commit 751ea66ebb
4 changed files with 23 additions and 30 deletions

View File

@ -20,15 +20,12 @@ func ModPortalListModsHandler(w http.ResponseWriter, r *http.Request) {
var statusCode int var statusCode int
resp, err, statusCode = factorio.ModPortalList() resp, err, statusCode = factorio.ModPortalList()
w.WriteHeader(statusCode)
if err != nil { 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) log.Println(resp)
w.WriteHeader(http.StatusInternalServerError)
return return
} }
w.WriteHeader(statusCode)
} }
// ModPortalModInfoHandler returns JSON response with the mod details // ModPortalModInfoHandler returns JSON response with the mod details
@ -114,19 +111,13 @@ func ModPortalLoginHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
loginStatus, err, statusCode := factorio.FactorioLogin(data.Username, data.Password) err, statusCode := factorio.FactorioLogin(data.Username, data.Password)
w.WriteHeader(statusCode)
if err != nil { if err != nil {
resp = fmt.Sprintf("Error trying to login into Factorio: %s", err) resp = fmt.Sprintf("Error trying to login into Factorio: %s", err)
log.Println(resp) log.Println(resp)
w.WriteHeader(http.StatusInternalServerError)
return return
} }
if loginStatus == "" {
resp = true
}
w.WriteHeader(statusCode)
} }
func ModPortalLoginStatusHandler(w http.ResponseWriter, r *http.Request) { func ModPortalLoginStatusHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -46,7 +46,7 @@ func TestMain(m *testing.M) {
} }
if !load { if !load {
// no credentials found, login... // 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 { if err != nil {
log.Fatalf("Error logging in into factorio: %s", err) log.Fatalf("Error logging in into factorio: %s", err)
return return

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
@ -40,13 +39,17 @@ func ModPortalList() (interface{}, error, int) {
if err != nil { if err != nil {
return "error", err, http.StatusInternalServerError return "error", err, http.StatusInternalServerError
} }
defer resp.Body.Close()
text, err := ioutil.ReadAll(resp.Body) text, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil { if err != nil {
return "error", err, http.StatusInternalServerError return "error", err, http.StatusInternalServerError
} }
if resp.StatusCode != http.StatusOK {
return nil, errors.New(string(text)), resp.StatusCode
}
var jsonVal interface{} var jsonVal interface{}
err = json.Unmarshal(text, &jsonVal) err = json.Unmarshal(text, &jsonVal)
if err != nil { if err != nil {
@ -69,9 +72,9 @@ func ModPortalModDetails(modId string) (ModPortalStruct, error, int) {
if err != nil { if err != nil {
return mod, err, http.StatusInternalServerError return mod, err, http.StatusInternalServerError
} }
defer resp.Body.Close()
text, err := ioutil.ReadAll(resp.Body) text, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil { if err != nil {
return mod, err, http.StatusInternalServerError return mod, err, http.StatusInternalServerError
} }
@ -81,6 +84,10 @@ func ModPortalModDetails(modId string) (ModPortalStruct, error, int) {
return mod, err, http.StatusInternalServerError return mod, err, http.StatusInternalServerError
} }
if resp.StatusCode != http.StatusOK {
return ModPortalStruct{}, errors.New(string(text)), resp.StatusCode
}
server := GetFactorioServer() server := GetFactorioServer()
installedBaseVersion := Version{} installedBaseVersion := Version{}
@ -99,37 +106,33 @@ func ModPortalModDetails(modId string) (ModPortalStruct, error, int) {
} }
//Log the user into factorio, so mods can be downloaded //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 var err error
resp, err := http.PostForm("https://auth.factorio.com/api-login", resp, err := http.PostForm("https://auth.factorio.com/api-login",
url.Values{"require_game_ownership": {"true"}, "username": {username}, "password": {password}}) url.Values{"require_game_ownership": {"true"}, "username": {username}, "password": {password}})
if err != nil { if err != nil {
log.Printf("error on logging in: %s", err) return err, http.StatusInternalServerError
return "", err, resp.StatusCode
} }
defer resp.Body.Close() defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body) bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Printf("error on reading resp.Body: %s", err) return err, http.StatusInternalServerError
return "", err, http.StatusInternalServerError
} }
bodyString := string(bodyBytes) bodyString := string(bodyBytes)
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
log.Println("error Statuscode not 200") return errors.New(bodyString), resp.StatusCode
return bodyString, errors.New(bodyString), resp.StatusCode
} }
var successResponse []string var successResponse []string
err = json.Unmarshal(bodyBytes, &successResponse) err = json.Unmarshal(bodyBytes, &successResponse)
if err != nil { if err != nil {
log.Printf("error on unmarshal body: %s", err) return err, http.StatusInternalServerError
return err.Error(), err, http.StatusInternalServerError
} }
credentials := Credentials{ credentials := Credentials{
@ -139,9 +142,8 @@ func FactorioLogin(username string, password string) (string, error, int) {
err = credentials.Save() err = credentials.Save()
if err != nil { if err != nil {
log.Printf("error saving the credentials. %s", err) return err, http.StatusInternalServerError
return err.Error(), err, http.StatusInternalServerError
} }
return "", nil, http.StatusOK return nil, http.StatusOK
} }

View File

@ -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)