Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
Go 1.12 and removing deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
s32x committed Mar 22, 2019
1 parent c23a562 commit a5ff950
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 129 deletions.
17 changes: 2 additions & 15 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ jobs:
working_directory: /go/src/s32x.com/ipdata
environment:
- DOCKER_TAG: s32x/ipdata
- HEROKU_APP: ipdatainfo
docker:
- image: circleci/golang:1.11.5
- image: circleci/golang:1.12
steps:
- checkout
- run:
name: Download vendored Go dependencies
command: GO111MODULE=on go mod vendor
- run:
name: Run unit tests
command: make test
- setup_remote_docker
- run:
name: Build Docker image with binary
Expand All @@ -24,10 +17,4 @@ jobs:
command: docker login -u $DOCKER_USER -p $DOCKER_PASS
- run:
name: Push image to DockerHub
command: docker push $DOCKER_TAG
- run:
name: Install the Heroku CLI
command: curl https://cli-assets.heroku.com/install.sh | sh
- run:
name: Deploy the app
command: make deploy
command: docker push $DOCKER_TAG
21 changes: 11 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# ============================== BINARY BUILDER ==============================
FROM golang:1.11.5 as builder
FROM golang:1.12 as builder

# Copy in the source
WORKDIR /go/src/s32x.com/ipdata
COPY / .
COPY . /src
WORKDIR /src

# Dependencies
RUN apt-get update -y && \
apt-get upgrade -y
RUN GO111MODULE=on go mod vendor

# Build the binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/server
# Vendor, Test and Build the Binary
RUN GO111MODULE=on go mod vendor
RUN go test ./...
RUN CGO_ENABLED=0 go build -o ./bin/server

# =============================== FINAL IMAGE ===============================
FROM alpine:latest

# Dependencies
RUN apk add --no-cache ca-certificates

# Static files
COPY service/static /service/static

# Binary
COPY --from=builder /go/src/s32x.com/ipdata/bin/server /usr/local/bin/server
# Static files and Binary
COPY --from=builder /src/db /db
COPY --from=builder /src/static /static
COPY --from=builder /src/bin/server /usr/local/bin/server
CMD ["server"]
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
deps:
-rm -rf vendor
-rm -rf db
-rm -f go.mod
-rm -f go.sum
go clean
GO111MODULE=on go mod init
GO111MODULE=on go mod vendor
mkdir db
wget -O ./db/city.tar.gz http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
wget -O ./db/asn.tar.gz http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz
test:
go test ./...
install:
make deps
go install
deploy:
heroku container:login
heroku container:push web -a ipdatainfo
heroku container:release web -a ipdatainfo
go install
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<p align="center">
<img src="service/static/assets/logo.png" width="310" height="110" border="0" alt="ipdata">
<img src="static/assets/logo.png" width="310" height="110" border="0" alt="ipdata">
<br>
<a href="https://circleci.com/gh/s32x/ipdata/tree/master"><img src="https://circleci.com/gh/s32x/ipdata/tree/master.svg?style=svg" alt="CircleCI"></a>
<a href="https://goreportcard.com/report/s32x.com/ipdata"><img src="https://goreportcard.com/badge/s32x.com/ipdata" alt="Go Report Card"></a>
<a href="https://godoc.org/s32x.com/ipdata/ipdata"><img src="https://godoc.org/s32x.com/ipdata/ipdata?status.svg" alt="GoDoc"></a>
</p>

<p align="center">
<img src="service/static/assets/graphic.png" width="650px" height="418px" alt="ipdata curl">
<img src="static/assets/graphic.png" width="650px" height="418px" alt="ipdata curl">
</p>

`ipdata` is a free and open-source ip address lookup system. It combines the results retrieved from multiple maxminddb databases to provide you general geo/isp data for version 4 addresses. The project is currently available as a [publicly consumable API](#public-api-usage), an easily importable [Golang package called `ipdata`](#full-go-example) for use in your own Go projects, and a public [Docker image on DockerHub](https://hub.docker.com/r/s32x/ipdata/).
Expand Down
Binary file added db/asn.tar.gz
Binary file not shown.
Binary file added db/city.tar.gz
Binary file not shown.
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module s32x.com/ipdata

go 1.12

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.2.8 // indirect
github.com/mattn/go-colorable v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mmcloughlin/geohash v0.0.0-20181009053802-f7f2bcae3294
github.com/oschwald/maxminddb-golang v1.3.0
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 // indirect
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a // indirect
github.com/valyala/fasttemplate v1.0.1 // indirect
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 // indirect
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 // indirect
)
22 changes: 14 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o=
github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mmcloughlin/geohash v0.0.0-20181009053802-f7f2bcae3294/go.mod h1:oNZxQo5yWJh0eMQEP/8hwQuVx9Z9tjwFUqcTB1SmG0c=
github.com/oschwald/maxminddb-golang v1.3.0/go.mod h1:3jhIUymTJ5VREKyIhWm66LJiQt04F0UCDdodShpjWsY=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 h1:aUX/1G2gFSs4AsJJg2cL3HuoRhCSCz733FE5GUSuaT4=
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 h1:xe1/2UUJRmA9iDglQSlkx8c5n3twv58+K0mPpC2zmhA=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28 changes: 14 additions & 14 deletions ipdata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"compress/gzip"
"errors"
"io"
"net/http"
"os"
"strings"
"sync"

