multi fixtures

master
Paulo Simão 2021-03-21 23:08:13 -03:00
parent 12e471958e
commit 04577b693a
1 changed files with 1 additions and 65 deletions

View File

@ -27,21 +27,11 @@ func Init() API {
mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "POST": case "POST":
h_SomeAPI2(w, r)
case "PUT":
h_SomeAPI(w, r) h_SomeAPI(w, r)
default: default:
http.Error(w, "Method not allowed", 500) http.Error(w, "Method not allowed", 500)
} }
}) })
mux.HandleFunc("/someapi3", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
h_SomeAPI3(w, r)
default:
http.Error(w, "Method not allowed", 500)
}
})
return ret return ret
} }
@ -50,7 +40,7 @@ func h_SomeAPI(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r) ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w) ctx = context.WithValue(ctx, "RES", w)
req := &Noop{} var req string
if r.Method != http.MethodGet && r.Method != http.MethodHead { if r.Method != http.MethodGet && r.Method != http.MethodHead {
err := json.NewDecoder(r.Body).Decode(&req) err := json.NewDecoder(r.Body).Decode(&req)
if err != nil { if err != nil {
@ -71,57 +61,3 @@ func h_SomeAPI(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
func h_SomeAPI2(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w)
req := &AStr2{}
if r.Method != http.MethodGet && r.Method != http.MethodHead {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
res, err := SomeAPI2(ctx, req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Add("Content-Type", "Application/json")
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
func h_SomeAPI3(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w)
req := &AStr{}
if r.Method != http.MethodGet && r.Method != http.MethodHead {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
res, err := SomeAPI3(ctx, req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Add("Content-Type", "Application/json")
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}