webapi

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: Apache-2.0, BSD-2-Clause Imports: 14 Imported by: 2

README

Web API

The web API interface allows to access functionality of the node software via exposed http endpoints.

How to use the API

The default port to access the web API is set to 8080:8080/tcp in docker-compose.yml, where the first port number is the internal port number within the node software, and the second for the access from an http port. An example where these two would be set to different values, or the external port is not utilized, can be found in the docker-network tool (see also the docker-compose.yml file in the docker-network tool folder).

The server instance of the web API is contacted via webapi.Server(). Next we need to register a route with a matching handler.

webapi.Server().ROUTE(path string, h HandlerFunc)

where ROUTE will be replaced later in this documentation by GET or POST. The HandlerFunc defines a function to serve HTTP requests that gives access to the Context

func HandlerFunc(c Context) error

We can then use the Context to send a JSON response to the node:

JSON(statuscode int, i interface{}) error

An implementation example is shown later for the POST method.

GET and POST

Two methods are currently employed. First, with GET we register a new GET route for a handler function. The handler is accessed via the address path. The handler for a GET method can set the node to perform certain actions.

webapi.Server().GET("path", HandlerFunc)

A command can be sent to the node software to the API, e.g. via command prompt:

curl "http://127.0.0.1:8080/path?command"

$$ . $$

Second, with POST we register a new POST route for a handler function. The handler can receive a JSON body input and send specific messages to the tangle.

webapi.Server().POST("path", HandlerFunc)

For example, the following Handler broadcastData sends a data message to the tangle

func broadcastData(c echo.Context) error {
	var request Request
	if err := c.Bind(&request); err != nil {
		log.Info(err.Error())
		return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
	}

	msg, err := issuer.IssuePayload(
		payload.NewGenericDataPayload(request.Data), messagelayer.Tangle())
	if err != nil {
		return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
	}
	return c.JSON(http.StatusOK, Response{ID: msg.ID().String()})
}

As an example the JSON body

{
	"data":"HelloWor"
}

can be sent to http://127.0.0.1:8080/data, which will issue a data message containing "HelloWor" (note that in this example the data input is size limited.)

Documentation

Index

Constants

View Source
const (
	// CfgBindAddress defines the config flag of the web API binding address.
	CfgBindAddress = "webapi.bindAddress"
	// CfgBasicAuthEnabled defines the config flag of the webapi basic auth enabler.
	CfgBasicAuthEnabled = "webapi.basic_auth.enabled"
	// CfgBasicAuthUsername defines the config flag of the webapi basic auth username.
	CfgBasicAuthUsername = "webapi.basic_auth.username"
	// CfgBasicAuthPassword defines the config flag of the webapi basic auth password.
	CfgBasicAuthPassword = "webapi.basic_auth.password"
)
View Source
const PluginName = "WebAPI"

PluginName is the name of the web API plugin.

Variables

This section is empty.

Functions

func IndexRequest

func IndexRequest(c echo.Context) error

IndexRequest returns INDEX

func Plugin added in v0.2.0

func Plugin() *node.Plugin

Plugin gets the plugin instance.

func Server

func Server() *echo.Echo

Server gets the server instance.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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