handler deletion, closes #21

This commit is contained in:
Cadey Ratio 2019-12-13 04:55:20 +00:00
parent e0bba0452c
commit 6ac270463f
5 changed files with 85 additions and 3 deletions

View File

@ -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 <handler-name>
$ 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 <handler-name>")
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
}

View File

@ -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")

View File

@ -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)

View File

@ -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))

View File

@ -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 {