forked from cohousing/location
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation.go
65 lines (50 loc) · 1.4 KB
/
location.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package location
import (
"net/url"
"github.com/gin-gonic/gin"
)
const key = "location"
// Config represents all available options for the middleware.
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
}
// DefaultConfig returns a generic default configuration mapped to localhost.
func DefaultConfig() Config {
return Config{
Host: "localhost:8080",
Scheme: "http",
}
}
// Default returns the location middleware with default configuration.
func Default() gin.HandlerFunc {
config := DefaultConfig()
return New(config)
}
// New returns the location middleware with user-defined custom configuration.
func New(config Config) gin.HandlerFunc {
location := newLocation(config)
return func(c *gin.Context) {
location.applyToContext(c)
}
}
// 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 Get(c *gin.Context) *url.URL {
v, ok := c.Get(key)
if !ok {
return nil
}
vv, ok := v.(*url.URL)
if !ok {
return nil
}
return vv
}