Last active
June 28, 2021 11:43
-
-
Save yutakahashi114/99d46c09c0d47f8bf9cf703e46ea1348 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"strings" | |
"github.com/go-chi/chi/v5" | |
"github.com/go-chi/render" | |
) | |
func main() { | |
... | |
mux.Post("/", func(w http.ResponseWriter, r *http.Request) { | |
b, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
render.JSON(w, r, http.StatusInternalServerError) | |
return | |
} | |
defer r.Body.Close() | |
out, err := route(r.Header.Get("X-Amz-Target"), b) | |
if err != nil { | |
log.Println(err) | |
render.JSON(w, r, http.StatusInternalServerError) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
w.Write(out) | |
}) | |
log.Println(http.ListenAndServe(":80", mux)) | |
} | |
func route(xAmzTarget string, body []byte) ([]byte, error) { | |
targets := strings.Split(xAmzTarget, ".") | |
if len(targets) < 2 { | |
return nil, fmt.Errorf("invalid header") | |
} | |
switch targets[1] { | |
case "AdminInitiateAuth": | |
return adminInitiateAuth(body) | |
case "AdminCreateUser": | |
return adminCreateUser(body) | |
... | |
} | |
return nil, fmt.Errorf("invalid operation name") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment