Make golang performance test return JSON instead of string
This commit is contained in:
parent
665881471d
commit
2f0a582aa7
|
@ -1,16 +1,30 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"os"
|
"net/http"
|
||||||
"net/http"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TestJSONResponse struct {
|
||||||
|
Test bool
|
||||||
|
}
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
|
response := TestJSONResponse{true}
|
||||||
|
|
||||||
|
js, err := json.Marshal(response)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(js)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
http.ListenAndServe(":" + os.Args[1], nil)
|
http.ListenAndServe(":"+os.Args[1], nil)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user