gobot.io/x/gobot/v2@v2.1.0/examples/hello_api_auth.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  	"html"
    12  	"net/http"
    13  
    14  	"gobot.io/x/gobot/v2"
    15  	"gobot.io/x/gobot/v2/api"
    16  )
    17  
    18  func main() {
    19  	master := gobot.NewMaster()
    20  
    21  	a := api.NewAPI(master)
    22  	a.AddHandler(api.BasicAuth("gort", "klatuu"))
    23  	a.Debug()
    24  
    25  	a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
    26  		fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
    27  	})
    28  	a.Start()
    29  
    30  	master.AddCommand("custom_gobot_command",
    31  		func(params map[string]interface{}) interface{} {
    32  			return "This command is attached to the mcp!"
    33  		})
    34  
    35  	hello := master.AddRobot(gobot.NewRobot("hello"))
    36  
    37  	hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} {
    38  		return fmt.Sprintf("This command is attached to the robot %v", hello.Name)
    39  	})
    40  
    41  	master.Start()
    42  }