2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2018-08-16 05:17:57 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2018-08-16 05:17:57 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
2018-08-16 05:17:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager := gobot.NewManager()
|
2018-08-16 05:17:57 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
a := api.NewAPI(manager)
|
2018-08-16 17:22:22 +08:00
|
|
|
|
|
|
|
// creates routes/handlers for the custom API
|
2018-08-16 05:17:57 +08:00
|
|
|
a.Get("/", func(res http.ResponseWriter, req *http.Request) {
|
2024-02-11 22:34:50 +08:00
|
|
|
if _, err := res.Write([]byte("OK")); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2018-08-16 05:17:57 +08:00
|
|
|
})
|
|
|
|
a.Get("/api/hello", func(res http.ResponseWriter, req *http.Request) {
|
2024-02-13 23:30:25 +08:00
|
|
|
msg := fmt.Sprintf("This command is attached to the robot %v", manager.Robot("hello").Name)
|
2024-02-11 22:34:50 +08:00
|
|
|
if _, err := res.Write([]byte(msg)); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2018-08-16 05:17:57 +08:00
|
|
|
})
|
2018-08-16 17:22:22 +08:00
|
|
|
|
2018-08-17 16:32:06 +08:00
|
|
|
// starts the API without the default C2PIO API and Robeaux web interface.
|
|
|
|
a.StartWithoutDefaults()
|
2018-08-16 05:17:57 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
manager.AddRobot(gobot.NewRobot("hello"))
|
2018-08-16 05:17:57 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
if err := manager.Start(); err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-16 05:17:57 +08:00
|
|
|
}
|