-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
124 lines (106 loc) · 2.85 KB
/
server.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os/exec"
"slices"
"strings"
)
const defaultPort = 8080
//go:embed static/*
var content embed.FS
type server struct {
config config
}
func newServer() server {
return server{config: config{Port: defaultPort}}
}
func (s *server) serve() error {
http.Handle("GET /", staticHandler())
http.HandleFunc("GET /commands", s.commandsHandler)
http.HandleFunc("PUT /command/{command}", s.commandHandler)
http.HandleFunc("GET /all", s.allHandler)
addr := net.JoinHostPort(s.config.Address, fmt.Sprint(s.config.Port))
return http.ListenAndServe(addr, nil)
}
func (s *server) allHandler(w http.ResponseWriter, r *http.Request) {
all, err := getSystemInfo(s.config.WatchMountpoints)
if err != nil {
log.Printf("collection failed %v", err)
http.Error(w, "Failed to collect data", http.StatusInternalServerError)
return
}
all.Commands = s.getCommands()
data, err := json.Marshal(all)
if err != nil {
log.Printf("json encoding failed: %v", err)
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
log.Printf("failed to write response: %v", err)
}
}
func (s *server) commandsHandler(w http.ResponseWriter, r *http.Request) {
commands := s.getCommands()
data, err := json.Marshal(commands)
if err != nil {
log.Printf("json encoding failed: %v", err)
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
log.Printf("failed to write response: %v", err)
}
}
func (s *server) getCommands() []string {
commands := make([]string, 0, len(s.config.Commands))
for k := range s.config.Commands {
commands = append(commands, k)
}
slices.Sort(commands)
return commands
}
func (s *server) commandHandler(w http.ResponseWriter, r *http.Request) {
const defaultSize = 256
cmdFromPath := s.config.Commands[r.PathValue("command")]
if len(cmdFromPath) == 0 {
http.Error(w, "Command does not exist", http.StatusInternalServerError)
return
}
command := exec.Command(cmdFromPath[0])
if len(cmdFromPath) > 1 {
command = exec.Command(cmdFromPath[0], cmdFromPath[1:]...)
}
rawOutput, err := command.CombinedOutput()
output := fmt.Sprintf("$ %v\n%s", strings.Join(command.Args, " "),
truncate(string(rawOutput), defaultSize, "..."))
if err != nil {
log.Printf("command %q failed: %s: %s", command, err, rawOutput)
http.Error(w, strings.TrimSpace(output), http.StatusInternalServerError)
return
}
_, err = fmt.Fprint(w, output)
if err != nil {
log.Printf("failed to write response: %v", err)
}
}
func staticHandler() http.Handler {
files, err := fs.Sub(content, "static")
if err != nil {
panic(err)
}
return http.FileServerFS(files)
}
func truncate(s string, max int, ellip string) string {
runes := []rune(s)
if len(runes) > max {
return string(runes[:max]) + ellip
}
return s
}