aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_dashboard.go
blob: f7e7a6f9cba68ca1b62edd95b523f1bbc1912ba9 (plain)
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
package admin

import (
	"net/http"
	"os"
	"path/filepath"
)

func (s *AdminServer) handleDashboard(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	countYAML := func(subdir string) int {
		dir := filepath.Join(s.dataDir, subdir)
		n := 0
		filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
			if err != nil || d.IsDir() {
				return nil
			}
			if filepath.Ext(d.Name()) == ".yaml" {
				n++
			}
			return nil
		})
		return n
	}

	dashboard := map[string]any{
		"roomCount":    countYAML("rooms"),
		"itemCount":    countYAML("items"),
		"objectCount":  countYAML("objects"),
		"mobCount":     countYAML("mobs"),
		"playerCount":  countYAML("players/characters"),
		"accountCount": countYAML("players/accounts"),
	}
	writeJSON(w, dashboard)
}