Skip to content

gin middleware to expose the server's hostname and scheme

License

Notifications You must be signed in to change notification settings

cohousing/location

This branch is 2 commits ahead of, 79 commits behind gin-contrib/location:master.

Repository files navigation

location

Build Status codecov Go Report Card GoDoc Join the chat at https://gitter.im/gin-gonic/gin

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

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",
	}))

	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

About

gin middleware to expose the server's hostname and scheme

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%