DPF/main.go

42 lines
752 B
Go
Raw Permalink Normal View History

2019-11-01 13:47:11 +00:00
package main
import (
"log"
2019-11-03 18:00:35 +00:00
"os"
2019-11-01 13:47:11 +00:00
2019-11-03 18:40:20 +00:00
"github.com/davecgh/go-spew/spew"
2019-11-01 13:47:11 +00:00
"github.com/gin-gonic/gin"
)
2019-11-03 18:00:35 +00:00
func (worker *Worker) OperateHandler(cxt *gin.Context) {
2019-11-03 18:40:20 +00:00
//log.Println(worker.Sensor())
2019-11-04 06:29:08 +00:00
cmd := CommandType(cxt.PostForm("command"))
worker.SendCommand(cmd)
log.Println("SendCommand: ", cmd)
2019-11-03 18:00:35 +00:00
}
func (worker *Worker) StatusHandler(cxt *gin.Context) {
2019-11-03 18:40:20 +00:00
cxt.Writer.WriteString(spew.Sprint(worker.Sensor()))
2019-11-03 18:00:35 +00:00
}
2019-11-01 13:47:11 +00:00
func main() {
2019-11-02 07:48:16 +00:00
2019-11-03 18:00:35 +00:00
f, err := os.OpenFile("./log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
log.SetOutput(f)
2019-11-01 13:47:11 +00:00
engine := gin.Default()
2019-11-03 18:00:35 +00:00
worker := NewWorker()
engine.GET("/status", worker.StatusHandler)
engine.POST("/operate", worker.OperateHandler)
go worker.Run()
2019-11-01 13:47:11 +00:00
log.Fatal(engine.Run(":15678"))
2019-11-02 07:48:16 +00:00
2019-11-01 13:47:11 +00:00
}