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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"html"
    10  	"net/http"
    11  
    12  	"gobot.io/x/gobot"
    13  	"gobot.io/x/gobot/api"
    14  )
    15  
    16  func main() {
    17  	master := gobot.NewMaster()
    18  
    19  	a := api.NewAPI(master)
    20  	a.AddHandler(api.BasicAuth("gort", "klatuu"))
    21  	a.Debug()
    22  
    23  	a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
    24  		fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
    25  	})
    26  	a.Start()
    27  
    28  	master.AddCommand("custom_gobot_command",
    29  		func(params map[string]interface{}) interface{} {
    30  			return "This command is attached to the mcp!"
    31  		})
    32  
    33  	hello := master.AddRobot(gobot.NewRobot("hello"))
    34  
    35  	hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} {
    36  		return fmt.Sprintf("This command is attached to the robot %v", hello.Name)
    37  	})
    38  
    39  	master.Start()
    40  }