controller

package
v0.0.0-...-7864f83 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxPinLimit  = 1000
	MaxNameLimit = 255
)

Variables

View Source
var Ctrl = func() Controller {

	controllerOnce.Do(func() {
		var cfg Config
		if err := config.Load(&cfg); err != nil {
			panic(err)
		}
		if len(cfg.AuthenticationGrants) > 0 {
			err := auth.SetAuthGrants(cfg.AuthenticationGrants)
			if err != nil {
				panic(fmt.Errorf(
					"Controller: failed to set custom authentication grants with error: %s",
					err,
				))
			}
		}
		ctx := context.Background()
		db, err := pinsdb.New(ctx, cfg.DatabaseAddr, cfg.DropDatabaseOnStart)
		if err != nil {
			panic(fmt.Errorf(
				"Controller: failed to connect to database at address ('%s') with error: %s",
				cfg.DatabaseAddr, err,
			))
		}
		apiAddr, err := ma.NewMultiaddr(cfg.IpfsClusterApiAddr)
		if err != nil {
			panic(fmt.Errorf(
				"Controller: failed to parse IPFS cluster API address ('%s')",
				cfg.IpfsClusterApiAddr,
			))
		}
		ipfsClient, err := cluster.NewDefaultClient(&cluster.Config{
			SSL:          cfg.IpfsClusterSsl,
			NoVerifyCert: cfg.IpfsClusterNoVerifyCert,
			APIAddr:      apiAddr,
			ProtectorKey: cfg.IpfsClusterProtectorKey,
			Timeout:      time.Duration(cfg.IpfsClusterTimeout) * time.Second,
		})
		if err != nil {
			panic(fmt.Errorf(
				"Controller: failed to create client for IPFS cluster ('%s') with error %s",
				cfg.IpfsClusterApiAddr, err,
			))
		}
		middleware.SetKeyFunc(auth.KeyFunc(cfg.AuthenticatorAddr))
		middleware.SetErrFunc(auth.ErrFunc())
		control = New(ctx, ipfsClient, db)
	})
	return control
}
View Source
var (
	// Pins controller errors
	ErrorPinNotFound = errors.New("pin not found")
)

Routes is the list of routes of the Pins HTTP API

Functions

func CreatePin

func CreatePin(w http.ResponseWriter, r *http.Request, p server.Parameters)

func FindPins

func FindPins(w http.ResponseWriter, r *http.Request, p server.Parameters)

func GetPin

func PatchPin

func PatchPin(w http.ResponseWriter, r *http.Request, p server.Parameters)

func RemovePin

func RemovePin(w http.ResponseWriter, r *http.Request, p server.Parameters)

func UpdatePin

func UpdatePin(w http.ResponseWriter, r *http.Request, p server.Parameters)

Types

type Config

type Config struct {
	DatabaseAddr         string   `toml:"database_addr"`
	AuthenticatorAddr    string   `toml:"authenticator_addr"`
	AuthenticationGrants []string `toml:"authentication_grants"`
	DropDatabaseOnStart  bool     `toml:"drop_database_on_start"`

	// IPFS configuration
	IpfsClusterSsl          bool   `toml:"ipfs_cluster_ssl"`
	IpfsClusterNoVerifyCert bool   `toml:"ipfs_cluster_no_verify_cert"`
	IpfsClusterApiAddr      string `toml:"ipfs_cluster_api_addr"`
	IpfsClusterProtectorKey []byte `toml:"ipfs_cluster_protector_key"`
	IpfsClusterTimeout      int    `toml:"ipfs_cluster_timeout"` // in seconds
}

type Controller

type Controller interface {
	// FindPins returns all pins that match the given parameters
	FindPins(
		uid string,
		cids []string,
		name, before, after string,
		match models.TextMatch,
		statuses []models.Status,
		limit int, offset int,
		sortBy string, meta models.Info,
	) (models.Pins, error)
	// GetPin returns the Pin for the given ID
	GetPin(uid, id string) (models.PinStatus, error)
	// CreatePin creates a new Pin based on the given Pin object
	CreatePin(uid string, pin models.Pin) (models.PinStatus, error)
	// UpdatePin updates the Pin at the given ID via the given data
	UpdatePin(uid, id string, data models.Pin) (models.PinStatus, error)
	// PatchPin modifies the Pin at the given ID via the set fields of data
	PatchPin(uid, id string, data models.Pin) (models.PinStatus, error)
	// RemovePin deletes the Pin at the given ID
	RemovePin(uid, id string) error
}

Controller represents an interface to the Pins manager

func New

func New(ctx context.Context, client cluster.Client, db pinsdb.Pins) Controller

New returns a new Controller for the given context and Pins database interface

type Route

type Route struct {
	Handler          server.Handler
	Method           string
	Path             string
	ResponseSettings []server.ResponseSetting
}

Route represents the route of the Pins HTTP API

Jump to

Keyboard shortcuts

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