This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
73 lines (58 loc) · 1.38 KB
/
web.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
66
67
68
69
70
71
72
73
package web
import (
"fmt"
"regexp"
"strings"
"github.com/labstack/echo/v4"
"github.com/wangtuanjie/ip17mon"
"github.com/laojianzi/mdclubgo/conf"
)
// IP
func IP(ctx echo.Context) string {
headerKeys := []string{"HTTP_X_FORWARDED_FOR", "HTTP_CLIENT_IP", "REMOTE_ADDR"}
for _, key := range headerKeys {
v := ctx.Request().Header.Get(key)
if v != "" {
return v
}
}
return "0.0.0.0"
}
// Location
func Location(ctx echo.Context, ip string) string {
if ip == "" {
ip = IP(ctx)
}
loc, err := ip17mon.Find(ip)
if err != nil {
return ""
}
return strings.TrimSpace(strings.Join([]string{loc.Country, loc.Region, loc.City}, " "))
}
// IPSign
func IPSign(ctx echo.Context) string {
return strings.Join(regexp.MustCompilePOSIX("[a-z0-9A-Z]*?").FindAllString(IP(ctx), -1), ".")
}
// HostPath
func HostPath(ctx echo.Context) string {
return fmt.Sprintf("%s://%s", ctx.Scheme(), ctx.Request().Host)
}
// StaticPath
func StaticPath(ctx echo.Context, defaultStaticPath string) string {
url := conf.Server.SiteStaticURL
if url == "" {
url = fmt.Sprintf("%s/%s", HostPath(ctx), defaultStaticPath)
}
if url != "" && url[len(url)-1] == '/' {
url = url[:len(url)-1]
}
return url
}
// SupportWebp
func SupportWebp(ctx echo.Context) bool {
accept := ctx.Request().Header.Get("HTTP_ACCEPT")
if accept == "" {
return false
}
return strings.Contains(accept, "image/webp")
}