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-09-22 19:18:39 +00:00
|
|
|
"regexp"
|
2021-05-11 22:53:23 +00:00
|
|
|
"time"
|
2021-03-18 01:25:06 +00:00
|
|
|
|
2021-05-06 04:04:58 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
2021-11-10 04:11:39 +00:00
|
|
|
|
|
|
|
"git.handmade.network/hmn/hmn/src/config"
|
2021-04-11 21:46:06 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/oops"
|
2021-03-18 01:25:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Q struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:35:01 +00:00
|
|
|
func QFromURL(u *url.URL) []Q {
|
|
|
|
var result []Q
|
|
|
|
for key, values := range u.Query() {
|
|
|
|
for _, v := range values {
|
|
|
|
result = append(result, Q{Name: key, Value: v})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:46:06 +00:00
|
|
|
var baseUrlParsed url.URL
|
2021-05-11 22:53:23 +00:00
|
|
|
var cacheBust string
|
2021-06-22 09:50:40 +00:00
|
|
|
var S3BaseUrl string
|
2021-05-25 13:48:57 +00:00
|
|
|
var isTest bool
|
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-06-22 09:50:40 +00:00
|
|
|
SetS3BaseUrl(config.Config.DigitalOcean.AssetsPublicUrlRoot)
|
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-06-22 09:50:40 +00:00
|
|
|
func SetS3BaseUrl(base string) {
|
|
|
|
S3BaseUrl = base
|
2021-09-22 19:18:39 +00:00
|
|
|
RegexS3Asset = regexp.MustCompile(fmt.Sprintf("%s(?P<key>[\\w\\-./]+)", regexp.QuoteMeta(S3BaseUrl)))
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 03:31:43 +00:00
|
|
|
func GetBaseHost() string {
|
|
|
|
return baseUrlParsed.Host
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:11:39 +00:00
|
|
|
type UrlContext struct {
|
|
|
|
PersonalProject bool
|
|
|
|
ProjectID int
|
|
|
|
ProjectSlug string
|
|
|
|
ProjectName string
|
2021-04-11 21:46:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:11:39 +00:00
|
|
|
var HMNProjectContext = UrlContext{
|
|
|
|
PersonalProject: false,
|
|
|
|
ProjectID: models.HMNProjectID,
|
|
|
|
ProjectSlug: models.HMNProjectSlug,
|
2021-05-11 22:53:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:11:39 +00:00
|
|
|
func Url(path string, query []Q) string {
|
|
|
|
return UrlWithFragment(path, query, "")
|
|
|
|
}
|
2021-04-11 21:46:06 +00:00
|
|
|
|
2021-11-10 04:11:39 +00:00
|
|
|
func UrlWithFragment(path string, query []Q, fragment string) string {
|
|
|
|
return HMNProjectContext.UrlWithFragment(path, query, fragment)
|
2021-03-18 01:25:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:51:28 +00:00
|
|
|
func (c *UrlContext) RewriteProjectUrl(u *url.URL) string {
|
|
|
|
// we need to strip anything matching the personal project regex to get the base path
|
|
|
|
match := RegexPersonalProject.FindString(u.Path)
|
|
|
|
return c.Url(u.Path[len(match):], QFromURL(u))
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|