package main import ( "html/template" "net/http" "os" "sync" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) var mu sync.Mutex var indexPage = template.Must(template.New("index.html").Parse(` <html> <head> <title>System Configuration</title> <style type="text/css"> div { margin-left: 20px; } </style> </head> <body> <h1>{{.Device}} Configuration</h1> <div> <p>System Version: {{.Version}}</p> </div> <hr> <h1>Setting IP Address</h1> <div> <form action="/address" method="post"> <input type="text" name="address" value="{{.IP}}" required /> <input type="text" name="address2" value="{{.IP2}}" required /> <input type="submit" value="Set" /> </form> </div> <h1>Updating System</h1> <div> <form action="/system" method="post" enctype="multipart/form-data"> <input type="file" name="image" /> <input type="submit" value="Update" /> </form> </div> <h1>Updating Application</h1> <div> <form action="/application" method="post" enctype="multipart/form-data"> <input type="file" name="image" /> <input type="submit" value="Update" /> </form> </div> <h1>Restore System Image</h1> <div> <form action="/restore"> <input type="submit" value="Restore" /> </form> </div> </body> </html> `)) var rebootPage = template.Must(template.New("reboot.html").Parse(` <html> <head> <title>OK</title> </head> <body> <p>{{.Message}}</p> <form action="/reboot/"> <input type="submit" value="Reboot" /> </form> </body> </html> `)) func runHttp() { gin.SetMode(gin.ReleaseMode) g := gin.Default() r := multitemplate.NewRenderer() r.Add("index.html", indexPage) r.Add("reboot.html", rebootPage) g.HTMLRender = r g.GET("/", renderIndex) g.POST("/address", handleAddress) g.POST("/system", handleSystem) g.POST("/application", handleApplication) g.GET("/reboot", handleReboot) g.GET("/restore", handleRestore) g.Run(":8988") } func handleAddress(c *gin.Context) { mu.Lock() defer mu.Unlock() addr := c.PostForm("address") addr2 := c.PostForm("address2") if err := setIpAddress(addr, addr2); err != nil { c.String(http.StatusBadRequest, "address: setIpAddress: %s", err.Error()) return } c.HTML(http.StatusOK, "reboot.html", gin.H{"Message": "IP address is updated."}) } func handleSystem(c *gin.Context) { mu.Lock() defer mu.Unlock() if err := saveFormFile(c, "image", "/tmp/system.zip"); err != nil { c.String(http.StatusBadRequest, "system: saveFormFile: %s", err.Error()) return } if err := updateSystem(); err != nil { c.String(http.StatusBadRequest, "system: updateSystem: %s", err.Error()) return } c.HTML(http.StatusOK, "reboot.html", gin.H{"Message": "System is updated."}) } func handleApplication(c *gin.Context) { mu.Lock() defer mu.Unlock() if err := saveFormFile(c, "image", "/tmp/application.zip"); err != nil { c.String(http.StatusBadRequest, "application: saveFormFile: %s", err.Error()) return } if err := updateApp(); err != nil { c.String(http.StatusBadRequest, "application: updateApplication: %s", err.Error()) return } c.HTML(http.StatusOK, "reboot.html", gin.H{"Message": "Application is updated."}) } func handleReboot(c *gin.Context) { reboot() c.String(http.StatusOK, "OK") } func handleRestore(c *gin.Context) { mu.Lock() defer mu.Unlock() restore() c.HTML(http.StatusOK, "reboot.html", gin.H{"Message": "Restoration is done."}) } func renderIndex(c *gin.Context) { device := "System" if len(os.Args) > 1 { device = os.Args[1] } c.HTML(http.StatusOK, "index.html", gin.H{ "Device": device, "IP": getIpAddress(), "IP2": getIpAddress2(), "Version": getVersion(), }) } func saveFormFile(c *gin.Context, name string, dst string) error { file, err := c.FormFile(name) if err != nil { return err } return c.SaveUploadedFile(file, dst) }