diff options
Diffstat (limited to 'derelict.go')
-rw-r--r-- | derelict.go | 54 |
1 files changed, 19 insertions, 35 deletions
diff --git a/derelict.go b/derelict.go index ef0b6c3..d084e49 100644 --- a/derelict.go +++ b/derelict.go @@ -1,9 +1,7 @@ package main import ( - "crypto/sha1" "embed" - "encoding/hex" "encoding/json" "html/template" "io/ioutil" @@ -11,7 +9,6 @@ import ( "net/http" "os" "regexp" - "sort" "github.com/gorilla/mux" ) @@ -23,21 +20,24 @@ var content embed.FS var internal embed.FS var ( + storage Storage templates *template.Template - root string - validToken *regexp.Regexp + validToken = regexp.MustCompile(`[a-fA-F0-9]+`) ) func main() { - initRoot() - - validToken = regexp.MustCompile(`[a-fA-F0-9]+`) + storage.Path = os.Getenv("DERELICTROOT") + if storage.Path == "" { + storage.Path = ".derelict" + } port := os.Getenv("DERELICTPORT") if port == "" { port = "8080" } + storage.MustInit() + templates = template.Must(template.ParseFS(internal, "*.tmpl")) router := mux.NewRouter() @@ -49,23 +49,13 @@ func main() { log.Fatal(http.ListenAndServe(":"+port, router)) } -func initRoot() { - root = os.Getenv("DERELICTROOT") - if root == "" { - root = ".derelict" - } - os.Mkdir(root, 0755) - os.Mkdir(root+"/battles", 0755) - os.Mkdir(root+"/tokens", 0700) -} - func handleBattlesPost(w http.ResponseWriter, r *http.Request) { token := r.Header.Get("Derelict-Token") if !validToken.MatchString(token) { http.Error(w, "Invalid token", http.StatusBadRequest) return } - _, err := os.Stat(root + "/tokens/" + token) + _, err := os.Stat(storage.Path + "/tokens/" + token) if _, ok := err.(*os.PathError); ok { http.Error(w, "Unauthorized", http.StatusUnauthorized) return @@ -85,32 +75,26 @@ func handleBattlesPost(w http.ResponseWriter, r *http.Request) { http.Error(w, "Error in body", http.StatusBadRequest) return } - hash := sha1.Sum(data) - name := hex.EncodeToString(hash[:]) - ioutil.WriteFile(root+"/battles/"+name, data, 0644) + name, err := storage.AddBattle(data) + if err != nil { + http.Error(w, "Error writing", http.StatusInternalServerError) + return + } w.WriteHeader(http.StatusOK) w.Write([]byte(name)) } func handleBattlesId(w http.ResponseWriter, r *http.Request) { - id := mux.Vars(r)["id"] - w.Header().Add("Content-Type", "application/json") - http.ServeFile(w, r, root+"/battles/"+id) + storage.ServeBattle(mux.Vars(r)["id"], w, r) } func handleRecent(w http.ResponseWriter, r *http.Request) { - files, err := ioutil.ReadDir(root + "/battles") + battles, err := storage.ListRecentBattles(10) if err != nil { - panic(err) - } - sort.Slice(files, func(lhs, rhs int) bool { - return files[lhs].ModTime().After(files[rhs].ModTime()) - }) - count := 10 - if count > len(files) { - count = len(files) + http.Error(w, "Error getting recent battles", http.StatusInternalServerError) + return } - templates.ExecuteTemplate(w, "recent", files[:count]) + templates.ExecuteTemplate(w, "recent", battles) } func handleView(w http.ResponseWriter, r *http.Request) { |