Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1580

Go: redirecting from HTTP to HTTPS goes to localhost

$
0
0

I'm trying to redirect from HTTP to HTTPS in Go, but no matter what it goes to localhost:

I define the servers and run them:

httpServer = &http.Server{        Addr:    login.HttpPORT,        Handler: http.HandlerFunc(redirectNonSecure),    }    httpsServer = &http.Server{        Addr:      login.HttpsPORT,        TLSConfig: &tlsConf,    }    errChan := make(chan error, 2)    go func() {        log.Println("Starting HTTP server on :8081")        if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {            errChan <- err        }    }()    go func() {        log.Println("Starting HTTPS server on :8082")        if err := httpsServer.ListenAndServeTLS(certPath, keyPath); err != nil && err != http.ErrServerClosed {            errChan <- err        }    }()    log.Fatalf("Server error: %v", <-errChan)

And here is the redirect func:

func redirectNonSecure(w http.ResponseWriter, r *http.Request) {    redirectURL := "https://" + r.Host + httpsServer.Addr + r.RequestURI    http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)}

It always redirects to https://127.0.0.1:8082 and it should return https://ipAddr:8082If i run it from localhost there's no issue, obviously, if i go directly to the https without redirect it also worls fine, this is just when redirecting.I also tried hardcoding the IP inside the redirect function but it still redirects to localhost which makes me think there's something else at play here.

EDIT: I'll share here my whole main function in case there's something outside of the provided code that's causing some conflict, i'm really lost here:

func main() {    logger.CreateLogger()    var login Login    err := yaml.Unmarshal(yamlFile, &login)    if err != nil {        logger.Error.Println(err.Error())        return    }    dbConn := fmt.Sprintf("%s:%s@tcp(%s)/%s", login.DBUser, login.DBPass, login.DBHost, login.Dbase)    db, err := sql.Open("mysql", dbConn)    if err != nil {        log.Println("Couldn't connect!")        logger.Error.Println(err.Error())        return    }    database = db    makePaths()    certificates, err := tls.LoadX509KeyPair(certPath, keyPath)    if err != nil {        logger.Error.Println(err.Error())        return    }    tlsConf := tls.Config{        Certificates: []tls.Certificate{certificates},    }    r := mux.NewRouter()    r.HandleFunc("/api/pages", APIPage).        Methods("GET").        Schemes("https")    r.HandleFunc("/api/pages/{page_guid:[0-9a-zA-Z\\-]+}", APIPage).        Methods("GET").        Schemes("https")    r.HandleFunc("/api/page/{page_guid:[0-9a-zA-Z\\-]+}/comments", APICommentPost).        Methods("POST").        Schemes("https")    r.HandleFunc("/api/page/{page_guid:[0-9a-zA-Z\\-]+}/comments/{comment_id:[0-9]+}", APICommentPut).        Methods("PUT").        Schemes("https")    r.HandleFunc("/page/{page_guid:[0-9a-zA-Z\\-]+}", ServePage).        Schemes("https")    r.HandleFunc("/register", RegisterPOST).        Methods("POST").        Schemes("https")    r.HandleFunc("/login", LoginPOST).        Methods("POST").        Schemes("https")    r.HandleFunc("/logout", LogoutPOST).        Methods("POST").        Schemes("https")    r.HandleFunc("/", RedirIndex).        Schemes("https")    r.HandleFunc("/home", ServeIndex).        Schemes("https")    r.HandleFunc("/cookietest", startHandler).        Schemes("https")    r.HandleFunc("/middle", createCookieHandler).        Schemes("https")    r.HandleFunc("/cookietestdelete", deleteCookieHandler).        Schemes("https")    http.Handle("/", r)    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))    httpServer = &http.Server{        Addr:    login.HttpPORT,        Handler: http.HandlerFunc(redirectNonSecure),    }    httpsServer = &http.Server{        Addr:      login.HttpsPORT,        TLSConfig: &tlsConf,    }    errChan := make(chan error, 2)    go func() {        log.Println("Starting HTTP server on :8081")        if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {            errChan <- err        }    }()    go func() {        log.Println("Starting HTTPS server on :8082")        if err := httpsServer.ListenAndServeTLS(certPath, keyPath); err != nil && err != http.ErrServerClosed {            errChan <- err        }    }()    log.Fatalf("Server error: %v", <-errChan)}

could there be some path precedence that's making it behave this way?


Viewing all articles
Browse latest Browse all 1580

Trending Articles