CommandableHttpController

Abstract controller that receives remote calls via HTTP/REST protocol to operations automatically generated for commands defined in ICommandable components.

Implements: RestController

Description

The CommandableHttpController class allows you to create controller that receive remote calls via the HTTP/REST protocol to operations automatically generated for commands defined in ICommandable components.

Important points

  • Each command is exposed as POST operation that receives all parameters in the body object.
  • Commandable controller require only three lines of code to implement a robust external HTTP-based remote interface.

Configuration parameters

  • base_route: base route for remote URI
  • dependencies:
    • endpoint: override for HTTP Endpoint dependency
    • controller: override for Controller dependency
  • connection(s):
    • discovery_key: (optional) a key to retrieve the connection from IDiscovery
    • protocol: connection protocol: http or https
    • host: host name or IP address
    • port: port number
    • uri: resource URI or connection string with all parameters in it

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
  • *:traces:*:*:1.0 - (optional) ITracer components to record traces
  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve connection

Constructors

InheritCommandableHttpController

Creates a new instance of the controller.

InheritCommandableHttpController(overrides IRegisterable, baseRoute string) *CommandableHttpController

  • overrides: IRegisterable - references to child class that overrides virtual methods.
  • baseRoute: string - a controller base route.

Fields

commandSet

Set of commands.

commandSet: CommandSet

SwaggerAuto

Boolean variable that defines whether the configuration of Swagger is autogenerated or not.

SwaggerAuto: bool = True

Methods

Configure

Configures a component by passing configuration parameters.

(c *CommandableHttpController) Configure(ctx context.Context, config *cconf.ConfigParams)

  • ctx: context.Context - operation context.
  • config: *cconf.ConfigParams - configuration parameters to be set.

Register

Registers all controller’s routes in the HTTP endpoint.

(c *CommandableHttpController) Register()

Examples

type MyCommandableHttpController struct {
	*CommandableHttpController
}

func NewMyCommandableHttpController() *MyCommandableHttpController {
	c := MyCommandableHttpController{
		CommandableHttpController: controller.NewCommandableHttpController("dummies"),
	}
	c.DependencyResolver.Put(context.Background(), "controller", cref.NewDescriptor("pip-controller-dummies", "controller", "default", "*", "*"))
	return &c
}

controller := NewMyCommandableHttpController();
controller.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
	"connection.protocol", "http",
	"connection.host", "localhost",
	"connection.port", 8080,
));
controller.SetReferences(context.Background(), cref.NewReferencesFromTuples(
	cref.NewDescriptor("mygroup","controller","default","default","1.0"), controller
));

opnErr := controller.Open(context.Background(), "123")
if opnErr == nil {
	fmt.Println("The REST controller is running on port 8080");
}

See also