urlhelper

package module
v0.0.0-...-b232a6b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

Url Helper

Small library to help generate absolute url paths based on incoming request. Uses X-Forwarded-Host and X-Forwarded-Proto first to gather Host and HTTP Scheme used generated urls.

Example


package main

import (
	"fmt"
	"github.com/30x/url-helper"
	"net/http"
)

func main() {

	http.HandleFunc("/some/resource", func(w http.ResponseWriter, r *http.Request) {
		url, _ := urlhelper.NewURLHelper(r)
		fmt.Println(url.Current())                            // GET http://1.2.3.4/some/resource?test=123 -> http://1.2.3.4/some/resource?test=123
		fmt.Println(url.Join("v1"))                           // GET http://1.2.3.4/some/resource?test=123 -> http://1.2.3.4/some/resource/v1?test=123
		fmt.Println(url.Join("../other"))                     // GET http://1.2.3.4/some/resource?test=123 -> http://1.2.3.4/some/other?test=123
		fmt.Println(url.Path("/new/root"))                    // GET http://1.2.3.4/some/resource?test=123 -> http://1.2.3.4/new/root
		fmt.Println(url.PathWithQuery("/new/root", "page=1")) // GET http://1.2.3.4/some/resource?test=123 -> http://1.2.3.4/new/root?page=1
	})

	http.ListenAndServe(":8080", nil)
}

Path Prefix

Url helper supports an optional header to prefix all paths. This can be used when api is in front of a proxy with a different path. For example /v1/status -> /status.

Header: X-Forwarded-Path-Prefix

To enable path you must enable a package wide flag. urlhelper.EnablePathPrefix = true

Documentation

Index

Constants

View Source
const (
	// XForwardedHost header for host forwarding
	XForwardedHost = "X-Forwarded-Host"
	// XForwardedProtocol header for http protocol forwarding
	XForwardedProtocol = "X-Forwarded-Proto"
	// XForwardedPathPrefix header for path prefix eg. Path is appended to beging of url generating urls
	XForwardedPathPrefix = "X-Forwarded-Path-Prefix"
)

Variables

View Source
var EnablePathPrefix = false

EnablePathPrefix is a flag to enable path prefix for entire module

Functions

This section is empty.

Types

type URLHelper

type URLHelper struct {
	// contains filtered or unexported fields
}

URLHelper struct to keep internal state of the request object

func NewURLHelper

func NewURLHelper(req *http.Request) (*URLHelper, error)

NewURLHelper creates a new URLHelper from the http.Request

func (URLHelper) Current

func (uh URLHelper) Current() string

Current retuns the current url with X-Forwarded-Host and X-Forwarded-Proto

func (URLHelper) Join

func (uh URLHelper) Join(pathname string) string

Join returns the absolute of the request joined with the inputed path segment. Keeps query params.

func (URLHelper) JoinWithQuery

func (uh URLHelper) JoinWithQuery(pathname string, query url.Values) string

func (URLHelper) Path

func (uh URLHelper) Path(pathname string) string

Path returns the absolute of the request reset to the inputed path segment. Removes all query params.

func (URLHelper) PathWithQuery

func (uh URLHelper) PathWithQuery(pathname string, query url.Values) string

PathWithQuery returns the absolute of the request reset to the inputed path segment and uses the inputed query params.

func (URLHelper) SchemePlusHost

func (uh URLHelper) SchemePlusHost() string

SchemePlusHost returns the Scheme concatenated with the host but no path

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL