Blame view

go/src/themaru/http.go 6.72 KB
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
1
2
3
4
5
  package main
  
  import (
  	"html/template"
  	"net/http"
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  	"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>
c35246804   김태훈   기능 추가 및 변경
25
      <p>System Version: {{.falinux_version}}</p>
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
26
27
    </div>
    <hr>
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    <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>
  `))
c35246804   김태훈   기능 추가 및 변경
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  var networkPage = template.Must(template.New("network.html").Parse(`
  <html>
  <head>
    <title>Network Configuration</title>
    <style type="text/css">
      div { margin-left: 20px; }
    </style>
  </head>
  <body>
    <form action="/network" method="post">
      <h1>Setting Network</h1>
      <div>
        <p>
          <label for="ethaddr">Ethernet Address</label><br />
          <input type="text" id="ethaddr" name="ethaddr" value="{{.ethaddr}}" required />
        </p>
        <p>
          <label for="ipaddr">IP Address</label><br />
          <input type="text" id="ipaddr" name="ipaddr" value="{{.ipaddr}}" required />
        </p>
        <p>
          <label for="netmask">Netmask</label><br />
          <input type="text" id="netmask" name="netmask" value="{{.netmask}}" required />
        </p>
        <p>
          <label for="gateway">Gateway</label><br />
          <input type="text" id="gateway" name="gateway" value="{{.gateway}}" required />
        </p>
      </div>
      <label for="submit"><h1>Click to Apply</h1></label>
      <div>
        <p><input id="submit" type="submit" value="Set" /></p>
      </div>
    </form>
  </body>
  </html>
  `))
  
  var networkPageDual = template.Must(template.New("network.html").Parse(`
  <html>
  <head>
    <title>Network Configuration</title>
    <style type="text/css">
      div { margin-left: 20px; }
    </style>
  </head>
  <body>
    <form action="/network" method="post">
      <h1>Setting Network</h1>
      <div>
        <p>
          <label for="ethaddr">Ethernet Address</label><br />
          <input type="text" id="ethaddr" name="ethaddr" value="{{.ethaddr}}" required />
        </p>
        <p>
          <label for="ipaddr">IP Address</label><br />
          <input type="text" id="ipaddr" name="ipaddr" value="{{.ipaddr}}" required />
        </p>
        <p>
          <label for="netmask">Netmask</label><br />
          <input type="text" id="netmask" name="netmask" value="{{.netmask}}" required />
        </p>
        <p>
          <label for="gateway">Gateway</label><br />
          <input type="text" id="gateway" name="gateway" value="{{.gateway}}" required />
        </p>
      </div>
      <h1>Setting Network 2</h1>
      <div>
        <p>
          <label for="ethaddr2">Ethernet Address</label><br />
          <input type="text" id="ethaddr2" name="ethaddr2" value="{{.ethaddr2}}" required />
        </p>
        <p>
          <label for="ipaddr2">IP Address</label><br />
          <input type="text" id="ipaddr2" name="ipaddr2" value="{{.ipaddr2}}" required />
        </p>
        <p>
          <label for="netmask2">Netmask</label><br />
          <input type="text" id="netmask2" name="netmask2" value="{{.netmask2}}" required />
        </p>
        <p>
          <label for="gateway2">Gateway</label><br />
          <input type="text" id="gateway2" name="gateway2" value="{{.gateway2}}" required />
        </p>
      </div>
      <label for="submit"><h1>Click to Apply</h1></label>
      <div>
        <p><input id="submit" type="submit" value="Set" /></p>
      </div>
    </form>
  </body>
  </html>
  `))
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  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)
c35246804   김태훈   기능 추가 및 변경
167
168
169
170
171
  	if hasDualNetwork {
  		r.Add("network.html", networkPageDual)
  	} else {
  		r.Add("network.html", networkPage)
  	}
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
172
173
174
  	g.HTMLRender = r
  
  	g.GET("/", renderIndex)
c35246804   김태훈   기능 추가 및 변경
175
  	g.GET("/network", renderNetwork)
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
176
c35246804   김태훈   기능 추가 및 변경
177
  	g.POST("/network", handleNetwork)
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
178
179
180
181
182
183
184
185
  	g.POST("/system", handleSystem)
  	g.POST("/application", handleApplication)
  
  	g.GET("/reboot", handleReboot)
  	g.GET("/restore", handleRestore)
  
  	g.Run(":8988")
  }
c35246804   김태훈   기능 추가 및 변경
186
  func handleNetwork(c *gin.Context) {
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
187
188
  	mu.Lock()
  	defer mu.Unlock()
c35246804   김태훈   기능 추가 및 변경
189
190
191
192
193
194
195
196
197
  	keyList := []string{"ethaddr", "ipaddr", "netmask", "gateway"}
  	if hasDualNetwork {
  		keyList = append(keyList, "ethaddr2", "ipaddr2", "netmask2", "gateway2")
  	}
  
  	env := map[string]string{}
  	for _, key := range keyList {
  		env[key] = c.PostForm(key)
  	}
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
198
c35246804   김태훈   기능 추가 및 변경
199
200
  	if err := setNetwork(env); err != nil {
  		c.String(http.StatusBadRequest, "network: setNetwork: %s", err.Error())
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
201
202
  		return
  	}
c35246804   김태훈   기능 추가 및 변경
203
  	c.HTML(http.StatusOK, "reboot.html", gin.H{"Message": "Network is configured."})
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  }
  
  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) {
c35246804   김태훈   기능 추가 및 변경
256
257
258
259
260
261
262
263
  	env, _ := getEnv(1)
  	env["Device"] = deviceName
  
  	c.HTML(http.StatusOK, "index.html", env)
  }
  
  func renderNetwork(c *gin.Context) {
  	env, _ := getEnv(0)
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
264
c35246804   김태훈   기능 추가 및 변경
265
  	c.HTML(http.StatusOK, "network.html", env)
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
266
267
268
269
270
271
272
273
274
275
  }
  
  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)
  }