Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gin-contrib/location
gin middleware to expose the server's hostname and scheme
https://github.com/gin-contrib/location
Last synced: 2 months ago
JSON representation
gin middleware to expose the server's hostname and scheme
- Host: GitHub
- URL: https://github.com/gin-contrib/location
- Owner: gin-contrib
- License: mit
- Created: 2016-04-11T19:58:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-05-03T04:07:44.000Z (9 months ago)
- Last Synced: 2024-05-03T13:20:04.804Z (9 months ago)
- Language: Go
- Homepage:
- Size: 89.8 KB
- Stars: 89
- Watchers: 10
- Forks: 20
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gin - gin-contrib/location
README
# location
[![Run Tests](https://github.com/gin-contrib/location/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/gin-contrib/location/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/gin-contrib/location/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/location)
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/location)](https://goreportcard.com/report/github.com/gin-contrib/location)
[![GoDoc](https://godoc.org/github.com/gin-contrib/location?status.svg)](https://godoc.org/github.com/gin-contrib/location)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:
```bash
go get github.com/gin-contrib/location
```Import it in your code:
```go
import "github.com/gin-contrib/location"
```### Default
```go
package mainimport (
"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
```go
package mainimport (
"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