database

package
v0.8.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

package database provides tools for enriching IP addresses from enrichment databases.

Example (IP2Location)
package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/brexhq/substation/internal/ip/database"
)

func main() {
	// create IP2Location container, open database, and close database when function returns
	ip2loc := database.IP2Location{
		Database: "location://path/to/ip2location.bin",
	}

	if err := ip2loc.Open(context.TODO()); err != nil {
		// handle error
		panic(err)
	}
	defer ip2loc.Close()

	// query database
	addr := "8.8.8.8"
	record, err := ip2loc.Get(addr)
	if err != nil {
		// handle error
		panic(err)
	}

	// marshal to JSON for printing
	res, _ := json.Marshal(record)
	fmt.Println(string(res))
}
Example (MaxMindCity)
package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/brexhq/substation/internal/ip/database"
)

func main() {
	// create MaxMind City container, open database, and close database when function returns
	mm := database.MaxMindCity{
		Database: "location://path/to/maxmind.mmdb",
	}

	if err := mm.Open(context.TODO()); err != nil {
		// handle error
		panic(err)
	}
	defer mm.Close()

	// query database
	addr := "8.8.8.8"
	record, err := mm.Get(addr)
	if err != nil {
		// handle error
		panic(err)
	}

	// marshal to JSON for printing
	res, _ := json.Marshal(record)
	fmt.Println(string(res))
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IP2Location

type IP2Location struct {
	// Database contains the location of the IP2Location database. This can be either a path on local disk, an HTTP(S) URL, or an AWS S3 URL.
	Database string `json:"database"`
	// contains filtered or unexported fields
}

IP2Location provides read access to an IP2Location binary database. The database is safe for concurrent access.

func (*IP2Location) Close

func (d *IP2Location) Close() error

Close closes the open database.

func (*IP2Location) Get

func (d *IP2Location) Get(addr string) (*ip.EnrichmentRecord, error)

Get queries the database and returns an aggregated database record containing enrichment information.

func (*IP2Location) IsEnabled

func (d *IP2Location) IsEnabled() bool

IsEnabled returns true if the database is open and ready for use.

func (*IP2Location) Open

func (d *IP2Location) Open(ctx context.Context) error

Open retrieves the database and opens it for querying.

type MaxMindASN

type MaxMindASN struct {
	// Database contains the location of the MaxMind City database. This can be either a path on local disk, an HTTP(S) URL, or an AWS S3 URL.
	Database string `json:"database"`
	/*
		Language determines the language that localized name data is returned as. More information is available here: https://support.maxmind.com/hc/en-us/articles/4414877149467-IP-Geolocation-Data.

		This is optional and defaults to "en" (English).
	*/
	Language string `json:"language"`
	// contains filtered or unexported fields
}

MaxMindASN provides read access to a MaxMind ASN database. The database is safe for concurrent access.

func (*MaxMindASN) Close

func (d *MaxMindASN) Close() error

Close closes the open database.

func (*MaxMindASN) Get

func (d *MaxMindASN) Get(addr string) (*ip.EnrichmentRecord, error)

Get queries the database and returns an aggregated database record containing enrichment information.

func (*MaxMindASN) IsEnabled

func (d *MaxMindASN) IsEnabled() bool

IsEnabled returns true if the database is open and ready for use.

func (*MaxMindASN) Open

func (d *MaxMindASN) Open(ctx context.Context) error

Open retrieves the database and opens it for querying.

type MaxMindCity

type MaxMindCity struct {
	// Database contains the location of the MaxMind City database. This can be either a path on local disk, an HTTP(S) URL, or an AWS S3 URL.
	Database string `json:"database"`
	/*
		Language determines the language that localized name data is returned as. More information is available here: https://support.maxmind.com/hc/en-us/articles/4414877149467-IP-Geolocation-Data.

		This is optional and defaults to "en" (English).
	*/
	Language string `json:"language"`
	// contains filtered or unexported fields
}

MaxMindCity provides read access to a MaxMind City database. The database is safe for concurrent access.

func (*MaxMindCity) Close

func (d *MaxMindCity) Close() error

Close closes the open database.

func (*MaxMindCity) Get

func (d *MaxMindCity) Get(addr string) (*ip.EnrichmentRecord, error)

Get queries the database and returns an aggregated database record containing enrichment information.

func (*MaxMindCity) IsEnabled

func (d *MaxMindCity) IsEnabled() bool

IsEnabled returns true if the database is open and ready for use.

func (*MaxMindCity) Open

func (d *MaxMindCity) Open(ctx context.Context) error

Open retrieves the database and opens it for querying.

type OpenCloser

type OpenCloser interface {
	ip.Getter
	Open(context.Context) error
	Close() error
	IsEnabled() bool
}

OpenCloser provides tools for opening, closing, and getting values from IP address enrichment databases.

func Get added in v0.8.0

func Get(cfg config.Config) (OpenCloser, error)

Get returns a pointer to an OpenCloser that is stored as a package level global variable. The OpenCloser must be opened before it can be used.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL