Redirect only from the address port number

Viewed 43

I've two containers running in docker. My API (and standard part) is /abc/{xyz}/xmc, where xyz is, of course, dynamic as this is provided by the user while sending a "POST" request. This API is running on both port 8080 and 3000 like:

http://localhost:3000/abc/nv8vhvv/xmc

http://localhost:8080/abc/chjbc8c/xmc

If a user sends a "POST" request to http://localhost:8080/abc/chjbc8c/xmc, I want to (internally in the golang file) redirect it to http://localhost:3000/abc/chjbc8c/xmc. How can I do this in golang?

1 Answers
package main

import (
    "fmt"
    "log"
    "net/http"
)

func redirect(w http.ResponseWriter, r *http.Request) {
    if r.Method == "POST" {
        redirectURI := fmt.Sprintf("http://localhost:3000%v", r.RequestURI)
        http.Redirect(w, r, redirectURI, 307)
    }
}

func main() {

    http.HandleFunc("/abc/nv8vhvv/xmc", redirect)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
Related