Fix up tokenHandler (remove json hack)
This commit is contained in:
parent
2f0fd351a8
commit
12e3bac6ce
1 changed files with 32 additions and 4 deletions
36
handlers.go
36
handlers.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -221,10 +222,37 @@ func handleCreater(c creater) errorHandler {
|
||||||
|
|
||||||
func tokenHandler(h http.Handler) http.Handler {
|
func tokenHandler(h http.Handler) http.Handler {
|
||||||
token := func(w http.ResponseWriter, r *http.Request) {
|
token := func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Hackish, but we want the token in a JSON object
|
recorder := httptest.NewRecorder()
|
||||||
w.Write([]byte(`{"token":"`))
|
h.ServeHTTP(recorder, r)
|
||||||
h.ServeHTTP(w, r)
|
|
||||||
w.Write([]byte(`"}`))
|
for key, val := range recorder.Header() {
|
||||||
|
w.Header()[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
w.WriteHeader(recorder.Code)
|
||||||
|
|
||||||
|
tokenData := string(recorder.Body.Bytes())
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
|
||||||
|
if recorder.Code != 200 {
|
||||||
|
data, _ = json.Marshal(struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
}{
|
||||||
|
Error: tokenData,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
data, _ = json.Marshal(struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}{
|
||||||
|
Token: tokenData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(data)
|
||||||
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(token)
|
return http.HandlerFunc(token)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue