Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaee4eac

Browse files
committed
开发IP地址定位功能
1 parent88b2c34 commitaee4eac

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

‎common/queryip.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package common
2+
3+
import (
4+
ip2region"github.com/chanyipiaomiao/ip2region/binding/golang"
5+
)
6+
7+
// QueryIP IP查询
8+
typeQueryIPstruct {
9+
DBPathstring
10+
}
11+
12+
// NewQueryIP IP对象
13+
funcNewQueryIP(dbPathstring)*QueryIP {
14+
return&QueryIP{DBPath:dbPath}
15+
}
16+
17+
// Query 根据IP查询
18+
func (q*QueryIP)Query(ipstring) (*ip2region.IpInfo,error) {
19+
region,err:=ip2region.New(q.DBPath)
20+
deferregion.Close()
21+
iferr!=nil {
22+
returnnil,err
23+
}
24+
25+
r,err:=region.MemorySearch(ip)
26+
iferr!=nil {
27+
returnnil,err
28+
}
29+
return&r,nil
30+
}

‎controllers/common.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,8 @@ type DingdingController struct {
134134
typeHolidayControllerstruct {
135135
BaseController
136136
}
137+
138+
// QueryIPController IP地址查询
139+
typeQueryIPControllerstruct {
140+
BaseController
141+
}

‎controllers/queryip.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package controllers
2+
3+
import (
4+
"devops-api/common"
5+
"fmt"
6+
)
7+
8+
// Get Get方法
9+
func (q*QueryIPController)Get() {
10+
requestID:=q.Data["RequestID"].(string)
11+
queryIPLog:=map[string]interface{}{
12+
"entryType":"query ip",
13+
"requestID":requestID,
14+
}
15+
ip:=q.GetString("ip")
16+
qip:=common.NewQueryIP("data/ip2region.db")
17+
r,err:=qip.Query(ip)
18+
iferr!=nil {
19+
queryIPLog["statuscode"]=1
20+
queryIPLog["errmsg"]=fmt.Sprintf("%s",err)
21+
common.GetLogger().Error(queryIPLog,"查询IP地址区域")
22+
q.Data["json"]=queryIPLog
23+
q.ServeJSON()
24+
return
25+
}
26+
queryIPLog["statuscode"]=0
27+
queryIPLog["errmsg"]=""
28+
queryIPLog["ip"]=ip
29+
queryIPLog["ipInfo"]=r
30+
common.GetLogger().Info(queryIPLog,"查询IP地址区域")
31+
q.Data["json"]=queryIPLog
32+
q.ServeJSON()
33+
}

‎routers/router.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ func init() {
4141
beego.NSNamespace("/holiworkday",
4242
beego.NSRouter("",&controllers.HolidayController{}),
4343
),
44+
beego.NSNamespace("/queryip",
45+
beego.NSRouter("",&controllers.QueryIPController{}),
46+
),
4447
),
4548
)
4649
beego.AddNamespace(apins)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp