database

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: MIT Imports: 10 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 string `json:"database"`
	// contains filtered or unexported fields
}

IP2Location provides read access to an IP2Location binary database.

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. The location of the database can be either a path on local disk, an HTTP(S) URL, or an AWS S3 URL.

type MaxMindASN

type MaxMindASN struct {
	Database string `json:"database"`
	Language string `json:"language"`
	// contains filtered or unexported fields
}

MaxMindASN provides read access to MaxMind ASN database.

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. The location of the database can be either a path on local disk, an HTTP(S) URL, or an AWS S3 URL. MaxMind language support is provided by calling GetMaxMindLanguage to retrieve a user-configured language.

type MaxMindCity

type MaxMindCity struct {
	Database string `json:"database"`
	Language string `json:"language"`
	// contains filtered or unexported fields
}

MaxMindCity provides read access to a MaxMind City database.

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. MaxMind language support is provided by calling GetMaxMindLanguage to retrieve a user-configured language.

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 Factory

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

Factory returns an OpenCloser. The returned OpenCloser must be opened before it can be used. func Factory(db string) (OpenCloser, error) {

Jump to

Keyboard shortcuts

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