1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-15 00:15:15 +02:00

81 lines
1.5 KiB
Go
Raw Permalink Normal View History

package webroute
import (
2019-10-15 14:46:42 -08:00
"fmt"
"net"
"net/url"
2019-10-15 14:46:42 -08:00
"os"
"strings"
"github.com/pkg/errors"
)
type WebRoute struct {
webAppUrl url.URL
webApiUrl url.URL
}
func New(apiBaseUrl, appBaseUrl string) (WebRoute, error) {
var r WebRoute
apiUrl, err := url.Parse(apiBaseUrl)
if err != nil {
return r, errors.WithMessagef(err, "Failed to parse api base URL '%s'", apiBaseUrl)
}
r.webApiUrl = *apiUrl
appUrl, err := url.Parse(appBaseUrl)
if err != nil {
return r, errors.WithMessagef(err, "Failed to parse app base URL '%s'", appBaseUrl)
}
r.webAppUrl = *appUrl
return r, nil
}
func (r WebRoute) WebAppUrl(urlPath string) string {
u := r.webAppUrl
u.Path = urlPath
return u.String()
}
func (r WebRoute) WebApiUrl(urlPath string) string {
u := r.webApiUrl
u.Path = urlPath
return u.String()
}
func (r WebRoute) UserResetPassword(resetHash string) string {
u := r.webAppUrl
2019-08-05 13:27:23 -08:00
u.Path = "/user/reset-password/" + resetHash
return u.String()
}
2019-08-05 13:27:23 -08:00
func (r WebRoute) UserInviteAccept(inviteHash string) string {
2019-08-05 13:27:23 -08:00
u := r.webAppUrl
u.Path = "/users/invite/" + inviteHash
return u.String()
2019-08-05 14:32:45 -08:00
}
2019-08-05 19:49:30 -08:00
func (r WebRoute) ApiDocs() string {
2019-08-05 19:49:30 -08:00
u := r.webApiUrl
u.Path = "/docs"
return u.String()
}
func (r WebRoute) ApiDocsJson(internal bool) string {
2019-08-05 19:49:30 -08:00
u := r.webApiUrl
2019-10-15 14:46:42 -08:00
if ev := os.Getenv("USE_NETWORK_ALIAS"); ev != "" {
if internal && strings.Contains(u.Host, ":") {
h, p, _ := net.SplitHostPort(u.Host)
if h == "127.0.0.1" || h == "localhost" {
u.Host = fmt.Sprintf("web-api:%s", p)
}
}
}
2019-08-05 19:49:30 -08:00
u.Path = "/docs/doc.json"
return u.String()
}