-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgopherling.go
190 lines (148 loc) · 3.99 KB
/
gopherling.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
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/gophergala/gopherling/loader"
"github.com/gophergala/gopherling/models"
"github.com/gorilla/websocket"
"github.com/julienschmidt/httprouter"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"net/http"
)
var (
database *mgo.Database
databaseSession *mgo.Session
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
func showTests(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
results := make([]models.Test, 0)
err := database.C("tests").Find(bson.M{}).All(&results)
if err != nil {
w.WriteHeader(500)
return
}
js, err := json.Marshal(results)
if err != nil {
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(js)
}
func addTest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
decoder := json.NewDecoder(r.Body)
var t models.Test
err := decoder.Decode(&t)
if err != nil {
w.WriteHeader(400)
return
}
tests := database.C("tests")
// i'm in a hurry :p
if len(t.Id) > 0 {
err = database.C("tests").RemoveId(t.Id)
}
t.Id = bson.NewObjectId()
err = tests.Insert(&t)
if err != nil {
w.WriteHeader(500)
} else {
js, err := json.Marshal(t)
if err != nil {
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
w.Write(js)
}
}
func showTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var t models.Test
err := database.C("tests").Find(bson.M{"_id": bson.ObjectIdHex(ps.ByName("id"))}).One(&t)
if err != nil {
w.WriteHeader(404)
return
}
js, err := json.Marshal(t)
if err != nil {
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(js)
}
func updateTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "I want to update a test, (id: %s)!\n", ps.ByName("id"))
}
func deleteTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
err := database.C("tests").RemoveId(bson.ObjectIdHex(ps.ByName("id")))
if err != nil {
w.WriteHeader(400)
return
}
w.WriteHeader(200)
}
func startTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
return
}
var t models.Test
err = database.C("tests").Find(bson.M{"_id": bson.ObjectIdHex(ps.ByName("id"))}).One(&t)
if err != nil {
if err := conn.WriteMessage(websocket.TextMessage, []byte("Not found")); err != nil {
fmt.Println("An error occured: the requested test doesn't exist")
}
return
}
l := loader.New(t.Requests, t.Concurrency, conn)
for _, task := range t.Tasks {
l.AddTasks(task.Method, t.BaseUrl, task.Path, task.Headers, task.RawBody)
}
l.Run()
}
func main() {
// Database infos
var dbHost, dbPort string
flag.StringVar(&dbHost, "dbHost", "127.0.0.1", "mongoDB host")
flag.StringVar(&dbPort, "dbPort", "27017", "mongoDB port")
flag.Parse()
// Initialize our database connection
databaseSession, err := mgo.Dial(dbHost + ":" + dbPort)
if err != nil {
panic("Couldn't connect to the database server")
} else {
fmt.Println("Successfully connected to the database server")
}
defer databaseSession.Close()
// Select the appropriate database
database = databaseSession.DB("gopherling")
// Create a new router instance
router := httprouter.New()
// View all the tests
router.GET("/api/tests", showTests)
// Add a test
router.POST("/api/tests", addTest)
// View a single test
router.GET("/api/tests/:id", showTest)
// Update a test
router.PUT("/api/tests/:id", updateTest)
// Delete a test
router.DELETE("/api/tests/:id", deleteTest)
// Start a test
router.GET("/api/tests/:id/start", startTest)
// Catch-all (angular app)
router.NotFound = http.HandlerFunc(http.FileServer(http.Dir("static")).ServeHTTP)
// Start listening
fmt.Println("Gopherling server started on port 9410")
http.ListenAndServe(":9410", router)
}