Hello George, right here is the code:
The Mux Router code:
router.HandleFunc(/api/{Entity}, features.GetPostMortemDiag).Strategies(GET, POST, PUT, DELETE)|
router.HandleFunc(/api/{entity}/{entityNumber}, features.GetPostMortemDiag).Strategies(GET, PUT)|]router.HandleFunc(/api/{entity}/{entityId}/{entity}, features.GetPostMortemDiag).Strategies(GET)
Handler Perform code:
func GetPostMortemDiag(w http.ResponseWriter, r *http.Request) {
log.Println("INFO - GetPostMortemDiag(): Getting into GetPostMortem Diag perform")
api_base_url = os.Getenv("MYAPP_POSTMORTEM_API") + r.URL.Path
// validate request auth
retcode := CheckRequestAuth(w, r, "MYAPP")
if retcode == 0 {
log.Println("INFO - GetPostMortemDiag(): Request auth is profitable")
} else {
log.Println("ERR - GetPostMortemDiag(): Request auth just isn't profitable")
return
}
// Steps to Name the backend API begin now
// construct the url additional with the parameters from request
// Name the backend API with the constructed urls
question := r.URL.Question() // get the question from the request
api_url, err := url.Parse(api_base_url)
if err != nil {
log.Println("ERR - GetPgDiag(): Couldn't parse the backend API base URL: ", err.Error())
w.WriteHeader(http.StatusUnprocessableEntity)
//undecided in regards to the above standing code
w.Header().Set("Content material-Kind", "utility/json")
resp := make(map[string]string)
resp["message"] = "Couldn't parse the backend API base URL, please appropriate the URL or contact the administrator"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Println("WARN - GetPgDiag(): Error occurred in JSON marshal for failed backend-api-parse response. Err: %s", err)
}
w.Write(jsonResp)
return
}
api_url.RawQuery = question.Encode() // builds additional the URL struct including the question
_, err = url.Parse(api_url.String())
if err != nil {
log.Println("ERR - GetPgDiag(): Couldn't parse the question to backend API base URL: ", err.Error())
w.WriteHeader(http.StatusUnprocessableEntity)
w.Header().Set("Content material-Kind", "utility/json")
resp := make(map[string]string)
resp["message"] = "Couldn't parse question to the backend API base URL, please appropriate the question or contact the administrator"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Println("WARN - GetPgDiag(): Error occurred in JSON marshal for failed backend-api-query-parse response. Err: %s", err)
}
w.Write(jsonResp)
return
}
log.Println("INFO - GetPostMortemDiag(): Calling API endpoint", api_url)
timeout, err = strconv.Atoi(os.Getenv("MYAPP_POSTMORTEM_API_TIMEOUT"))
if err != nil {
log.Println("WARN - GetPostMortemDiag(): MYAPP_POSTMORTEM_API_TIMEOUT atmosphere variable not set or inaccurate, Utilizing default worth ", err)
timeout = default_timeout
}
token = SetJwt(false, "sgfr") // false means refresh=false - that's, DO NOT pressure a refresh
if token == "" {
log.Println("ERR - GetPostMortemDiag(): ForgeRock Token Validation failed: ")
w.WriteHeader(http.StatusUnauthorized)
w.Header().Set("Content material-Kind", "utility/json")
resp := make(map[string]string)
resp["message"] = "ForgeRock Token Validation failed , Please contact the administrator"
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Println("WARN - GetPostMortemDiag(): Error occurred in JSON marshal for Unauthorized response. Err: %s", err)
}
w.Write(jsonResp)
return
}
if r.Methodology == "POST" || r.Methodology == "PUT" || r.Methodology == "DELETE" {
if r.URL.Path == "/api/images-upload" {
UploadImages(w, r)
return
} else {
physique, err := ioutil.ReadAll(r.Physique)
if err != nil {
err_text := fmt.Sprintf("ERR - CallPostMortemAPI(): Error studying request physique: %v", err)
log.Printf(err_text)
http.Error(w, err_text, http.StatusBadRequest)
return
}
BodyData := string(physique)
myReader = strings.NewReader(BodyData)
}
}
log.Println("myReader", replicate.TypeOf(myReader))
if myReader != nil {
log.Println("DEBUG - CallPostMortemAPI() : Request has a physique")
req, err = http.NewRequest(r.Methodology, api_url.String(), myReader)
} else {
log.Println("DEBUG - CallPostMortemAPI() : Request doesn't have a physique")
req, err = http.NewRequest(r.Methodology, api_url.String(), nil)
}
if err != nil {
log.Println("ERR - GetPostMortemDiag(): Error whereas creating request to the backend API: "+api_url.String(), err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content material-Kind", "utility/json")
resp := make(map[string]string)
resp["message"] = "Error whereas creating request to the backend API" + api_url.String()
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Println("WARN - GetPostMortemDiag(): Error occurred in JSON marshal for Unauthorized response. Err: %s", err)
}
w.Write(jsonResp)
return
}
req.Header.Set("Authorization", "Bearer "+token)
if os.Getenv("ENV") == "LOCAL" {
req.Header.Set("Shopper-Key", os.Getenv("SGFR-CONSUMER-KEY-POSTMORTEM"))
} else {
req.Header.Set("Shopper-Key", GetDataFromVault(SGFR_CONSUMER_KEY_VALUE))
}
req.Header.Set("Content material-Kind", "utility/json")
consumer := &http.Consumer{
Timeout: time.Second * time.Period(timeout),
}
response, err := consumer.Do(req)
if err != nil {
log.Println("ERR - CallPostMortemAPI(): API Name failed: "+api_url.String(), err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content material-Kind", "utility/json")
resp := make(map[string]string)
resp["message"] = "API Name failed: " + api_url.String()
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Println("WARN - CallPostMortemAPI(): Error occurred in JSON marshal for Unauthorized response. Err: %s", err)
}
//tbd - deal with error situation
w.Write(jsonResp)
return
}
w.WriteHeader(response.StatusCode)
defer response.Physique.Shut()
w.Header().Set("Content material-Kind", "utility/json")
log.Println("INFO - GetPostMortemDiag(): Response code from URL: ", response.StatusCode)
b, err := io.ReadAll(response.Physique)
// fmt.Fprint(w, string(b))
w.Write(b)
}
** React Code **
They’re axios calls to totally different endpoints – to totally different endpoints served my mux, and known as from one React web page. The issue is that some/all of these a number of calls are exhibiting the identical response – which is the response for a kind of calls.
As you see within the mux-router code, {entity} makes it simpler to overload one mux router entry to serve a couple of endpoint. Nonetheless, even with out such {entity} parameterization, if we made separate mux traces for every endpoint, and separate handler perform for every such endpoint, the bahaviour has been the identical.

