diff --git a/cmd/wasmcloud/handler_delete.go b/cmd/wasmcloud/handler_delete.go new file mode 100644 index 0000000..af7028f --- /dev/null +++ b/cmd/wasmcloud/handler_delete.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + "flag" + "fmt" + "io" + "log" + "net/http" + "os" + + "github.com/google/subcommands" +) + +type handlerDeleteCmd struct{} + +func (handlerDeleteCmd) SetFlags(fs *flag.FlagSet) {} +func (handlerDeleteCmd) Name() string { return "delete" } +func (handlerDeleteCmd) Synopsis() string { return "delete a handler" } +func (handlerDeleteCmd) Usage() string { + return `wasmcloud delete + +$ wasmcloud delete filename.wasm + +Deletes a handler on wasmcloud. Please run this with care. No data loss will +happen, but support will need to be contacted. + +Flags: +` +} + +func (h handlerDeleteCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + if fs.NArg() != 1 { + fmt.Println("usage: wasmcloud delete ") + return subcommands.ExitUsageError + } + + hname := fs.Arg(0) + + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, *apiServer+"/api/handler/delete?name="+hname, nil) + if err != nil { + log.Fatal(err) + } + withAPI(req) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + + if resp.StatusCode != http.StatusNoContent { + io.Copy(os.Stdout, resp.Body) + return subcommands.ExitFailure + } + + fmt.Println("deleted handler", hname) + + return subcommands.ExitSuccess +} diff --git a/cmd/wasmcloud/main.go b/cmd/wasmcloud/main.go index 8260cc6..465f48b 100644 --- a/cmd/wasmcloud/main.go +++ b/cmd/wasmcloud/main.go @@ -23,6 +23,7 @@ func main() { subcommands.Register(&whoamiCmd{}, "api") subcommands.Register(&handlerCreateCmd{}, "handlers") + subcommands.Register(&handlerDeleteCmd{}, "handlers") subcommands.Register(&handlerListCmd{}, "handlers") subcommands.Register(&handlerLogsCmd{}, "handlers") subcommands.Register(&handlerInvokeCmd{}, "handlers") diff --git a/cmd/wasmcloudd/handler.go b/cmd/wasmcloudd/handler.go index c111a59..2521472 100644 --- a/cmd/wasmcloudd/handler.go +++ b/cmd/wasmcloudd/handler.go @@ -16,6 +16,27 @@ import ( "within.website/olin/namegen" ) +func deleteHandler(w http.ResponseWriter, r *http.Request, u *User) { + ctx := r.Context() + q := r.URL.Query() + name := q.Get("name") + if name == "" { + http.NotFound(w, r) + return + } + + var hdlr Handler + err := db.Where("name = ?", name).First(&hdlr).Error + if err != nil { + ln.Error(ctx, err) + http.NotFound(w, r) + return + } + + db.Delete(&hdlr) + w.WriteHeader(http.StatusNoContent) +} + func createHandler(w http.ResponseWriter, r *http.Request, u *User) { if !u.CanCreateHandlers { http.Error(w, "you can't create handlers, contact support", http.StatusForbidden) diff --git a/cmd/wasmcloudd/main.go b/cmd/wasmcloudd/main.go index 846441e..72f3c84 100644 --- a/cmd/wasmcloudd/main.go +++ b/cmd/wasmcloudd/main.go @@ -57,6 +57,7 @@ func main() { // API routes rtr.HandleFunc("/api/handler", makeHandler(true, listHandlers)).Methods(http.MethodGet) + rtr.HandleFunc("/api/handler/delete", makeHandler(true, deleteHandler)).Methods(http.MethodDelete) rtr.HandleFunc("/api/handler/create", makeHandler(true, createHandler)).Methods(http.MethodPost) rtr.HandleFunc("/api/handler/logs", makeHandler(true, getLogs)).Methods(http.MethodGet) rtr.HandleFunc("/api/whoami", makeHandler(true, apiWhoami)) diff --git a/cmd/wasmcloudd/models.go b/cmd/wasmcloudd/models.go index e65db86..b2c7c16 100644 --- a/cmd/wasmcloudd/models.go +++ b/cmd/wasmcloudd/models.go @@ -36,9 +36,9 @@ type ExecutionLog struct { type Handler struct { gorm.Model Name string `gorm:"unique;not null"` - Path string `gorm:"unique;not null"` - UserID uint `json:"-"` - User User `json:"-"` + Path string + UserID uint `json:"-"` + User User `json:"-"` } func (t Token) ToCookie() *http.Cookie {