htmgo/htmgo-site/internal/urlhelper/resolve.go
maddalax 35877a1b2e
New Docs (#63)
* scripting enhancements

* tests

* cleanup / tests

* new docs wip

* add more docs

* more updates

* add caching docs

* add sse docs

* more docs

* sidebar, and fix navigation blocks

* remove old docs

* set proper meta

* fixes
2024-10-30 13:27:42 -05:00

28 lines
669 B
Go

package urlhelper
import (
"github.com/maddalax/htmgo/framework/h"
"net/url"
)
func ToAbsoluteUrl(ctx *h.RequestContext, path string) string {
// Define the relative path you want to add
relativePath := path
// Parse the current request URL
currentURL := ctx.Request.URL
// Set scheme and host from the request to create an absolute URL
scheme := "http"
if ctx.Request.TLS != nil {
scheme = "https"
}
currentURL.Host = ctx.Request.Host
currentURL.Scheme = scheme
// Combine the base URL with the relative path
absoluteURL := currentURL.ResolveReference(&url.URL{Path: relativePath})
// Output the full absolute URL
return absoluteURL.String()
}