fix(manager): replace Unmarshal with Decode

Instead of repeating io.Read/Unmarshal pattern, use the Decoder,
which also gets us content-type negotiation.
pull/396/head
Matt Butcher 9 years ago
parent 3cd679da63
commit dec2f0605a

@ -17,7 +17,6 @@ limitations under the License.
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
@ -298,19 +297,11 @@ func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler s
func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common.Template {
util.LogHandlerEntry(handler, r)
j, err := getJSONFromRequest(w, r, handler)
if err != nil {
return nil
}
t := &common.Template{}
if err := json.Unmarshal(j, t); err != nil {
e := fmt.Errorf("%v\n%v", err, string(j))
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
if err := httputil.Decode(w, r, t); err != nil {
httputil.BadRequest(w, r, err)
return nil
}
return t
}
@ -479,18 +470,12 @@ func getRegistryHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Co
func getRegistry(w http.ResponseWriter, r *http.Request, handler string) *common.Registry {
util.LogHandlerEntry(handler, r)
j, err := getJSONFromRequest(w, r, handler)
if err != nil {
return nil
}
t := &common.Registry{}
if err := json.Unmarshal(j, t); err != nil {
e := fmt.Errorf("%v\n%v", err, string(j))
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
if err := httputil.Decode(w, r, t); err != nil {
httputil.BadRequest(w, r, err)
return nil
}
return t
}
@ -611,18 +596,11 @@ func getFileHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Contex
func getCredential(w http.ResponseWriter, r *http.Request, handler string) *common.RegistryCredential {
util.LogHandlerEntry(handler, r)
j, err := getJSONFromRequest(w, r, handler)
if err != nil {
return nil
}
t := &common.RegistryCredential{}
if err := json.Unmarshal(j, t); err != nil {
e := fmt.Errorf("%v\n%v", err, string(j))
util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
if err := httputil.Decode(w, r, t); err != nil {
httputil.BadRequest(w, r, err)
return nil
}
return t
}

@ -17,14 +17,15 @@ limitations under the License.
package main
import (
"github.com/kubernetes/helm/cmd/manager/router"
"github.com/kubernetes/helm/pkg/version"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/kubernetes/helm/cmd/manager/router"
"github.com/kubernetes/helm/pkg/httputil"
"github.com/kubernetes/helm/pkg/version"
)
var (
@ -52,6 +53,8 @@ func main() {
os.Exit(1)
}
httputil.DefaultEncoder.MaxReadLen = c.Config.MaxTemplateLength
// Set up routes
handler := router.NewHandler(c)
registerDeploymentRoutes(c, handler)

Loading…
Cancel
Save