gobot: gobot.io/x/gobot/api Index | Files | Directories

package api

import "gobot.io/x/gobot/api"

Package api provides a webserver to interact with your Gobot program over the network.


package main

import (


func main() {
	gbot := gobot.NewMaster()

  // Starts the API server on default port 3000

  // Accessible via http://localhost:3000/api/commands/say_hello
	gbot.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
		return "Master says hello!"

	hello := gbot.AddRobot(gobot.NewRobot("Eve"))

  // Accessible via http://localhost:3000/api/robots/Eve/commands/say_hello
	hello.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
		return fmt.Sprintf("%v says hello!", hello.Name)


It follows Common Protocol for Programming Physical Input and Output (CPPP-IO) spec: https://gobot.io/x/cppp-io


Package Files

api.go basic_auth.go cors.go doc.go

func AllowRequestsFrom Uses

func AllowRequestsFrom(allowedOrigins ...string) http.HandlerFunc

AllowRequestsFrom returns handler to verify that requests come from allowedOrigins

func BasicAuth Uses

func BasicAuth(username, password string) http.HandlerFunc

BasicAuth returns basic auth handler.

type API Uses

type API struct {
    Host string
    Port string
    Cert string
    Key  string
    // contains filtered or unexported fields

API represents an API server

func NewAPI Uses

func NewAPI(m *gobot.Master) *API

NewAPI returns a new api instance

func (*API) AddC3PIORoutes Uses

func (a *API) AddC3PIORoutes()

AddC3PIORoutes adds all of the standard C3PIO routes to the API. For more information, please see: http://cppp.io/

func (*API) AddHandler Uses

func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request))

AddHandler appends handler to api handlers

func (*API) AddRobeauxRoutes Uses

func (a *API) AddRobeauxRoutes()

AddRobeauxRoutes adds all of the robeaux web interface routes to the API. The Robeaux web interface requires the C3PIO API, so it is also activated when you call this method.

func (*API) Debug Uses

func (a *API) Debug()

Debug add handler to api that prints each request

func (*API) Delete Uses

func (a *API) Delete(path string, f func(http.ResponseWriter, *http.Request))

Delete wraps api router Delete call

func (*API) Get Uses

func (a *API) Get(path string, f func(http.ResponseWriter, *http.Request))

Get wraps api router Get call

func (*API) Head Uses

func (a *API) Head(path string, f func(http.ResponseWriter, *http.Request))

Head wraps api router Head call

func (*API) Options Uses

func (a *API) Options(path string, f func(http.ResponseWriter, *http.Request))

Options wraps api router Options call

func (*API) Post Uses

func (a *API) Post(path string, f func(http.ResponseWriter, *http.Request))

Post wraps api router Post call

func (*API) Put Uses

func (a *API) Put(path string, f func(http.ResponseWriter, *http.Request))

Put wraps api router Put call

func (*API) ServeHTTP Uses

func (a *API) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP calls api handlers and then serves request using api router

func (*API) Start Uses

func (a *API) Start()

Start initializes the api by setting up Robeaux web interface.

func (*API) StartWithoutDefaults Uses

func (a *API) StartWithoutDefaults()

StartWithoutDefaults initializes the api without setting up the default routes. Good for custom web interfaces.

type CORS Uses

type CORS struct {
    AllowOrigins []string
    AllowHeaders []string
    AllowMethods []string
    ContentType  string
    // contains filtered or unexported fields

CORS represents CORS configuration

func (*CORS) AllowedHeaders Uses

func (c *CORS) AllowedHeaders() string

AllowedHeaders returns allowed headers in a string

func (*CORS) AllowedMethods Uses

func (c *CORS) AllowedMethods() string

AllowedMethods returns allowed http methods in a string



Package api imports 13 packages (graph) and is imported by 1 packages. Updated 2019-10-16. Refresh now. Tools for package owners.