location

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 4 Imported by: 87

README

location

Run Tests codecov Go Report Card GoDoc

This Gin middleware can be used to automatically find and expose the server's hostname and scheme by inspecting information in the incoming http.Request. The alternative to this plugin would be explicitly providing such information to the server as a command line argument or environment variable.

Usage

Start using it

Download and install it:

go get github.com/gin-contrib/location

Import it in your code:

import "github.com/gin-contrib/location"
Default
package main

import (
  "github.com/gin-contrib/location"
  "github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()

  // configure to automatically detect scheme and host
  // - use http when default scheme cannot be determined
  // - use localhost:8080 when default host cannot be determined
  router.Use(location.Default())

  router.GET("/", func(c *gin.Context) {
    url := location.Get(c)

    // url.Scheme
    // url.Host
    // url.Path
  })

  router.Run()
}
Custom
package main

import (
  "github.com/gin-contrib/location"
  "github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()

  // configure to automatically detect scheme and host with
  // fallback to https://foo.com/base
  // - use https when default scheme cannot be determined
  // - use foo.com when default host cannot be determined
  // - include /base as the path
  router.Use(location.New(location.Config{
    Scheme: "https",
    Host: "foo.com",
    Base: "/base",
    Headers: location.Headers{Scheme: "X-Forwarded-Proto", Host: "X-Forwarded-For"},
  }))

  router.GET("/", func(c *gin.Context) {
    url := location.Get(c)

    // url.Scheme
    // url.Host
    // url.Path
  })

  router.Run()
}

Contributing

Fork -> Patch -> Push -> Pull Request

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() gin.HandlerFunc

Default returns the location middleware with default configuration.

func Get

func Get(c *gin.Context) *url.URL

Get returns the Location information for the incoming http.Request from the context. If the location is not set a nil value is returned.

func New

func New(config Config) gin.HandlerFunc

New returns the location middleware with user-defined custom configuration.

Types

type Config

type Config struct {
	// Scheme is the default scheme that should be used when it cannot otherwise
	// be ascertained from the incoming http.Request.
	Scheme string

	// Host is the default host that should be used when it cannot otherwise
	// be ascertained from the incoming http.Request.
	Host string

	// Base is the base path that should be used in conjunction with proxy
	// servers that do path re-writing.
	Base string

	// Header used to map schemes and host.
	// May be overriden to allow reading values from custom header fields.
	Headers Headers
}

Config represents all available options for the middleware.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a generic default configuration mapped to localhost.

type Headers added in v0.0.2

type Headers struct {
	Scheme string
	Host   string
}

Headers represents the header fields used to map schemes and host.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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