get forwarded url
This commit is contained in:
1
go.mod
1
go.mod
@@ -2,6 +2,7 @@ module github.com/sentriz/gonic
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.37.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/denisenkom/go-mssqldb v0.0.0-20190315220205-a8ed825ac853 // indirect
|
||||
github.com/dhowden/tag v0.0.0-20181104225729-a9f04c2798ca
|
||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
|
||||
|
||||
@@ -50,6 +50,9 @@ func (c *Controller) ServeHome(w http.ResponseWriter, r *http.Request) {
|
||||
c.DB.Table("albums").Count(&data.AlbumCount)
|
||||
c.DB.Table("tracks").Count(&data.TrackCount)
|
||||
c.DB.Find(&data.AllUsers)
|
||||
var apiKey db.Setting
|
||||
c.DB.Where("key = ?", "lastfm_api_key").First(&apiKey)
|
||||
data.CurrentLastFMAPIKey = apiKey.Value
|
||||
renderTemplate(w, r, "home", &data)
|
||||
}
|
||||
|
||||
|
||||
@@ -142,6 +142,21 @@ func respondError(w http.ResponseWriter, r *http.Request,
|
||||
))
|
||||
}
|
||||
|
||||
func firstExisting(or string, strings ...string) string {
|
||||
current := ""
|
||||
for _, s := range strings {
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
current = s
|
||||
break
|
||||
}
|
||||
if current == "" {
|
||||
return or
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, r *http.Request,
|
||||
name string, data *templateData) {
|
||||
session := r.Context().Value("session").(*sessions.Session)
|
||||
@@ -154,11 +169,18 @@ func renderTemplate(w http.ResponseWriter, r *http.Request,
|
||||
if ok {
|
||||
data.User = user
|
||||
}
|
||||
if r.URL.Host == "" {
|
||||
data.RequestRoot = "http://localhost"
|
||||
} else {
|
||||
data.RequestRoot = fmt.Sprintf("%s://%s", r.URL.Scheme, r.URL.Host)
|
||||
}
|
||||
scheme := firstExisting(
|
||||
"http", // fallback
|
||||
r.Header.Get("X-Forwarded-Proto"),
|
||||
r.Header.Get("X-Forwarded-Scheme"),
|
||||
r.URL.Scheme,
|
||||
)
|
||||
host := firstExisting(
|
||||
"localhost:7373", // fallback
|
||||
r.Header.Get("X-Forwarded-Host"),
|
||||
r.Host,
|
||||
)
|
||||
data.RequestRoot = fmt.Sprintf("%s://%s", scheme, host)
|
||||
err := templates[name].ExecuteTemplate(w, "layout", data)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("500 when executing: %v", err), 500)
|
||||
|
||||
Reference in New Issue
Block a user