-
Notifications
You must be signed in to change notification settings - Fork 0
/
restapi.go
54 lines (45 loc) · 1.35 KB
/
restapi.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
package ltt
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
func RunAPIServer(lt *LoadTest) error {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if lt.Config.Verbose {
lt.Log.Println("http: / request")
}
lt.Stats.Lock()
lt.Stats.Calculate()
data, err := json.Marshal(lt)
lt.Stats.Unlock()
if err != nil {
lt.Log.Printf("error marshalling stats: %s\n", err.Error())
writer.WriteHeader(http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write(data)
})
http.HandleFunc("/set-num-users", func(writer http.ResponseWriter, request *http.Request) {
numUsers, _ := strconv.Atoi(request.URL.Query().Get("num-users"))
lt.Log.Printf("http: /set-num-users request, num-users: %d\n", numUsers)
lt.TargetUserNum = numUsers
writer.WriteHeader(http.StatusOK)
})
http.HandleFunc("/reset", func(writer http.ResponseWriter, request *http.Request) {
lt.Log.Println("http: /reset request")
lt.Stats.Lock()
lt.Stats.Reset()
lt.Stats.Unlock()
writer.WriteHeader(http.StatusOK)
})
lt.Log.Printf("Starting REST API on %s:%d", lt.Config.APIHost, lt.Config.APIPort)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", lt.Config.APIHost, lt.Config.APIPort), nil)
if err != nil {
return err
}
return nil
}