// MakeHandler returns a handler function to serve a request
MakeHandler returns a handler function to serve a request
// MakeHandler returns a handler function to serve a request
func MakeHandler(factory func() Request) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		req := factory()
		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			w.WriteHeader(http.StatusBadRequest)
			WriteError(w, fmt.Errorf("Invalid JSON: %v", err))
			return
		}
		if err := req.Validate(); err != nil {
			w.WriteHeader(http.StatusBadRequest)
			WriteError(w, fmt.Errorf("Invalid request: %v", err))
			return
		}
		defer func() {
			if e := recover(); e != nil {
				w.WriteHeader(http.StatusInternalServerError)
				WriteError(w, fmt.Errorf("Internal server error"))
			}
		}()
		req.Handle(w)
	}
}
func MakeHandler ( factory func ( ) Request ) func ( http . ResponseWriter , * http . Request ) { return func ( w http . ResponseWriter , r * http . Request ) { w . Header ( ) . Set ( " " , " " ) req := factory ( ) if err := json . NewDecoder ( r . Body ) . Decode ( & req ) ; err != nil { w . WriteHeader ( http . StatusBadRequest ) WriteError ( w , fmt . Errorf ( " " , err ) ) return } if err := req . Validate ( ) ; err != nil { w . WriteHeader ( http . StatusBadRequest ) WriteError ( w , fmt . Errorf ( " " , err ) ) return } defer func ( ) { if e := recover ( ) ; e != nil { w . WriteHeader ( http . StatusInternalServerError ) WriteError ( w , fmt . Errorf ( " " ) ) } } ( ) req . Handle ( w ) } }
MakeHandler returns a handler function to serve a request
MakeHandler returns a handler function to serve a request
// Decode JSON // Validate request // Handle panics just in case