-
Notifications
You must be signed in to change notification settings - Fork 0
/
contoller.go
62 lines (54 loc) · 1.39 KB
/
contoller.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"github.com/flosch/pongo2"
"github.com/goji/param"
"github.com/zenazn/goji/web"
"net/http"
"strconv"
)
func IndexController(c web.C, w http.ResponseWriter, r *http.Request) {
tpl, err := pongo2.DefaultSet.FromFile("./view/index.tpl")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tables := []Table{}
DB.Find(&tables)
tpl.ExecuteWriter(pongo2.Context{"naptlist": tables}, w)
}
func CreateController(c web.C, w http.ResponseWriter, r *http.Request) {
var table Table
r.ParseForm()
err := param.Parse(r.Form, &table)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result := Allocate(table.TargetIP, table.TargetPort, table.BoundPort)
if result != true {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, "Create")
}
func RemoveController(c web.C, w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(c.URLParams["id"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
table := Table{}
DB.First(&table, id)
fmt.Print(table.Id, table.TargetIP)
if table.Id == 0 {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
result := Release(table.TargetIP, table.TargetPort, table.BoundPort)
if result != true {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, "Remove")
}