-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
38 lines (34 loc) · 1.09 KB
/
main.go
File metadata and controls
38 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Network struct {
Address string `form:"address" json:"address" url:"address" xml:"address" binding:"required"`
Netmask string `form:"netmask" json:"netmask" url:"netmask" xml:"netmask" binding:"required"`
Gateway string `form:"gateway" json:"gateway" url:"gateway" xml:"gateway" binding:"required"`
DNS []string `form:"dns" json:"dns" url:"dns" xml:"dns" binding:"required"`
}
func main() {
r := gin.Default()
r.GET("netcfg", func(c *gin.Context) {
c.JSON(http.StatusOK, GetNetwork())
})
r.POST("netcfg", func(c *gin.Context) {
var json Network
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if n := NetworkConfig(json.Address, json.Netmask, json.Gateway, json.DNS); !n {
c.JSON(http.StatusInternalServerError, gin.H{"status": "500"})
return
}
c.JSON(http.StatusOK, gin.H{
"info": "restart machine to apply new configure."})
})
r.GET("metrics", func(c *gin.Context) {
c.JSON(http.StatusOK, metrics())
})
r.Run(":8753")
}