github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/web/handlers.go (about)

     1  package web
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"net/url"
     8  
     9  	"github.com/hellofresh/janus/pkg/render"
    10  	log "github.com/sirupsen/logrus"
    11  )
    12  
    13  // Home handler is just a nice home page message
    14  func Home() http.HandlerFunc {
    15  	return func(w http.ResponseWriter, r *http.Request) {
    16  		render.JSON(w, http.StatusOK, "Welcome to Janus")
    17  	}
    18  }
    19  
    20  // RedirectHTTPS redirects an http request to https
    21  func RedirectHTTPS(port int) http.HandlerFunc {
    22  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    23  		host, _, _ := net.SplitHostPort(req.Host)
    24  
    25  		target := url.URL{
    26  			Scheme: "https",
    27  			Host:   fmt.Sprintf("%s:%v", host, port),
    28  			Path:   req.URL.Path,
    29  		}
    30  		if len(req.URL.RawQuery) > 0 {
    31  			target.RawQuery += "?" + req.URL.RawQuery
    32  		}
    33  		log.Printf("redirect to: %s", target.String())
    34  		http.Redirect(w, req, target.String(), http.StatusTemporaryRedirect)
    35  	})
    36  }