package fire import ( "net/http" "git.mokkon.com/sainw/server_helper/util" ) func ApiHandler(ep *EndPoint) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) util.Return { ctx := r.Context() err := ep.Load(ctx, r) if err != nil { return util.NewErrorStatus().SetMessage(err.Error()) } var doc map[string]interface{} switch r.Method { case "POST": if doc, err = ep.Post(ctx); err != nil { return util.NewErrorStatus().SetMessage(err.Error()) } case "PUT": if doc, err = ep.Put(ctx); err != nil { return util.NewErrorStatus().SetMessage(err.Error()) } case "DELETE": if doc, err = ep.Delete(ctx); err != nil { return util.NewErrorStatus().SetMessage(err.Error()) } default: return util.NewErrorStatus().SetMessage("invalid method") } return util.NewOkStatus().SetData(doc) } return util.NoCache(util.Cors(Auth(ep.app, util.Json(fn)))) }