github.com/infraboard/keyauth@v0.8.1/apps/ip2region/spec.go (about) 1 package ip2region 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "strconv" 8 9 "github.com/infraboard/keyauth/apps/token/session" 10 ) 11 12 // Service todo 13 type Service interface { 14 UpdateDBFile(*UpdateDBFileRequest) error 15 LookupIP(ip string) (*IPInfo, error) 16 } 17 18 // NewDefaultIPInfo todo 19 func NewDefaultIPInfo() *IPInfo { 20 return &IPInfo{} 21 } 22 23 // IPInfo todo 24 type IPInfo struct { 25 CityID int64 `bson:"city_id" json:"city_id"` 26 Country string `bson:"country" json:"country"` 27 Region string `bson:"region" json:"region"` 28 Province string `bson:"province" json:"province"` 29 City string `bson:"city" json:"city"` 30 ISP string `bson:"isp" json:"isp"` 31 } 32 33 func (ip IPInfo) String() string { 34 return strconv.FormatInt(ip.CityID, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.ISP 35 } 36 37 // NewUploadFileRequestFromHTTP todo 38 func NewUploadFileRequestFromHTTP(r *http.Request) (*UpdateDBFileRequest, error) { 39 req := &UpdateDBFileRequest{ 40 reader: r.Body, 41 Session: session.NewSession(), 42 } 43 return req, nil 44 } 45 46 // UpdateDBFileRequest 上传文件请求 47 type UpdateDBFileRequest struct { 48 *session.Session 49 reader io.ReadCloser 50 } 51 52 // Validate 校验参数 53 func (req *UpdateDBFileRequest) Validate() error { 54 if req.reader == nil { 55 return fmt.Errorf("file reader is nil") 56 } 57 58 return nil 59 } 60 61 // ReadCloser todo 62 func (req *UpdateDBFileRequest) ReadCloser() io.ReadCloser { 63 return req.reader 64 }