hdkv
/
wind8_fetcher
Archived
1
Fork 0
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues/pull-requests.
wind8_fetcher/api/fetch_request/request.go

63 lines
1.5 KiB
Go

package fetchrequest
import (
// stdlib
"encoding/json"
"net/http"
)
/*
HandleFetchRequest accepts and works with request, generated by wind8 site engine
*/
func HandleFetchRequest(w http.ResponseWriter, r *http.Request) {
response := make(map[string]interface{})
responseBody := make([]byte, 0)
w.Header().Add("Content-Type", "application/json; charset=utf-8")
if r.Method == "POST" {
requestValid := true
requestBody := make(map[string]string)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&requestBody)
if err != nil {
c.Log.Errorln(err)
requestValid = false
response["description"] = "Wrong data, must be valid JSON."
}
if requestBody["god_name"] == "" {
requestValid = false
response["description"] = "Godville god name not passed."
}
if requestValid {
profile, skills, pantheons, status := startParsing(requestBody["god_name"])
if status != "success" {
requestValid = false
response["description"] = "Parsing error"
} else {
response["profile"] = profile
response["skills"] = skills
response["pantheons"] = pantheons
}
}
if requestValid {
response["status"] = "success"
} else {
response["status"] = "error"
response["error"] = "400"
w.WriteHeader(400)
}
} else {
w.WriteHeader(404)
response["status"] = "error"
response["error_code"] = "404"
response["description"] = "We're accepting only POST requests. Sorry."
}
responseBody, err := json.Marshal(response)
if err != nil {
c.Log.Errorln(err)
}
w.Write(responseBody)
}