gobot.io/x/gobot@v1.16.0/examples/hello_api_custom.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/api"
    13  )
    14  
    15  func main() {
    16  	master := gobot.NewMaster()
    17  
    18  	a := api.NewAPI(master)
    19  
    20  	// creates routes/handlers for the custom API
    21  	a.Get("/", func(res http.ResponseWriter, req *http.Request) {
    22  		res.Write([]byte("OK"))
    23  	})
    24  	a.Get("/api/hello", func(res http.ResponseWriter, req *http.Request) {
    25  		msg := fmt.Sprintf("This command is attached to the robot %v", master.Robot("hello").Name)
    26  		res.Write([]byte(msg))
    27  	})
    28  
    29  	// starts the API without the default C2PIO API and Robeaux web interface.
    30  	a.StartWithoutDefaults()
    31  
    32  	master.AddRobot(gobot.NewRobot("hello"))
    33  
    34  	master.Start()
    35  }