Repository navigation
Expand file tree
/
Copy pathfileserver.go
More file actions
74 lines (56 loc) · 1.75 KB
/
Copy pathfileserver.go
File metadata and controls
74 lines (56 loc) · 1.75 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cmd
import (
"net/http"
"os"
"path"
"strings"
"github.com/saulmaldonado/agones-mc/internal/config"
"github.com/saulmaldonado/agones-mc/pkg/fileserver"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var fileServerCmd = cobra.Command{
Use: "fileserver",
Short: "Minecraft GameServer pod file server",
Long: "Pod file server for viewing and editing minecraft world data and config files in the minecraft server's data directory",
Run: func(cmd *cobra.Command, args []string) {
if err := Run(); err != nil {
logger.Fatal("file server error", zap.Error(err))
}
},
}
func init() {
RootCmd.AddCommand(&fileServerCmd)
}
func Run() error {
cfg := config.NewFileServerConfig()
vol := cfg.GetVolume()
http.Handle("/", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
if err := fileserver.GetFile(rw, r); err != nil {
logger.Error("error getting file", zap.Error(err))
}
case http.MethodPost:
if err := fileserver.UploadFile(rw, r, vol); err != nil {
logger.Error("error uploading new file", zap.Error(err))
}
case http.MethodPut:
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
if _, err := os.Stat(path.Join(vol, r.URL.Path)); os.IsNotExist(err) {
http.Error(rw, err.Error(), http.StatusNotFound)
return
}
if err := fileserver.UploadFile(rw, r, vol); err != nil {
logger.Error("error editing file", zap.Error(err))
}
case http.MethodDelete:
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
if err := fileserver.DeleteFile(rw, r, path.Join(r.URL.Path, vol), vol); err != nil {
logger.Error("error deleting file", zap.Error(err))
}
}
}))
logger.Info("starting server on :8080")
return http.ListenAndServe(":8080", nil)
}