Documentation ¶
Overview ¶
Package api provides a webserver to interact with your Gobot program over the network.
Example:
package main import ( "fmt" "gobot.io/x/gobot" "gobot.io/x/gobot/api" ) func main() { gbot := gobot.NewMaster() // Starts the API server on default port 3000 api.NewAPI(gbot).Start() // 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/robots/Eve/commands/say_hello hello.AddCommand("say_hello", func(params map[string]interface{}) interface{} { return fmt.Sprintf("%v says hello!", hello.Name) }) gbot.Start() }
It follows Common Protocol for Programming Physical Input and Output (CPPP-IO) spec: https://gobot.io/x/cppp-io
Index ¶
- func AllowRequestsFrom(allowedOrigins ...string) http.HandlerFunc
- func BasicAuth(username, password string) http.HandlerFunc
- type API
- func (a *API) AddC3PIORoutes()
- func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request))
- func (a *API) AddRobeauxRoutes()
- func (a *API) Debug()
- func (a *API) Delete(path string, f func(http.ResponseWriter, *http.Request))
- func (a *API) Get(path string, f func(http.ResponseWriter, *http.Request))
- func (a *API) Head(path string, f func(http.ResponseWriter, *http.Request))
- func (a *API) Options(path string, f func(http.ResponseWriter, *http.Request))
- func (a *API) Post(path string, f func(http.ResponseWriter, *http.Request))
- func (a *API) Put(path string, f func(http.ResponseWriter, *http.Request))
- func (a *API) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (a *API) Start()
- func (a *API) StartWithoutDefaults()
- type CORS
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowRequestsFrom ¶
func AllowRequestsFrom(allowedOrigins ...string) http.HandlerFunc
AllowRequestsFrom returns handler to verify that requests come from allowedOrigins
func BasicAuth ¶
func BasicAuth(username, password string) http.HandlerFunc
BasicAuth returns basic auth handler.
Types ¶
type API ¶
type API struct { Host string Port string Cert string Key string // contains filtered or unexported fields }
API represents an API server
func (*API) AddC3PIORoutes ¶ added in v1.12.0
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 ¶
func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request))
AddHandler appends handler to api handlers
func (*API) AddRobeauxRoutes ¶ added in v1.12.0
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) ServeHTTP ¶
func (a *API) ServeHTTP(res http.ResponseWriter, req *http.Request)
ServeHTTP calls api handlers and then serves request using api router
func (*API) Start ¶
func (a *API) Start()
Start initializes the api by setting up Robeaux web interface.
func (*API) StartWithoutDefaults ¶ added in v1.12.0
func (a *API) StartWithoutDefaults()
StartWithoutDefaults initializes the api without setting up the default routes. Good for custom web interfaces.
type CORS ¶
type CORS struct { AllowOrigins []string AllowHeaders []string AllowMethods []string ContentType string // contains filtered or unexported fields }
CORS represents CORS configuration
func (*CORS) AllowedHeaders ¶
AllowedHeaders returns allowed headers in a string
func (*CORS) AllowedMethods ¶
AllowedMethods returns allowed http methods in a string