cmdserver

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

Diatheke Command Server

This package provides a simple http server that may be used for Diatheke command fulfillment. Such a server should be deployed with Diatheke, Cubic, Luna, etc. so that the Diatheke server can access the command server.

Security

Note that this server only supports http at the present. It does not allow secure connections (https), so any data sent to this server will not be encrypted. As such, it is not recommended to use this server for production.

Usage

Import the package using the go tool:

go get -u github.com/cobaltspeech/examples-go/cmdserver

To use the command server, create a new server, register command handlers and run.

type SomeHandler struct {
	// Contains some data fields
}

func (h *SomeHandler) fooCmd(input cmdserver.Params) (cmdserver.Params, error) {
	// Do something interesting with the command input.
	return nil, nil
}

func (h *SomeHandler) barCmd(input cmdserver.Params) (cmdserver.Params, error) {
	// Do something interesting with the command input.

	// Create the output parameters
	outParams := make(cmdserver.Params)
	outParams["expectedKey"] = "expectedVal"

	return outParams, nil
}

func main() {
	// Create the server (with an optional logger if desired)
	svr := cmdserver.NewServer(nil)

	// Set handlers for command IDs
	handler := SomeHandler{}
	svr.SetHandler("foo", handler.fooCmd)
	svr.SetHandler("bar", handler.barCmd)

	// Run the server
	if err := svr.Run(":24601"); err != nil {
		os.Exit(1)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(Params) (Params, error)

Handler is a function that takes a map of input parameters and returns a (possibly empty or nil) map of output parameters, as expected by a Diatheke command.

type Params

type Params map[string]string

Params is an alias for a map[string]string that includes some convenience functions for converting to other types. To create a new Params object, use the go standard make function (e.g., `make(Params)`). Note that the convenience functions are not safe to use concurrently (just as it is not safe to access a regular map in Go concurrently).

func (Params) AsBool

func (p Params) AsBool(key string) (bool, error)

AsBool returns the parameter value for the given key as a bool. Returns an error if the key was not found or there was a problem during conversion.

func (Params) AsFloat32

func (p Params) AsFloat32(key string) (float32, error)

AsFloat32 returns the parameter value for the given key as a float32. Returns an error if the key was not found or there was problem during conversion.

func (Params) AsFloat64

func (p Params) AsFloat64(key string) (float64, error)

AsFloat64 returns the parameter value for the given key as a float64. Returns an error if the key was not found or there was a problem during conversion.

func (Params) AsInt

func (p Params) AsInt(key string) (int, error)

AsInt returns the parameter value for the given key as an int. Returns an error if the key was not found or there was a problem during conversion.

func (Params) AsString

func (p Params) AsString(key string) (string, error)

AsString returns the parameter value for the given key as a string. Returns an error if the key was not found. Note that the underlying map may be used directly to get the parameter and check it's existence without the error (e.g., `val, ok := p[key]`).

func (Params) SetBool

func (p Params) SetBool(key string, val bool)

SetBool converts the given bool to a string and stores it in the parameter map.

func (Params) SetFloat32

func (p Params) SetFloat32(key string, val float32)

SetFloat32 converts the given float32 to a string and stores it in the parameter map. This uses the 'g' formatting style for the conversion, with a precision of 4. For different formatting, it is recommended to use strconv.FormatFloat().

func (Params) SetFloat64

func (p Params) SetFloat64(key string, val float64)

SetFloat64 converts the given float64 to a string and stores it in the parameter map. This uses the 'g' formatting style for the conversion, with a precision of 4. For different formatting, it is recommended to use strconv.FormatFloat().

func (Params) SetInt

func (p Params) SetInt(key string, val int)

SetInt converts the given int to a string and stores it in the parameter map.

func (Params) SetString

func (p Params) SetString(key, val string)

SetString sets the parameter value for the given key. This is no different than setting it directly on the object (e.g., `p[key] = val`).

type Server

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

Server represents an http server that handles Diatheke commands. With this server, all commands go to the same URL, so the Diatheke model should be set accordingly. Once received, commands are sent to Handlers added with the SetHandler function.

func NewServer

func NewServer(logger log.Logger) Server

NewServer returns a new command server.

func (*Server) Run

func (svr *Server) Run(address string) error

Run starts the http server and listens at the given address (e.g., ":8072", "localhost:1515", "127.0.0.1:3535") until either an error occurs or the interrupt signal is received.

func (*Server) ServeHTTP

func (svr *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface. It decodes the command, forwards the data to the correct command Handler, then encodes the result to send back to Diatheke.

func (*Server) SetHandler

func (svr *Server) SetHandler(cmdID string, h Handler)

SetHandler registers the provided Handler to be called for the specified command ID.

Jump to

Keyboard shortcuts

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