Add README with installation and usage examples for GeoIP2 package
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
# geoip2
|
||||
|
||||
Получение данных с сервиса maxmind.com. Изначально пакет пишется для сервиса https://git.kor-elf.net/kor-elf-shield/kor-elf-shield.
|
||||
|
||||
## Установка
|
||||
```sh
|
||||
go get git.kor-elf.net/kor-elf-shield/geoip2
|
||||
```
|
||||
|
||||
## Примеры использования
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
|
||||
"git.kor-elf.net/kor-elf-shield/geoip2/service/maxmind/mmdb"
|
||||
)
|
||||
|
||||
type Logger struct{}
|
||||
|
||||
func (l *Logger) Error(err error) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Для получения username и password ознакомьтесь тут: https://support.maxmind.com/knowledge-base/articles/create-a-maxmind-account
|
||||
username := "username"
|
||||
password := "password"
|
||||
download, err := mmdb.NewDownload(mmdb.DownloadURLCityLite, username, password, mmdb.DefaultDownloadConfig())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
city := mmdb.NewCity(download, &Logger{}, "tmp", mmdb.LanguageRussian)
|
||||
defer func() {
|
||||
_ = city.Close()
|
||||
}()
|
||||
|
||||
/*
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Если хотите обновить данные
|
||||
if err := city.Refresh(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
*/
|
||||
|
||||
// Вместо 127.0.0.1 укажите интересующиеся IP адрес
|
||||
ip, err := netip.ParseAddr("127.0.0.1")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
info, err := city.Info(ip)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(info.ToString())
|
||||
}
|
||||
```
|
||||
## Лицензия
|
||||
|
||||
[MIT](https://git.kor-elf.net/kor-elf-shield/geoip2/src/branch/main/LICENSE)
|
||||
Reference in New Issue
Block a user