Expand All @@ -21,9 +21,9 @@ type Client struct {
}

// NewClient creates and returns a fully populated ipdata Client
func NewClient() (*Client, error) {
func NewClient(cityPath, asnPath string) (*Client, error) {
c := &Client{mu: sync.Mutex{}}
if err := c.updateReaders(); err != nil {
if err := c.updateReaders(cityPath, asnPath); err != nil {
return nil, err
}
return c, nil
Expand All @@ -37,30 +37,30 @@ func (c *Client) Close() {

// updateReaders concurrently retrieves and populates the databases using the
// urls on the Client
func (c *Client) updateReaders() error {
func (c *Client) updateReaders(cityPath, asnPath string) error {
c.mu.Lock()
defer c.mu.Unlock()

// Concurrently retrieve and populate the mmdb databases
var g errgroup.Group
g.Go(readMMDB(geoLite2City, &c.city))
g.Go(readMMDB(geoLite2ASN, &c.asn))
g.Go(readMMDB(cityPath, &c.city))
g.Go(readMMDB(asnPath, &c.asn))
return g.Wait()
}

// readMMDB takes a compressed MaxMind database URL, downloads, decompresses,
// and generates a maxmindb.Reader
func readMMDB(url string, out *maxminddb.Reader) func() error {
// readMMDB takes a compressed MaxMind database at the given path, decompresses
// it and generates a maxmindb.Reader
func readMMDB(path string, out *maxminddb.Reader) func() error {
return func() error {
// Retrieve the compressed database from the url
res, err := http.Get(url)
// Open the database file at the passed path
file, err := os.Open(path)
if err != nil {
return err
}
defer res.Body.Close()
defer file.Close()

// GZIP read the response body
gzr, err := gzip.NewReader(res.Body)
// GZIP read file
gzr, err := gzip.NewReader(file)
if err != nil {
return err
}
Expand Down
7 changes: 0 additions & 7 deletions ipdata/maxmind.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package ipdata /* import "s32x.com/ipdata/ipdata" */

const (
maxMindDB = "http://geolite.maxmind.com/download/geoip/database/"
geoLite2City = maxMindDB + "GeoLite2-City.tar.gz"
geoLite2Country = maxMindDB + "GeoLite2-Country.tar.gz"
geoLite2ASN = maxMindDB + "GeoLite2-ASN.tar.gz"
)

// ASN is the query response for ASN lookups
type ASN struct {
Number uint64 `maxminddb:"autonomous_system_number"`
Expand Down
50 changes: 45 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,56 @@ package main /* import "s32x.com/ipdata" */

import (
"log"
"net/http"
"os"

"s32x.com/ipdata/service"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"s32x.com/ipdata/ipdata"
)

var (
port = getenv("PORT", "8080")
cityPath = getenv("CITY_PATH", "./db/city.tar.gz")
asnPath = getenv("ASN_PATH", "./db/asn.tar.gz")
)

func main() {
service.Start(
getenv("PORT", "8080"),
getenv("ENV", "dev"),
)
// Create the ipdata client
ic, err := ipdata.NewClient(cityPath, asnPath)
if err != nil {
log.Fatal(err)
}
defer ic.Close()

// Create a new echo Echo and bind all middleware
e := echo.New()
e.HideBanner = true

// Bind middleware
e.Pre(middleware.RemoveTrailingSlashWithConfig(
middleware.TrailingSlashConfig{
RedirectCode: http.StatusPermanentRedirect,
}))
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Pre(middleware.Secure())
e.Use(middleware.Gzip())
e.Use(middleware.CORS())

// Serve the static web content on the base echo instance
e.Static("*", "./static")

// Bind all API endpoint handlers
e.GET("/lookup/:ip", func(c echo.Context) error {
return c.JSON(http.StatusOK, ic.Lookup(c.Param("ip")))
})
e.GET("/healthcheck", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

// Listen on the passed port
e.Logger.Fatal(e.Start(":" + port))
}

// getenv attempts to retrieve and return a variable from the environment. If it
Expand Down
56 changes: 0 additions & 56 deletions service/service.go

This file was deleted.

File renamed without changes
File renamed without changes
File renamed without changes.

0 comments on commit a5ff950

Please sign in to comment.