aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/delete.go
blob: 49a13266e452bfca79eb646fd3baf2ec78daaf51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main

import (
	"fmt"
	"net/http"
)

func gameDelete(w http.ResponseWriter, r *http.Request) {
	if unauthorizedIfNotLocal(w, r) {
		return
	}

	err := r.ParseForm()
	if err != nil {
		Logger.Printf("Installer: While parsing form: %s", err)
		http.Error(w, fmt.Sprintf("Invalid form: %s", err), 400)
		return
	}

	game := r.PostForm.Get("name")

	if game == "" {
		Logger.Println("Deleter: No game specified")
		http.Error(w, "Game param required", 400)
		return
	}

	Lib.Lock()
	g, ok := Lib.Games[game]
	Lib.Unlock()
	if !ok {
		Logger.Printf("Missing: %s", game)
		http.Error(w, "Game is missing", 404)
		return
	}

	err = g.Delete()
	if err != nil {
		Logger.Printf("Error removing game: %s", err)
		http.Error(w, fmt.Sprintf("Error removing game: %s", err), 500)
		return
	}
	Logger.Printf("Removed game: %s", game)

	reloadLib()
	http.Redirect(w, r, "/", 302)
}