Add forgoten files

main
René Jochum 2 years ago
parent 73367df5d5
commit 04ead2ae04
Signed by: jochum
GPG Key ID: F7D906F5E51E8E5E

10
.gitignore vendored

@ -0,0 +1,10 @@
.env
.DS_STORE
.task/
!.gitkeep
go.work
go.work.sum

@ -3,7 +3,9 @@
[![Go Reference](https://pkg.go.dev/badge/jochum.dev/jo-micro/geoip.svg)](https://pkg.go.dev/jochum.dev/jo-micro/geoip)
# geoip
/
Maxmind GeoIP2 Service with autoupdates for go-micro
## Developers corner
### Build podman/docker image

@ -0,0 +1,392 @@
package geoiphandler
import (
"context"
"fmt"
"net"
"path/filepath"
"strings"
"sync"
"time"
"go-micro.dev/v4/errors"
"go-micro.dev/v4/server"
"jochum.dev/jo-micro/auth2"
"jochum.dev/jo-micro/auth2/plugins/verifier/endpointroles"
"jochum.dev/jo-micro/geoip/internal/maxmind"
"jochum.dev/jo-micro/geoip/proto/geoippb"
"jochum.dev/jo-micro/geoip/utils"
"jochum.dev/jo-micro/logruscomponent"
"jochum.dev/jo-micro/router"
"github.com/oschwald/geoip2-golang"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"jochum.dev/jo-micro/components"
)
type Config struct {
RefreshSeconds int
AccountID string
LicenseKey string
DataDirectory string
}
type Handler struct {
sync.RWMutex
initialized bool
cReg *components.Registry
config Config
refreshCancel context.CancelFunc
dbCity *geoip2.Reader
dbCountry *geoip2.Reader
dbASN *geoip2.Reader
}
const Name = "geoipHandler"
func New() *Handler {
return &Handler{initialized: false}
}
func MustReg(cReg *components.Registry) *Handler {
return cReg.Must(Name).(*Handler)
}
func (h *Handler) Name() string {
return Name
}
func (h *Handler) Priority() int {
return 100
}
func (h *Handler) Initialized() bool {
return h.initialized
}
func (h *Handler) Init(cReg *components.Registry, cli *cli.Context) error {
if h.initialized {
return nil
}
h.cReg = cReg
logrus := logruscomponent.MustReg(h.cReg).Logger()
r := router.MustReg(h.cReg)
r.Add(
router.NewRoute(
router.Method(router.MethodGet),
router.Path("/country/:ip"),
router.Endpoint(geoippb.GeoIPService.Country),
router.Params("ip"),
router.AuthRequired(),
router.RatelimitUser("1-S", "10-M"),
),
router.NewRoute(
router.Method(router.MethodGet),
router.Path("/country/:ip/:commaLanguages"),
router.Endpoint(geoippb.GeoIPService.Country),
router.Params("ip", "commaLanguages"),
router.AuthRequired(),
router.RatelimitUser("1-S", "10-M"),
),
router.NewRoute(
router.Method(router.MethodGet),
router.Path("/city/:ip"),
router.Endpoint(geoippb.GeoIPService.City),
router.Params("ip"),
router.AuthRequired(),
router.RatelimitUser("1-S", "10-M"),
),
router.NewRoute(
router.Method(router.MethodGet),
router.Path("/city/:ip/:commaLanguages"),
router.Endpoint(geoippb.GeoIPService.City),
router.Params("ip", "commaLanguages"),
router.AuthRequired(),
router.RatelimitUser("1-S", "10-M"),
),
)
authVerifier := endpointroles.NewVerifier(
endpointroles.WithLogrus(logruscomponent.MustReg(h.cReg).Logger()),
)
authVerifier.AddRules(
endpointroles.NewRule(
endpointroles.Endpoint(geoippb.GeoIPService.Country),
endpointroles.RolesAllow(auth2.RolesServiceAndAdmin),
),
endpointroles.NewRule(
endpointroles.Endpoint(geoippb.GeoIPService.City),
endpointroles.RolesAllow(auth2.RolesServiceAndAdmin),
),
)
auth2.ClientAuthMustReg(h.cReg).Plugin().AddVerifier(authVerifier)
if cli.String(fmt.Sprintf("%s_maxmind_account_id", strings.ToLower(cReg.FlagPrefix()))) == "" || cli.String(fmt.Sprintf("%s_maxmind_license_key", strings.ToLower(cReg.FlagPrefix()))) == "" {
err := errors.InternalServerError("NO_MAXMIND_ACCOUNT", "you have to provide geoip_maxmind_account_id and geoip_maxmind_license_key")
logrus.Fatal(err)
return err
}
if err := utils.IsDirectoryAndWriteable(cli.String(fmt.Sprintf("%s_data_directory", strings.ToLower(cReg.FlagPrefix())))); err != nil {
logrus.Fatal(err)
return err
}
h.config = Config{
DataDirectory: cli.String(fmt.Sprintf("%s_data_directory", strings.ToLower(cReg.FlagPrefix()))),
RefreshSeconds: cli.Int(fmt.Sprintf("%s_refreshdb", strings.ToLower(cReg.FlagPrefix()))),
AccountID: cli.String(fmt.Sprintf("%s_maxmind_account_id", strings.ToLower(cReg.FlagPrefix()))),
LicenseKey: cli.String(fmt.Sprintf("%s_maxmind_license_key", strings.ToLower(cReg.FlagPrefix()))),
}
refreshContext, cancel := context.WithCancel(context.Background())
go func(ctx context.Context) {
ticker := time.NewTicker(time.Duration(h.config.RefreshSeconds) * time.Second)
client, err := maxmind.New(maxmind.Config{
Logrus: logrus,
DownloadDirectory: h.config.DataDirectory,
LicenseKey: h.config.LicenseKey,
})
if err != nil {
logrus.Error(err)
return
}
for {
downloader, err := client.NewDownloader(maxmind.EIDGeoLite2Country)
if err != nil {
logrus.Error(err)
return
}
_, err = downloader.Download()
if err != nil {
logrus.Error(err)
} else {
db, err := geoip2.Open(filepath.Join(h.config.DataDirectory, "GeoLite2-Country.mmdb"))
if err != nil {
logrus.Error(err)
} else {
h.Lock()
h.dbCountry = db
h.Unlock()
}
}
downloader, err = client.NewDownloader(maxmind.EIDGeoLite2City)
if err != nil {
logrus.Error(err)
return
}
_, err = downloader.Download()
if err != nil {
logrus.Error(err)
continue
} else {
db, err := geoip2.Open(filepath.Join(h.config.DataDirectory, "GeoLite2-City.mmdb"))
if err != nil {
logrus.Error(err)
} else {
h.Lock()
h.dbCity = db
h.Unlock()
}
}
downloader, err = client.NewDownloader(maxmind.EIDGeoLite2ASN)
if err != nil {
logrus.Error(err)
return
}
_, err = downloader.Download()
if err != nil {
logrus.Error(err)
continue
} else {
db, err := geoip2.Open(filepath.Join(h.config.DataDirectory, "GeoLite2-ASN.mmdb"))
if err != nil {
logrus.Error(err)
} else {
h.Lock()
h.dbASN = db
h.Unlock()
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
continue
}
}
}(refreshContext)
h.refreshCancel = cancel
geoippb.RegisterGeoIPServiceHandler(h.cReg.Service().Server(), h)
h.initialized = true
return nil
}
func (h *Handler) Stop() error {
h.refreshCancel()
h.Lock()
if h.dbCountry != nil {
if err := h.dbCountry.Close(); err != nil {
logrus.Error(err)
}
}
if h.dbCity != nil {
if err := h.dbCity.Close(); err != nil {
logrus.Error(err)
}
}
if h.dbASN != nil {
if err := h.dbASN.Close(); err != nil {
logrus.Error(err)
}
}
h.Unlock()
h.initialized = false
return nil
}
func (h *Handler) Flags(r *components.Registry) []cli.Flag {
return []cli.Flag{
&cli.IntFlag{
Name: fmt.Sprintf("%s_refreshdb", strings.ToLower(r.FlagPrefix())),
Usage: "Refresh the DB every x seconds, default is every half day",
EnvVars: []string{fmt.Sprintf("%s_REFRESHDB", strings.ToUpper(r.FlagPrefix()))},
Value: 43200,
},
&cli.StringFlag{
Name: fmt.Sprintf("%s_data_directory", strings.ToLower(r.FlagPrefix())),
Usage: "GeoIP data directory",
EnvVars: []string{fmt.Sprintf("%s_DATA_DIRECTORY", strings.ToUpper(r.FlagPrefix()))},
Value: "/data",
},
&cli.StringFlag{
Name: fmt.Sprintf("%s_maxmind_account_id", strings.ToLower(r.FlagPrefix())),
Usage: "Maxmind account ID",
EnvVars: []string{fmt.Sprintf("%s_MAXMIND_ACCOUNT_ID", strings.ToUpper(r.FlagPrefix()))},
},
&cli.StringFlag{
Name: fmt.Sprintf("%s_maxmind_license_key", strings.ToLower(r.FlagPrefix())),
Usage: "Maxmind license key",
EnvVars: []string{fmt.Sprintf("%s_MAXMIND_LICENSE_KEY", strings.ToUpper(r.FlagPrefix()))},
},
}
}
func (h *Handler) Health(context context.Context) error {
return nil
}
func (h *Handler) WrapHandlerFunc(ctx context.Context, req server.Request, rsp interface{}) error {
return nil
}
func (h *Handler) Country(ctx context.Context, in *geoippb.IpRequest, out *geoippb.CountryResponse) error {
ip := net.ParseIP(in.Ip)
if ip == nil {
return errors.BadRequest("INVALID_IP", "invalid ip '%s' given", in.Ip)
}
h.RLock()
if h.dbCountry == nil {
h.RUnlock()
return errors.InternalServerError("DB_NOT_READY_YET", "maxmind country database is not ready yet")
}
record, err := h.dbCountry.Country(ip)
h.RUnlock()
if err != nil {
logrus.Error(err)
}
out.IsoCode = record.Country.IsoCode
out.CountryName = make(map[string]string)
for _, lang := range in.Languages {
if n, ok := record.Country.Names[lang]; ok {
out.CountryName[lang] = n
}
}
if in.CommaLanguages != "" {
for _, lang := range strings.Split(in.CommaLanguages, ",") {
if n, ok := record.Country.Names[lang]; ok {
out.CountryName[lang] = n
}
}
}
return nil
}
func (h *Handler) City(ctx context.Context, in *geoippb.IpRequest, out *geoippb.CityResponse) error {
ip := net.ParseIP(in.Ip)
if ip == nil {
return errors.BadRequest("INVALID_IP", "invalid ip '%s' given", in.Ip)
}
h.RLock()
if h.dbCity == nil {
h.RUnlock()
return errors.InternalServerError("DB_NOT_READY_YET", "maxmind country database is not ready yet")
}
record, err := h.dbCity.City(ip)
h.RUnlock()
if err != nil {
logrus.Error(err)
}
out.IsoCode = record.Country.IsoCode
out.TimeZone = record.Location.TimeZone
out.Latitude = record.Location.Latitude
out.Longitude = record.Location.Longitude
out.CountryName = make(map[string]string)
for _, lang := range in.Languages {
if n, ok := record.Country.Names[lang]; ok {
out.CountryName[lang] = n
}
}
if in.CommaLanguages != "" {
for _, lang := range strings.Split(in.CommaLanguages, ",") {
if n, ok := record.Country.Names[lang]; ok {
out.CountryName[lang] = n
}
}
}
out.CityName = make(map[string]string)
for _, lang := range in.Languages {
if n, ok := record.City.Names[lang]; ok {
out.CityName[lang] = n
}
}
if in.CommaLanguages != "" {
for _, lang := range strings.Split(in.CommaLanguages, ",") {
if n, ok := record.City.Names[lang]; ok {
out.CityName[lang] = n
}
}
}
return nil
}

@ -0,0 +1,136 @@
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
}

@ -16,7 +16,7 @@ require (
github.com/urfave/cli/v2 v2.16.3
go-micro.dev/v4 v4.8.1
google.golang.org/protobuf v1.28.1
jochum.dev/jo-micro/auth2 v0.5.5
jochum.dev/jo-micro/auth2 v0.5.6
jochum.dev/jo-micro/components v0.3.2
jochum.dev/jo-micro/logruscomponent v0.0.5
jochum.dev/jo-micro/router v0.4.10

@ -297,6 +297,8 @@ jochum.dev/jo-micro/auth2 v0.5.4 h1:gHF84nYpFBUBYiQy/2411QeRRSlskXkK45+NgNypt5s=
jochum.dev/jo-micro/auth2 v0.5.4/go.mod h1:2tJTA5Hy5/A9XBsaKQuFsSM+ukrSRuJ1uTNU77Rir7A=
jochum.dev/jo-micro/auth2 v0.5.5 h1:M3o8gye0TROScFyyubCyMhd7dvOblcILbmX7hn+YC2A=
jochum.dev/jo-micro/auth2 v0.5.5/go.mod h1:SIgJ4EXjhX/H/IsG6ZGP6+WvJGAFmPCHyHe35TixlBU=
jochum.dev/jo-micro/auth2 v0.5.6 h1:XfEqhSex6guh9ogfGpRoYjW+RjEW8dAK2u0mHdqVZ28=
jochum.dev/jo-micro/auth2 v0.5.6/go.mod h1:6+SpPpiER7v4WQRWT2oMq3mnXDXqQM7wIJEl7vmrnMQ=
jochum.dev/jo-micro/components v0.3.2 h1:Z6Od76Uh2C2+bKhfZvaDLbry8vWGe4Ie/rDfrObE1pg=
jochum.dev/jo-micro/components v0.3.2/go.mod h1:MXpsIY5Gut4/wDNquiCN+e4zYtATVwn+7uiNKj4nlKk=
jochum.dev/jo-micro/logruscomponent v0.0.4 h1:KkJhLIM0mm2tlk+z+gZdpb5vCeuBs6bhG3bncGhCqoQ=

@ -0,0 +1,15 @@
package geoip
type Country struct {
ISOCode string
CountryName map[string]string
}
type City struct {
ISOCode string
TimeZone string
CityName map[string]string
CountryName map[string]string
Latitude float64
Longitude float64
}

@ -0,0 +1,386 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.21.5
// source: geoippb.proto
package geoippb
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type IpRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
Languages []string `protobuf:"bytes,2,rep,name=languages,proto3" json:"languages,omitempty"` // Optional
CommaLanguages string `protobuf:"bytes,3,opt,name=commaLanguages,proto3" json:"commaLanguages,omitempty"` // Optional for url requests
}
func (x *IpRequest) Reset() {
*x = IpRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_geoippb_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *IpRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*IpRequest) ProtoMessage() {}
func (x *IpRequest) ProtoReflect() protoreflect.Message {
mi := &file_geoippb_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use IpRequest.ProtoReflect.Descriptor instead.
func (*IpRequest) Descriptor() ([]byte, []int) {
return file_geoippb_proto_rawDescGZIP(), []int{0}
}
func (x *IpRequest) GetIp() string {
if x != nil {
return x.Ip
}
return ""
}
func (x *IpRequest) GetLanguages() []string {
if x != nil {
return x.Languages
}
return nil
}
func (x *IpRequest) GetCommaLanguages() string {
if x != nil {
return x.CommaLanguages
}
return ""
}
type CountryResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
IsoCode string `protobuf:"bytes,1,opt,name=isoCode,proto3" json:"isoCode,omitempty"`
CountryName map[string]string `protobuf:"bytes,2,rep,name=countryName,proto3" json:"countryName,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
}
func (x *CountryResponse) Reset() {
*x = CountryResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_geoippb_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CountryResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CountryResponse) ProtoMessage() {}
func (x *CountryResponse) ProtoReflect() protoreflect.Message {
mi := &file_geoippb_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CountryResponse.ProtoReflect.Descriptor instead.
func (*CountryResponse) Descriptor() ([]byte, []int) {
return file_geoippb_proto_rawDescGZIP(), []int{1}
}
func (x *CountryResponse) GetIsoCode() string {
if x != nil {
return x.IsoCode
}
return ""
}
func (x *CountryResponse) GetCountryName() map[string]string {
if x != nil {
return x.CountryName
}
return nil
}
type CityResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
IsoCode string `protobuf:"bytes,1,opt,name=isoCode,proto3" json:"isoCode,omitempty"`
TimeZone string `protobuf:"bytes,2,opt,name=timeZone,proto3" json:"timeZone,omitempty"`
CityName map[string]string `protobuf:"bytes,3,rep,name=cityName,proto3" json:"cityName,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
CountryName map[string]string `protobuf:"bytes,4,rep,name=countryName,proto3" json:"countryName,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
Latitude float64 `protobuf:"fixed64,5,opt,name=latitude,proto3" json:"latitude,omitempty"`
Longitude float64 `protobuf:"fixed64,6,opt,name=longitude,proto3" json:"longitude,omitempty"`
}
func (x *CityResponse) Reset() {
*x = CityResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_geoippb_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CityResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CityResponse) ProtoMessage() {}
func (x *CityResponse) ProtoReflect() protoreflect.Message {
mi := &file_geoippb_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CityResponse.ProtoReflect.Descriptor instead.
func (*CityResponse) Descriptor() ([]byte, []int) {
return file_geoippb_proto_rawDescGZIP(), []int{2}
}
func (x *CityResponse) GetIsoCode() string {
if x != nil {
return x.IsoCode
}
return ""
}
func (x *CityResponse) GetTimeZone() string {
if x != nil {
return x.TimeZone
}
return ""
}
func (x *CityResponse) GetCityName() map[string]string {
if x != nil {
return x.CityName
}
return nil
}
func (x *CityResponse) GetCountryName() map[string]string {
if x != nil {
return x.CountryName
}
return nil
}
func (x *CityResponse) GetLatitude() float64 {
if x != nil {
return x.Latitude
}
return 0
}
func (x *CityResponse) GetLongitude() float64 {
if x != nil {
return x.Longitude
}
return 0
}
var File_geoippb_proto protoreflect.FileDescriptor
var file_geoippb_proto_rawDesc = []byte{
0x0a, 0x0d, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
0x07, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x22, 0x61, 0x0a, 0x09, 0x49, 0x70, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x02, 0x69, 0x70, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67,
0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x09, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61,
0x67, 0x65, 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x4c, 0x61, 0x6e, 0x67,
0x75, 0x61, 0x67, 0x65, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x63, 0x6f, 0x6d,
0x6d, 0x61, 0x4c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x73, 0x22, 0xb8, 0x01, 0x0a, 0x0f,
0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12,
0x18, 0x0a, 0x07, 0x69, 0x73, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x07, 0x69, 0x73, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x4b, 0x0a, 0x0b, 0x63, 0x6f, 0x75,
0x6e, 0x74, 0x72, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29,
0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79,
0x4e, 0x61, 0x6d, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x0b, 0x63, 0x6f, 0x75, 0x6e, 0x74,
0x72, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x1a, 0x3e, 0x0a, 0x10, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72,
0x79, 0x4e, 0x61, 0x6d, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65,
0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05,
0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c,
0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x86, 0x03, 0x0a, 0x0c, 0x43, 0x69, 0x74, 0x79, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x69, 0x73, 0x6f, 0x43, 0x6f,
0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x69, 0x73, 0x6f, 0x43, 0x6f, 0x64,
0x65, 0x12, 0x1a, 0x0a, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x5a, 0x6f, 0x6e, 0x65, 0x18, 0x02, 0x20,
0x01, 0x28, 0x09, 0x52, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x5a, 0x6f, 0x6e, 0x65, 0x12, 0x3f, 0x0a,
0x08, 0x63, 0x69, 0x74, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32,
0x23, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x43, 0x69, 0x74, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x45,
0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x63, 0x69, 0x74, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x48,
0x0a, 0x0b, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20,
0x03, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x2e, 0x43, 0x69,
0x74, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74,
0x72, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x0b, 0x63, 0x6f, 0x75,
0x6e, 0x74, 0x72, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x61, 0x74, 0x69,
0x74, 0x75, 0x64, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x6c, 0x61, 0x74, 0x69,
0x74, 0x75, 0x64, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64,
0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x01, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75,
0x64, 0x65, 0x1a, 0x3b, 0x0a, 0x0d, 0x43, 0x69, 0x74, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x45, 0x6e,
0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x1a,
0x3e, 0x0a, 0x10, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x4e, 0x61, 0x6d, 0x65, 0x45, 0x6e,
0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x32,
0x7e, 0x0a, 0x0c, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12,
0x39, 0x0a, 0x07, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x12, 0x2e, 0x67, 0x65, 0x6f,
0x69, 0x70, 0x70, 0x62, 0x2e, 0x49, 0x70, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18,
0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x33, 0x0a, 0x04, 0x43, 0x69,
0x74, 0x79, 0x12, 0x12, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x2e, 0x49, 0x70, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62,
0x2e, 0x43, 0x69, 0x74, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42,
0x31, 0x5a, 0x2f, 0x6a, 0x6f, 0x63, 0x68, 0x75, 0x6d, 0x2e, 0x64, 0x65, 0x76, 0x2f, 0x6a, 0x6f,
0x2d, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x2f, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x2f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x70, 0x62, 0x3b, 0x67, 0x65, 0x6f, 0x69, 0x70,
0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_geoippb_proto_rawDescOnce sync.Once
file_geoippb_proto_rawDescData = file_geoippb_proto_rawDesc
)
func file_geoippb_proto_rawDescGZIP() []byte {
file_geoippb_proto_rawDescOnce.Do(func() {
file_geoippb_proto_rawDescData = protoimpl.X.CompressGZIP(file_geoippb_proto_rawDescData)
})
return file_geoippb_proto_rawDescData
}
var file_geoippb_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
var file_geoippb_proto_goTypes = []interface{}{
(*IpRequest)(nil), // 0: geoippb.IpRequest
(*CountryResponse)(nil), // 1: geoippb.CountryResponse
(*CityResponse)(nil), // 2: geoippb.CityResponse
nil, // 3: geoippb.CountryResponse.CountryNameEntry
nil, // 4: geoippb.CityResponse.CityNameEntry
nil, // 5: geoippb.CityResponse.CountryNameEntry
}
var file_geoippb_proto_depIdxs = []int32{
3, // 0: geoippb.CountryResponse.countryName:type_name -> geoippb.CountryResponse.CountryNameEntry
4, // 1: geoippb.CityResponse.cityName:type_name -> geoippb.CityResponse.CityNameEntry
5, // 2: geoippb.CityResponse.countryName:type_name -> geoippb.CityResponse.CountryNameEntry
0, // 3: geoippb.GeoIPService.Country:input_type -> geoippb.IpRequest
0, // 4: geoippb.GeoIPService.City:input_type -> geoippb.IpRequest
1, // 5: geoippb.GeoIPService.Country:output_type -> geoippb.CountryResponse
2, // 6: geoippb.GeoIPService.City:output_type -> geoippb.CityResponse
5, // [5:7] is the sub-list for method output_type
3, // [3:5] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
}
func init() { file_geoippb_proto_init() }
func file_geoippb_proto_init() {
if File_geoippb_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_geoippb_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*IpRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_geoippb_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CountryResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_geoippb_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CityResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_geoippb_proto_rawDesc,
NumEnums: 0,
NumMessages: 6,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_geoippb_proto_goTypes,
DependencyIndexes: file_geoippb_proto_depIdxs,
MessageInfos: file_geoippb_proto_msgTypes,
}.Build()
File_geoippb_proto = out.File
file_geoippb_proto_rawDesc = nil
file_geoippb_proto_goTypes = nil
file_geoippb_proto_depIdxs = nil
}

@ -0,0 +1,104 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: geoippb.proto
package geoippb
import (
fmt "fmt"
proto "google.golang.org/protobuf/proto"
math "math"
)
import (
context "context"
api "go-micro.dev/v4/api"
client "go-micro.dev/v4/client"
server "go-micro.dev/v4/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for GeoIPService service
func NewGeoIPServiceEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for GeoIPService service
type GeoIPService interface {
Country(ctx context.Context, in *IpRequest, opts ...client.CallOption) (*CountryResponse, error)
City(ctx context.Context, in *IpRequest, opts ...client.CallOption) (*CityResponse, error)
}
type geoIPService struct {
c client.Client
name string
}
func NewGeoIPService(name string, c client.Client) GeoIPService {
return &geoIPService{
c: c,
name: name,
}
}
func (c *geoIPService) Country(ctx context.Context, in *IpRequest, opts ...client.CallOption) (*CountryResponse, error) {
req := c.c.NewRequest(c.name, "GeoIPService.Country", in)
out := new(CountryResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *geoIPService) City(ctx context.Context, in *IpRequest, opts ...client.CallOption) (*CityResponse, error) {
req := c.c.NewRequest(c.name, "GeoIPService.City", in)
out := new(CityResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for GeoIPService service
type GeoIPServiceHandler interface {
Country(context.Context, *IpRequest, *CountryResponse) error
City(context.Context, *IpRequest, *CityResponse) error
}
func RegisterGeoIPServiceHandler(s server.Server, hdlr GeoIPServiceHandler, opts ...server.HandlerOption) error {
type geoIPService interface {
Country(ctx context.Context, in *IpRequest, out *CountryResponse) error
City(ctx context.Context, in *IpRequest, out *CityResponse) error
}
type GeoIPService struct {
geoIPService
}
h := &geoIPServiceHandler{hdlr}
return s.Handle(s.NewHandler(&GeoIPService{h}, opts...))
}
type geoIPServiceHandler struct {
GeoIPServiceHandler
}
func (h *geoIPServiceHandler) Country(ctx context.Context, in *IpRequest, out *CountryResponse) error {
return h.GeoIPServiceHandler.Country(ctx, in, out)
}
func (h *geoIPServiceHandler) City(ctx context.Context, in *IpRequest, out *CityResponse) error {
return h.GeoIPServiceHandler.City(ctx, in, out)
}

@ -0,0 +1,25 @@
package utils
import (
"os"
"path/filepath"
"go-micro.dev/v4/errors"
)
func IsDirectoryAndWriteable(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if !info.IsDir() {
return errors.InternalServerError("NOT_A_DIRECTORY", "'%s' is not a directory", path)
}
f := filepath.Join(path, ".touch")
if err := os.WriteFile(f, []byte(""), 0o600); err != nil {
return errors.InternalServerError("DIRECTORY_NOT_WRITEABLE", "directory '%s' is not writeable: %s", path, err)
}
return os.Remove(f)
}

@ -0,0 +1,46 @@
package utils
import (
"fmt"
"github.com/avast/retry-go/v4"
"go-micro.dev/v4"
)
func ServiceRetryGet(service micro.Service, svcName string, attempts uint) (string, error) {
r := service.Options().Registry
var (
hostAndPort string
)
err := retry.Do(
func() error {
services, err := r.GetService(svcName)
if err == nil {
for _, s := range services {
for _, n := range s.Nodes {
hostAndPort = n.Address
break
}
if hostAndPort != "" {
break
}
}
}
if hostAndPort == "" {
return fmt.Errorf("Service %v not found", svcName)
}
return nil
},
retry.Attempts(attempts),
)
if err != nil {
return "", err
}
return hostAndPort, nil
}
Loading…
Cancel
Save