gobot.io/x/gobot/v2@v2.1.0/examples/hello_api_custom.go (about)

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