-
Notifications
You must be signed in to change notification settings - Fork 7
/
location.go
38 lines (30 loc) · 1.04 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
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package callhome
import (
"context"
"github.com/ip2location/ip2location-go/v9"
)
var _ LocationService = (*locationService)(nil)
type locationService struct {
db *ip2location.DB
}
// LocationService provides a service for obtaining location information from an IP address.
type LocationService interface {
// GetLocation returns the location information for a given IP address.
GetLocation(ctx context.Context, ip string) (ip2location.IP2Locationrecord, error)
}
// NewLocationService creates a new LocationService that uses the specified IP2Location database file.
func NewLocationService(dbfilepath string) (LocationService, error) {
db, err := ip2location.OpenDB(dbfilepath)
if err != nil {
return nil, err
}
return &locationService{
db: db,
}, nil
}
// GetLocation returns the location information for a given IP address.
func (ls *locationService) GetLocation(ctx context.Context, ip string) (ip2location.IP2Locationrecord, error) {
return ls.db.Get_all(ip)
}