Skip to content

Commit

Permalink
Add go.mod; Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
night-codes committed Feb 4, 2022
1 parent 58e4e9a commit 882eacd
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.a
test
*.exe
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
# go-sypexgeo

SypexGeo API bindings for Go (provides access data from [SypexGeo](https://sypexgeo.net/) IP database files). Accepts only SypexGeo 2.2 or above format.

For more information about Sypex Geo databases and their features please visit http://sypexgeo.net.

Direct DB links that might became broken over time:

* Sypex Geo City DB (free) - http://sypexgeo.net/files/SxGeoCity_utf8.zip
* Sypex Geo City Max DB - https://sypexgeo.net/ru/buy/

- Sypex Geo City DB (free) - http://sypexgeo.net/files/SxGeoCity_utf8.zip
- Sypex Geo City Max DB - https://sypexgeo.net/ru/buy/

## Documentation
[Docs on godoc.org](https://godoc.org/gopkg.in/night-codes/go-sypexgeo.v1)

[Docs on pkg.go.dev](https://pkg.go.dev/github.com/night-codes/go-sypexgeo)

## How To Install

## How To Install
```bash
go get gopkg.in/night-codes/go-sypexgeo.v1
go get github.com/night-codes/go-sypexgeo
```



## Getting Started

```go
package main

import "gopkg.in/night-codes/go-sypexgeo.v1"
import "github.com/night-codes/go-sypexgeo"

func main() {
geo := sypexgeo.New("/path/to/db/SxGeoCity.dat")
info, err := geo.GetCityFull("93.73.35.74")
}
```


## License
MIT License
Copyright (C) 2015 Oleksiy Chechel ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

MIT License

Copyright (C) 2015-2022 Oleksiy Chechel ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions examples/clib/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module go-sypexgeo/examples/clib

go 1.16

require (
github.com/night-codes/conv v1.0.2
github.com/night-codes/go-sypexgeo v1.0.0
)
4 changes: 4 additions & 0 deletions examples/clib/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/night-codes/conv v1.0.2 h1:0u4R30EOHAN2aJK4yMdCyidpIEVUzpoZxUUIq7bD9ro=
github.com/night-codes/conv v1.0.2/go.mod h1:Lb9+y5Znhtq1/e2hJ93+FXkbLYyqAzyYe1tcSZvQa0k=
github.com/night-codes/go-sypexgeo v0.0.0-20220204130107-58e4e9af21d4 h1:bdTnhJ5mYHa5q8Hsq+6bhsZe8Nadt2LTYqMhYfpE/Og=
github.com/night-codes/go-sypexgeo v0.0.0-20220204130107-58e4e9af21d4/go.mod h1:a1CTVmL98MYMFqhW+ulcA1Rk5gX6MMDOztUFFbmyEhk=
41 changes: 21 additions & 20 deletions examples/clib/sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

/*
#include <stdlib.h>
#ifndef GO_SYPEGEO
#define GO_SYPEGEO
Expand Down Expand Up @@ -50,8 +51,8 @@ static void spxFree(spxInfo s) {
*/
import "C"
import (
"github.com/night-codes/conv"
"github.com/night-codes/go-sypexgeo"
"gopkg.in/night-codes/types.v1"
)

var geo sypexgeo.SxGEO
Expand All @@ -64,7 +65,7 @@ func spxConnect(c *C.char) {

//export spxGetCity
func spxGetCity(ip *C.char, full C.int) (ret C.spxInfo) {
var info sypexgeo.Obj
var info map[string]interface{}
var err error
if int(full) == 1 {
info, err = geo.GetCityFull(C.GoString(ip))
Expand All @@ -75,31 +76,31 @@ func spxGetCity(ip *C.char, full C.int) (ret C.spxInfo) {
if err != nil {
ret.err = 1
} else {
city, _ := info["city"].(sypexgeo.Obj)
country, _ := info["country"].(sypexgeo.Obj)
region, _ := info["region"].(sypexgeo.Obj)
city, _ := info["city"].(map[string]interface{})
country, _ := info["country"].(map[string]interface{})
region, _ := info["region"].(map[string]interface{})

ret = C.spxInfo{
city: C.spxCity{
id: C.int(types.Int(city["id"])),
lat: C.float(types.Float32(city["lat"])),
lon: C.float(types.Float32(city["lon"])),
name_en: C.CString(types.String(city["name_en"])),
name_ru: C.CString(types.String(city["name_ru"])),
id: C.int(conv.Int(city["id"])),
lat: C.float(conv.Float32(city["lat"])),
lon: C.float(conv.Float32(city["lon"])),
name_en: C.CString(conv.String(city["name_en"])),
name_ru: C.CString(conv.String(city["name_ru"])),
},
country: C.spxCountry{
id: C.int(types.Int(country["id"])),
iso: C.CString(types.String(country["iso"])),
lat: C.float(types.Float32(country["lat"])),
lon: C.float(types.Float32(country["lon"])),
name_en: C.CString(types.String(country["name_en"])),
name_ru: C.CString(types.String(country["name_ru"])),
id: C.int(conv.Int(country["id"])),
iso: C.CString(conv.String(country["iso"])),
lat: C.float(conv.Float32(country["lat"])),
lon: C.float(conv.Float32(country["lon"])),
name_en: C.CString(conv.String(country["name_en"])),
name_ru: C.CString(conv.String(country["name_ru"])),
},
region: C.spxRegion{
id: C.int(types.Int(region["id"])),
iso: C.CString(types.String(region["iso"])),
name_en: C.CString(types.String(region["name_en"])),
name_ru: C.CString(types.String(region["name_ru"])),
id: C.int(conv.Int(region["id"])),
iso: C.CString(conv.String(region["iso"])),
name_en: C.CString(conv.String(region["name_en"])),
name_ru: C.CString(conv.String(region["name_ru"])),
},
}
}
Expand Down
129 changes: 129 additions & 0 deletions examples/clib/sp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package command-line-arguments */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments. */


#line 3 "sp.go"

#include <stdlib.h>

#ifndef GO_SYPEGEO
#define GO_SYPEGEO

typedef struct {
int id;
float lat;
float lon;
char* name_en;
char* name_ru;
} spxCity;

typedef struct {
int id;
char* iso;
char* name_en;
char* name_ru;
} spxRegion;

typedef struct {
int id;
char* iso;
float lat;
float lon;
char* name_en;
char* name_ru;
} spxCountry;

typedef struct {
spxCity city;
spxCountry country;
spxRegion region;
int err;
} spxInfo;

static void spxFree(spxInfo s) {
free(s.city.name_en);
free(s.city.name_ru);
free(s.country.iso);
free(s.country.name_en);
free(s.country.name_ru);
free(s.region.iso);
free(s.region.name_en);
free(s.region.name_ru);
}
#endif

#line 1 "cgo-generated-wrapper"


/* End of preamble from import "C" comments. */


/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus
extern "C" {
#endif

extern void spxConnect(char* c);
extern spxInfo spxGetCity(char* ip, int full);
extern char* spxGetCountry(char* ip);
extern int spxGetCountryID(char* ip);

#ifdef __cplusplus
}
#endif
3 changes: 1 addition & 2 deletions examples/clib/test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include "sp.h"


void printInfo(spxInfo info) {
Expand All @@ -18,5 +19,3 @@ int main() {
printf("Coutry code: %s\n", spxGetCountry("53.12.92.225"));
printf("Coutry ID: %i\n", spxGetCountryID("53.12.92.225"));
}


18 changes: 18 additions & 0 deletions examples/geoAPI/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module go-sypexgeo/examples/geoAPI

go 1.16

require (
github.com/night-codes/go-sypexgeo v1.0.0
github.com/night-codes/tokay v1.4.2
)

require (
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/klauspost/compress v1.13.3 // indirect
github.com/night-codes/govalidator v1.0.4 // indirect
github.com/night-codes/tokay-render v1.0.2 // indirect
github.com/night-codes/tokay-websocket v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.28.0 // indirect
)
43 changes: 43 additions & 0 deletions examples/geoAPI/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.3 h1:BtAvtV1+h0YwSVwWoYXMREPpYu9VzTJ9QDI1TEg/iQQ=
github.com/klauspost/compress v1.13.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/night-codes/go-sypexgeo v0.0.0-20220204130107-58e4e9af21d4 h1:bdTnhJ5mYHa5q8Hsq+6bhsZe8Nadt2LTYqMhYfpE/Og=
github.com/night-codes/go-sypexgeo v0.0.0-20220204130107-58e4e9af21d4/go.mod h1:a1CTVmL98MYMFqhW+ulcA1Rk5gX6MMDOztUFFbmyEhk=
github.com/night-codes/govalidator v1.0.4 h1:MPPrWKLDw0P1Dunqgoh4VCiOq8jjpW1RqjQ3XYQE52Q=
github.com/night-codes/govalidator v1.0.4/go.mod h1:698OUuzQEHaly/WE4iDX2Zq9XUD9tlIubaKTtqDgJvE=
github.com/night-codes/tokay v1.4.2 h1:2FbSk13/rBXHXlu7IVKzQvX3pCluY3gPvLU+EiA38ME=
github.com/night-codes/tokay v1.4.2/go.mod h1:2ohDuQ/JkoAqAyVKXhnOE8VptxAtuSaKf3/Guv7rUYY=
github.com/night-codes/tokay-render v1.0.2 h1:llMZ9rqk7qcJdYxKKiXLTwgckmJjp6/jTDjYi/JHSx0=
github.com/night-codes/tokay-render v1.0.2/go.mod h1:luRmj6jFl2hpI21p039tjz/E+z8v1zfZ6tH3Kmihj58=
github.com/night-codes/tokay-websocket v1.0.0 h1:2/Dx6vSyMCxw6IkhhaODKQDgzJaF0BeQpp8mBNTVAGM=
github.com/night-codes/tokay-websocket v1.0.0/go.mod h1:ihLq/OaMOFoaKayuSeXuJ+6i0h56JXmoRvqJiOQklGc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.28.0 h1:ruVmTmZaBR5i67NqnjvvH5gEv0zwHfWtbjoyW98iho4=
github.com/valyala/fasthttp v1.28.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 882eacd

Please sign in to comment.