Files

55 lines
1.8 KiB
Go

package httpapi
import (
"net/http"
"sea-vhost/internal/hosting"
"strconv"
)
func (s *Server) siteSettings(w http.ResponseWriter, r *http.Request) {
project, _ := projectID(r)
site, _ := strconv.ParseUint(r.PathValue("siteID"), 10, 64)
if r.Method == "GET" {
v, e := s.projects.WebsiteSettings(r.Context(), currentUser(r), project, site)
if e != nil {
writeError(w, 404, "网站不存在或无法读取设置")
return
}
writeJSON(w, 200, map[string]any{"settings": v})
return
}
var v hosting.WebsiteSettings
if decodeJSON(r, &v) != nil {
writeError(w, 400, "设置格式无效")
return
}
if e := s.projects.SetWebsiteSettings(r.Context(), currentUser(r), project, site, v); e != nil {
writeError(w, 422, e.Error())
return
}
u := currentUser(r)
s.store.Log(r.Context(), u.TenantID, u.ID, "update", "site", strconv.FormatUint(site, 10), requestClientIP(r), true, "更新网站基础设置")
w.WriteHeader(204)
}
func (s *Server) siteLog(w http.ResponseWriter, r *http.Request) {
project, _ := projectID(r)
site, _ := strconv.ParseUint(r.PathValue("siteID"), 10, 64)
lines, _ := strconv.Atoi(r.URL.Query().Get("lines"))
v, e := s.projects.SiteLog(r.Context(), currentUser(r), project, site, r.URL.Query().Get("kind"), r.URL.Query().Get("search"), lines)
if e != nil {
writeError(w, 422, "无法读取该网站日志")
return
}
writeJSON(w, 200, v)
}
func (s *Server) hostMetrics(w http.ResponseWriter, r *http.Request) {
project, _ := projectID(r)
days, _ := strconv.Atoi(r.URL.Query().Get("days"))
v, e := s.store.HostMetrics(r.Context(), project, days)
if e != nil {
writeError(w, 503, "无法读取资源历史")
return
}
writeJSON(w, 200, map[string]any{"samples": v, "retentionDays": 30, "cpuUnit": "100% = 一个 CPU 核心"})
}