twid

package
v0.0.0-...-002d596 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: LGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

package twid implements a server that can load and run multiple Twid modules. These modules can form a complete Twid server that interacts with Twilio over its REST and webhooks API.

For examples on how to use this package, see github.com/diamondburned/twidiscord.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main()

Main runs the twid server as if it were to be executed from a package main program. This function is extremely useful when code-generating files.

func Register

func Register(mod Module)

Register registers a module to be loaded by the twid server.

Types

type CommandHandler

type CommandHandler interface {
	Handler
	// Command returns the module's root command. Commands are checked against
	// collisions.
	Command() twicli.Command
}

CommandHandler is a module that uses the twicli.Command API.

type Config

type Config struct {
	HTTP struct {
		ListenAddr cfgutil.EnvString `toml:"listen_addr" json:"listen_addr"`
	} `toml:"http" json:"http"`
}

Config is the twid config block.

type ConfigType

type ConfigType string

ConfigType is the type of the configuration file, e.g. "toml" or "json".

const (
	TOMLConfig ConfigType = "toml"
	JSONConfig ConfigType = "json"
)

type HTTPCommander

type HTTPCommander interface {
	Handler
	// HTTPHandler returns the HTTP handler for the module.
	HTTPHandler() http.Handler
	// HTTPPrefix returns the HTTP prefix that the module will serve on. The
	// prefix must not contain a trailing slash.
	HTTPPrefix() string
}

HTTPCommander is a module that implements HTTP serving.

type Handler

type Handler interface {
	// Config returns the module's configuration. The configuration is assumed
	// to be the root structure, and each module should wrap its configuration
	// in a block named after the module.
	Config() any
	// Start starts the module.
	Start(ctx context.Context) error
}

Handler is a handler instance created by a registered module.

type Loader

type Loader struct {
	Config struct {
		Twid  Config       `toml:"twid" json:"twid"`
		Twipi twipi.Config `toml:"twipi" json:"twipi"`
	}
	// contains filtered or unexported fields
}

Loader is a module loader. It assists in loading a list of modules and starting them.

func NewGlobalLoader

func NewGlobalLoader() *Loader

NewGlobalLoader creates a new loader with the global modules.

func NewLoader

func NewLoader(modules []Module) *Loader

NewLoader creates a new loader with the given modules.

func (*Loader) LoadConfig

func (l *Loader) LoadConfig(b []byte, configType string) error

LoadConfig loads the configuration from the given bytes into all the module handlers. configType determines the type of the configuration file, e.g. "toml" or "json".

func (*Loader) LoadConfigFile

func (l *Loader) LoadConfigFile(path string) error

LoadConfigFile loads the configuration file from the given path into all the module handlers.

func (*Loader) Start

func (l *Loader) Start(ctx context.Context) error

Start starts the HTTP server and the loaded modules' handlers. It blocks until the context is canceled or any of the handlers fail to start.

type MessageHandler

type MessageHandler interface {
	Handler
	// HandleMessage handles the given message. Each HandleMessage call is
	// assumed to be in a separate goroutine.
	HandleMessage(ctx context.Context, msg twipi.Message)
}

MessageHandler is a module that can handle a Twipi message. This is commonly used as an alternative for CommandHandler.

type Module

type Module struct {
	// Name is the name of the module.
	Name string
	// New is the constructor that creates a new Handler.
	New func() Handler
}

Module is a module that can be loaded by the twid server.

type TwipiHandler

type TwipiHandler interface {
	Handler
	// BindTwipi binds the configured Twipi server to the module.
	BindTwipi(*twipi.ConfiguredServer)
}

TwipiHandler is a module that can bind a Twipi server.

Jump to

Keyboard shortcuts

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