api

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2016 License: Apache-2.0 Imports: 13 Imported by: 0

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

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 NewAPI

func NewAPI(m *gobot.Master) *API

NewAPI returns a new api instance

func (*API) AddHandler

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

AddHandler appends handler to api handlers

func (*API) Debug

func (a *API) Debug()

Debug add handler to api that prints each request

func (*API) Delete

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

Delete wraps api router Delete call

func (*API) Get

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

Get wraps api router Get call

func (*API) Head

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

Head wraps api router Head call

func (*API) Options

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

Options wraps api router Options call

func (*API) Post

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

Post wraps api router Post call

func (*API) Put

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

Put wraps api router Put call

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 c3pio routes and robeaux

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

func (c *CORS) AllowedHeaders() string

AllowedHeaders returns allowed headers in a string

func (*CORS) AllowedMethods

func (c *CORS) AllowedMethods() string

AllowedMethods returns allowed http methods in a string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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