ferretdb

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: Apache-2.0 Imports: 11 Imported by: 7

Documentation

Overview

Package ferretdb provides embeddable FerretDB implementation.

See build/version package documentation for information about Go build tags that affect this package.

Example (Tcp)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/FerretDB/FerretDB/ferretdb"
)

func main() {
	f, err := ferretdb.New(&ferretdb.Config{
		Listener: ferretdb.ListenerConfig{
			TCP: "127.0.0.1:17027",
		},
		Handler:       "pg",
		PostgreSQLURL: "postgres://127.0.0.1:5432/ferretdb",
	})
	if err != nil {
		log.Fatal(err)
	}

	ctx, cancel := context.WithCancel(context.Background())

	done := make(chan struct{})

	go func() {
		log.Print(f.Run(ctx))
		close(done)
	}()

	uri := f.MongoDBURI()
	fmt.Println(uri)

	// Use MongoDB URI as usual. For example:
	//
	// import "go.mongodb.org/mongo-driver/mongo"
	//
	// [...]
	//
	// mongo.Connect(ctx, options.Client().ApplyURI(uri))

	cancel()
	<-done

}
Output:
mongodb://127.0.0.1:17027/
Example (Tls)
package main

import (
	"context"
	"fmt"
	"log"
	"path/filepath"

	"github.com/FerretDB/FerretDB/ferretdb"
)

func main() {
	certPath := filepath.Join("..", "build", "certs", "server-cert.pem")
	keyPath := filepath.Join("..", "build", "certs", "server-key.pem")
	caPath := filepath.Join("..", "build", "certs", "rootCA-cert.pem")

	f, err := ferretdb.New(&ferretdb.Config{
		Listener: ferretdb.ListenerConfig{
			TLS:         "127.0.0.1:17028",
			TLSCertFile: certPath,
			TLSKeyFile:  keyPath,
			TLSCAFile:   caPath,
		},
		Handler:       "pg",
		PostgreSQLURL: "postgres://127.0.0.1:5432/ferretdb",
	})
	if err != nil {
		log.Fatal(err)
	}

	ctx, cancel := context.WithCancel(context.Background())

	done := make(chan struct{})

	go func() {
		log.Print(f.Run(ctx))
		close(done)
	}()

	uri := f.MongoDBURI()
	fmt.Println(uri)

	// Use MongoDB URI as usual. To connect to TLS listener, set TLS config.
	// For example:
	//
	// import "go.mongodb.org/mongo-driver/mongo"
	// import "go.mongodb.org/mongo-driver/mongo/options"
	//
	// [...]
	//
	// mongo.Connect(ctx, options.Client().ApplyURI(uri))

	cancel()
	<-done

}
Output:
mongodb://127.0.0.1:17028/?tls=true
Example (Unix)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/FerretDB/FerretDB/ferretdb"
)

func main() {
	f, err := ferretdb.New(&ferretdb.Config{
		Listener: ferretdb.ListenerConfig{
			Unix: "/tmp/ferretdb.sock",
		},
		Handler:       "pg",
		PostgreSQLURL: "postgres://127.0.0.1:5432/ferretdb",
	})
	if err != nil {
		log.Fatal(err)
	}

	ctx, cancel := context.WithCancel(context.Background())

	done := make(chan struct{})

	go func() {
		log.Print(f.Run(ctx))
		close(done)
	}()

	uri := f.MongoDBURI()
	fmt.Println(uri)

	// Use MongoDB URI as usual.

	cancel()
	<-done

}
Output:
mongodb://%2Ftmp%2Fferretdb.sock/

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Listener ListenerConfig

	// Handler to use; one of `pg`, `sqlite`, or `tigris` (if enabled at compile-time).
	Handler string

	// PostgreSQL connection string for `pg` handler.
	// See:
	//   - https://pkg.go.dev/github.com/jackc/pgx/v5/pgxpool#ParseConfig
	//   - https://pkg.go.dev/github.com/jackc/pgx/v5#ParseConfig
	//   - https://pkg.go.dev/github.com/jackc/pgx/v5/pgconn#ParseConfig
	PostgreSQLURL string // For example: `postgres://hostname:5432/ferretdb`.

	// SQLite directory path or `file:` URI for `sqlite` handler.
	// See:
	//   - https://www.sqlite.org/c3ref/open.html
	//   - https://www.sqlite.org/uri.html
	SQLiteURI string

	// Tigris parameters for `tigris` handler.
	// See https://www.tigrisdata.com/docs/sdkstools/golang/getting-started/
	TigrisURL          string
	TigrisClientID     string
	TigrisClientSecret string
}

Config represents FerretDB configuration.

type FerretDB

type FerretDB struct {
	// contains filtered or unexported fields
}

FerretDB represents an instance of embeddable FerretDB implementation.

func New

func New(config *Config) (*FerretDB, error)

New creates a new instance of embeddable FerretDB implementation.

func (*FerretDB) MongoDBURI

func (f *FerretDB) MongoDBURI() string

MongoDBURI returns MongoDB URI for this FerretDB instance.

TCP's connection string is returned if both TCP and Unix listeners are enabled. TLS is preferred over both.

func (*FerretDB) Run

func (f *FerretDB) Run(ctx context.Context) error

Run runs FerretDB until ctx is done.

When this method returns, listener and all connections are closed.

type ListenerConfig added in v0.7.1

type ListenerConfig struct {
	// Listen TCP address.
	// If empty, TCP listener is disabled.
	TCP string

	// Listen Unix domain socket path.
	// If empty, Unix listener is disabled.
	Unix string

	// Listen TLS address.
	// If empty, TLS listener is disabled.
	TLS string

	// Server certificate path.
	TLSCertFile string

	// Server key path.
	TLSKeyFile string

	// Root CA certificate path.
	TLSCAFile string
}

ListenerConfig represents listener configuration.

Jump to

Keyboard shortcuts

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