Skip to content

Instantly share code, notes, and snippets.

@yutakahashi114
Last active June 28, 2021 11:43
Show Gist options
  • Save yutakahashi114/99d46c09c0d47f8bf9cf703e46ea1348 to your computer and use it in GitHub Desktop.
Save yutakahashi114/99d46c09c0d47f8bf9cf703e46ea1348 to your computer and use it in GitHub Desktop.
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