2021-03-18 01:25:06 +00:00
|
|
|
package hmnurl
|
|
|
|
|
|
|
|
import (
|
2021-05-11 22:53:23 +00:00
|
|
|
"fmt"
|
2021-03-18 01:25:06 +00:00
|
|
|
"net/url"
|
2021-05-11 22:53:23 +00:00
|
|
|
"time"
|
2021-03-18 01:25:06 +00:00
|
|
|
|
|
|
|
"git.handmade.network/hmn/hmn/src/config"
|
2021-05-06 04:04:58 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
2021-04-11 21:46:06 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/oops"
|
2021-03-18 01:25:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const StaticPath = "/public"
|
|
|
|
const StaticThemePath = "/public/themes"
|
|
|
|
|
|
|
|
type Q struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:46:06 +00:00
|
|
|
var baseUrlParsed url.URL
|
2021-05-11 22:53:23 +00:00
|
|
|
var cacheBust string
|
2021-04-11 21:46:06 +00:00
|
|
|
|
|
|
|
func init() {
|
2021-05-05 20:34:32 +00:00
|
|
|
SetGlobalBaseUrl(config.Config.BaseUrl)
|
2021-05-11 22:53:23 +00:00
|
|
|
SetCacheBust(fmt.Sprint(time.Now().Unix()))
|
2021-05-05 20:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetGlobalBaseUrl(fullBaseUrl string) {
|
|
|
|
parsed, err := url.Parse(fullBaseUrl)
|
2021-04-11 21:46:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "could not parse base URL"))
|
|
|
|
}
|
2021-05-04 09:24:31 +00:00
|
|
|
if parsed.Scheme == "" || parsed.Host == "" {
|
|
|
|
panic(oops.New(nil, "Website is misconfigured. Config should include a full BaseUrl (e.g. \"http://handmade.local:9001\")"))
|
|
|
|
}
|
2021-04-11 21:46:06 +00:00
|
|
|
|
|
|
|
baseUrlParsed = *parsed
|
|
|
|
}
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
func SetCacheBust(newCacheBust string) {
|
|
|
|
cacheBust = newCacheBust
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:25:06 +00:00
|
|
|
func Url(path string, query []Q) string {
|
2021-04-11 21:46:06 +00:00
|
|
|
return ProjectUrl(path, query, "")
|
|
|
|
}
|
|
|
|
|
2021-05-06 04:04:58 +00:00
|
|
|
func ProjectUrl(path string, query []Q, slug string) string {
|
2021-05-11 22:53:23 +00:00
|
|
|
return ProjectUrlWithFragment(path, query, slug, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectUrlWithFragment(path string, query []Q, slug string, fragment string) string {
|
2021-05-06 04:04:58 +00:00
|
|
|
subdomain := slug
|
|
|
|
if slug == models.HMNProjectSlug {
|
|
|
|
subdomain = ""
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:46:06 +00:00
|
|
|
host := baseUrlParsed.Host
|
|
|
|
if len(subdomain) > 0 {
|
2021-05-06 04:04:58 +00:00
|
|
|
host = slug + "." + host
|
2021-03-18 01:25:06 +00:00
|
|
|
}
|
2021-04-11 21:46:06 +00:00
|
|
|
|
|
|
|
url := url.URL{
|
|
|
|
Scheme: baseUrlParsed.Scheme,
|
|
|
|
Host: host,
|
|
|
|
Path: trim(path),
|
|
|
|
RawQuery: encodeQuery(query),
|
2021-05-11 22:53:23 +00:00
|
|
|
Fragment: fragment,
|
2021-04-11 21:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return url.String()
|
2021-03-18 01:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func trim(path string) string {
|
2021-04-22 23:02:50 +00:00
|
|
|
if len(path) > 0 && path[0] == '/' {
|
2021-03-18 01:25:06 +00:00
|
|
|
return path[1:]
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeQuery(query []Q) string {
|
|
|
|
result := url.Values{}
|
|
|
|
for _, q := range query {
|
|
|
|
result.Set(q.Name, q.Value)
|
|
|
|
}
|
|
|
|
return result.Encode()
|
|
|
|
}
|