You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
2.7 KiB
Go
137 lines
2.7 KiB
Go
package geoip
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
"go-micro.dev/v4/errors"
|
|
"jochum.dev/jo-micro/auth2"
|
|
"jochum.dev/jo-micro/components"
|
|
"jochum.dev/jo-micro/geoip/cmd/microgeoipd/config"
|
|
"jochum.dev/jo-micro/geoip/proto/geoippb"
|
|
"jochum.dev/jo-micro/geoip/utils"
|
|
)
|
|
|
|
const Name = "geoipClient"
|
|
|
|
type Handler struct {
|
|
initialized bool
|
|
cReg *components.Registry
|
|
}
|
|
|
|
func MustReg(cReg *components.Registry) *Handler {
|
|
return cReg.Must(Name).(*Handler)
|
|
}
|
|
|
|
func New() *Handler {
|
|
return &Handler{
|
|
initialized: false,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) client(ctx context.Context) (geoippb.GeoIPService, context.Context, error) {
|
|
// Wait until the service is here
|
|
_, err := utils.ServiceRetryGet(h.cReg.Service(), config.Name, 10)
|
|
if err != nil {
|
|
return nil, ctx, err
|
|
}
|
|
|
|
service := geoippb.NewGeoIPService(config.Name, h.cReg.Service().Client())
|
|
|
|
// Optional Service Account
|
|
if err := auth2.RegHasClientAuth(h.cReg); err == nil {
|
|
ctx, err = auth2.ClientAuthMustReg(h.cReg).Plugin().ServiceContext(ctx)
|
|
if err != nil {
|
|
return nil, ctx, err
|
|
}
|
|
}
|
|
|
|
return service, ctx, nil
|
|
}
|
|
|
|
func (c *Handler) Priority() int {
|
|
return 30
|
|
}
|
|
|
|
func (c *Handler) Name() string {
|
|
return Name
|
|
}
|
|
|
|
func (c *Handler) Flags(cReg *components.Registry) []cli.Flag {
|
|
return []cli.Flag{}
|
|
}
|
|
|
|
func (c *Handler) Initialized() bool {
|
|
return c.initialized
|
|
}
|
|
|
|
func (h *Handler) Init(cReg *components.Registry, cli *cli.Context) error {
|
|
if h.initialized {
|
|
return errors.InternalServerError("ALREADY_INITIALIZED", "already initialized")
|
|
}
|
|
|
|
h.cReg = cReg
|
|
|
|
h.initialized = true
|
|
return nil
|
|
}
|
|
|
|
func (h *Handler) Stop() error {
|
|
h.initialized = false
|
|
return nil
|
|
}
|
|
|
|
func (h *Handler) Health(context context.Context) error {
|
|
if !h.Initialized() {
|
|
return errors.InternalServerError("NOT_INITIALIZED", "not initialized")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *Handler) Country(ctx context.Context, ip string, languages []string) (*Country, error) {
|
|
client, ctx, err := h.client(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req := &geoippb.IpRequest{
|
|
Ip: ip,
|
|
Languages: languages,
|
|
}
|
|
sCountry, err := client.Country(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Country{
|
|
ISOCode: sCountry.IsoCode,
|
|
CountryName: sCountry.CountryName,
|
|
}, nil
|
|
}
|
|
|
|
func (h *Handler) City(ctx context.Context, ip string, languages []string) (*City, error) {
|
|
client, ctx, err := h.client(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req := &geoippb.IpRequest{
|
|
Ip: ip,
|
|
Languages: languages,
|
|
}
|
|
sCity, err := client.City(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &City{
|
|
ISOCode: sCity.IsoCode,
|
|
TimeZone: sCity.TimeZone,
|
|
CityName: sCity.CityName,
|
|
CountryName: sCity.CountryName,
|
|
Latitude: sCity.Latitude,
|
|
Longitude: sCity.Longitude,
|
|
}, nil
|
|
}
|