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