仰望星辰工作室

miaopanel

miaopanel/ internal/httpapi/update.go 1.3 KB · 46 行 原始文件
1package httpapi
2
3import (
4 "net/http"
5 "time"
6)
7
8// handleUpdateCheck reports whether a newer panel build is available.
9func (s *Server) handleUpdateCheck(w http.ResponseWriter, r *http.Request) {
10 if !s.updater.Enabled() {
11 writeOK(w, s.updater.DisabledInfo())
12 return
13 }
14 info, err := s.updater.Check(r.Context())
15 if err != nil {
16 writeJSON(w, http.StatusBadGateway, Envelope{Code: 502, Msg: "检查更新失败: " + err.Error()})
17 return
18 }
19 writeOK(w, info)
20}
21
22// handleUpdateApply downloads, verifies and installs the newest build, then
23// asks systemd to restart the panel.
24func (s *Server) handleUpdateApply(w http.ResponseWriter, r *http.Request) {
25 var req struct {
26 Confirm string `json:"confirm"`
27 }
28 if err := decodeJSON(w, r, &req, 4<<10); err != nil {
29 writeErr(w, err)
30 return
31 }
32 // An explicit phrase keeps a stray JSON client from replacing the binary.
33 if req.Confirm != "UPDATE" {
34 writeJSON(w, http.StatusBadRequest, Envelope{Code: 400, Msg: "确认信息不正确"})
35 return
36 }
37 started := time.Now()
38 res, err := s.updater.Apply(r.Context())
39 if err != nil {
40 s.audit(r, "system", "update", "panel", "failed", started)
41 writeJSON(w, http.StatusBadRequest, Envelope{Code: 400, Msg: err.Error()})
42 return
43 }
44 s.audit(r, "system", "update", res.Version, "ok", started)
45 writeOK(w, res)
46}