-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (62 loc) · 1.49 KB
/
main.go
File metadata and controls
68 lines (62 loc) · 1.49 KB
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
package main
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/ip2location/ip2location-go/v9"
"net"
)
const ERRCODE = -1
const SUCCESSCODE = 1
type IpResponse struct {
Country string
Region string
City string
}
func main() {
db, err := ip2location.OpenDB("./IP2LOCATION-LITE-DB3.BIN")
if err != nil {
panic(err)
return
}
defer db.Close()
r := gin.Default()
r.GET("/ip/:address", func(c *gin.Context) {
address := c.Param("address")
if _, err := validateIpv4Address(address); err != nil {
c.JSON(200, gin.H{
"error": err.Error(),
"code": ERRCODE,
})
return
}
results, err := db.Get_all(address)
if err != nil {
c.JSON(200, gin.H{
"error": err.Error(),
"code": ERRCODE,
})
return
}
var resp IpResponse
resp.Country = results.Country_long
resp.Region = results.Region
resp.City = results.City
c.JSON(200, gin.H{
"error": nil,
"code": SUCCESSCODE,
"data": resp,
})
return
})
r.Run("0.0.0.0:2222")
}
func validateIpv4Address(ip string) (result bool, err error) {
address := net.ParseIP(ip)
if address != nil {
return true, nil
} else {
err = errors.New(ip + "is not a legal ipv4 address")
return false, err
}
return
}