converge/pkg/server/ui/convergeaccess.go
Erik Brakkee 3408e2893a moved all ui stuff to the ui package.
The structure of converge server is now much more clear in the package
// structure below pkg/server.
2024-09-08 11:16:49 +02:00

57 lines
1.1 KiB
Go

package ui
import (
"git.wamblee.org/converge/pkg/models"
"net/http"
"regexp"
"strings"
"time"
)
func GetUserLocation(r *http.Request) (*time.Location, error) {
tzName := r.URL.Query().Get("timezone")
if tzName == "" {
tzName = r.Header.Get("X-Timezone")
}
if tzName == "" {
tzName = "UTC"
}
return time.LoadLocation(tzName)
}
func getConvergeAccess(r *http.Request) models.ConvergeAccess {
pattern := regexp.MustCompile("^(.*)/usage$")
matches := pattern.FindStringSubmatch(r.URL.Path)
contextPath := ""
if len(matches) == 2 {
contextPath = matches[1]
}
secure := ""
if r.TLS == nil {
secure = ""
} else {
secure = "s"
}
for _, header := range []string{"X-Forwarded-Proto", "X-Scheme", "X-Forwarded-Scheme"} {
values := r.Header.Values(header)
for _, value := range values {
if strings.ToLower(value) == "https" {
secure = "s"
}
}
}
location, err := GetUserLocation(r)
if err != nil {
panic(err)
}
baseUrl := strings.ReplaceAll(r.Host+contextPath, "//", "/")
return models.ConvergeAccess{
Secure: secure,
BaseUrl: baseUrl,
Location: location,
}
}