/
handlers.go
179 lines (149 loc) · 5.06 KB
/
handlers.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"net/http"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/go-zoo/bone"
"github.com/jinzhu/gorm"
"github.com/mholt/binding"
"github.com/unrolled/render"
)
// DBHandler used for passing database connection to handlers
type DBHandler struct {
db *gorm.DB
r *render.Render
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
// setting logger
log.WithFields(log.Fields{
"url_query": r.URL.Query(),
"url_path": r.URL.Path,
}).Info("Getting home view")
ren := render.New(render.Options{
Layout: "layout",
})
w.Header().Set("Content-Type", "application/json")
ren.HTML(w, http.StatusOK, "example", nil)
}
func (h *DBHandler) stuboShowHandler(rw http.ResponseWriter, req *http.Request) {
var stuboInstances []Stubo
// Getting all stubo instances
stuboInstances = h.getAllInstances()
// setting logger
log.WithFields(log.Fields{
"count": len(stuboInstances),
}).Info("Getting all Stubo instances")
newmap := map[string]interface{}{"metatitle": "Stubo Instances", "Instances": stuboInstances}
h.r.HTML(rw, http.StatusOK, "stubos", newmap)
}
// stubosCreateHandler inserts a new guitar into the db.
func (h *DBHandler) stubosCreateHandler(rw http.ResponseWriter, req *http.Request) {
h.stubosEdit(rw, req, 0)
}
// stubosEdit is shared between the create and update handler, since they share most of the logic.
func (h *DBHandler) stubosEdit(rw http.ResponseWriter, req *http.Request, id uint) {
stuboForm := new(StuboForm)
log.WithFields(log.Fields{
"id": id,
"url_path": req.URL.Path,
}).Info("Entering stubosEdti")
// validate form
if err := binding.Bind(req, stuboForm); err.Handle(rw) {
fmt.Println(err.Error())
return
}
// assing form variables to stubo struct
stubo := Stubo{Name: stuboForm.Name, Version: stuboForm.Version, Hostname: stuboForm.Hostname,
Port: stuboForm.Port, Protocol: stuboForm.Protocol}
h.db.Create(&stubo)
log.WithFields(log.Fields{
"id": id,
"url_path": req.URL.Path,
}).Info("Stubo added")
// TODO: should add some messages to user about successful creation
h.stuboShowHandler(rw, req)
}
// stuboDestroyHandler deletes a stubo instance from the db.
func (h *DBHandler) stuboDestroyHandler(rw http.ResponseWriter, req *http.Request) {
id := bone.GetValue(req, "id")
stubo := Stubo{}
h.db.Delete(&stubo, id)
log.WithFields(log.Fields{
"id": id,
"url_path": req.URL.Path,
}).Info("Stubo deleted")
h.r.JSON(rw, http.StatusOK, map[string]string{"data": "Stubo instance deleted!"})
}
func (h *DBHandler) stuboDetailedHandler(rw http.ResponseWriter, req *http.Request) {
id := bone.GetValue(req, "id")
u, err := strconv.ParseUint(id, 10, 64)
if err != nil {
http.Error(rw, "Bad stubo ID.", 400)
return
}
var stubo Stubo
// getting stubo from database
stubo = h.getStuboDetails(u)
stuboURI := stubo.Protocol + "://" + stubo.Hostname + ":" + stubo.Port
// getting all scenarios
client := &Client{&http.Client{}}
scenarios, err := client.getScenarios(stuboURI)
if err != nil {
http.Error(rw, "Failed to get scenarios from Stubo!", 400)
return
}
log.WithFields(log.Fields{
"id": id,
"url_path": req.URL.Path,
"scenario_count": len(scenarios),
}).Info("Stubo details fetched")
newmap := map[string]interface{}{"metatitle": "Stubo Details", "Stubo": stubo, "Scenarios": scenarios}
h.r.HTML(rw, http.StatusOK, "stuboDetails", newmap)
}
func (h *DBHandler) scenarioDetailedHandler(rw http.ResponseWriter, req *http.Request) {
// stubo ID, should be stored in database
id := bone.GetValue(req, "id")
u, err := strconv.ParseUint(id, 10, 64)
if err != nil {
http.Error(rw, "Bad stubo ID.", 400)
return
}
var stubo Stubo
// getting stubo from database
stubo = h.getStuboDetails(u)
// getting all scenarios
client := &Client{&http.Client{}}
scenarioName := bone.GetValue(req, "scenario")
stuboURI := stubo.Protocol + "://" + stubo.Hostname + ":" + stubo.Port
scenario, err := client.getScenarioDetails(stuboURI, scenarioName)
if err != nil {
http.Error(rw, "Failed to get scenario details from Stubo!", 400)
return
}
newmap := map[string]interface{}{"metatitle": "Scenario Details", "Stubo": stubo, "Scenario": scenario}
h.r.HTML(rw, http.StatusOK, "scenarioDetails", newmap)
}
func (h *DBHandler) scenarioStubsHandler(rw http.ResponseWriter, req *http.Request) {
// stubo ID, should be stored in database
id := bone.GetValue(req, "id")
u, err := strconv.ParseUint(id, 10, 64)
if err != nil {
http.Error(rw, "Bad stubo ID.", 400)
return
}
var stubo Stubo
// getting stubo from database
stubo = h.getStuboDetails(u)
// getting all scenarios
client := &Client{&http.Client{}}
scenarioName := bone.GetValue(req, "scenario")
stuboURI := stubo.Protocol + "://" + stubo.Hostname + ":" + stubo.Port
stubs, err := client.getScenarioStubs(stuboURI, scenarioName)
if err != nil {
http.Error(rw, "Failed to get scenario stubs from Stubo!", 400)
return
}
newmap := map[string]interface{}{"metatitle": "Scenario Stubs", "Scenario": scenarioName, "Stubo": stubo, "Stubs": stubs}
h.r.HTML(rw, http.StatusOK, "scenarioStubs", newmap)
}