package httpapi import ( "errors" "fmt" "net/http" "sea-vhost/internal/scope" "strconv" "strings" "time" "sea-vhost/internal/service" "sea-vhost/internal/store" ) func (s *Server) dashboard(w http.ResponseWriter, r *http.Request) { user := currentUser(r) plan, usage, err := s.store.PlanAndUsage(r.Context(), *user.TenantID) if err != nil { writeError(w, http.StatusInternalServerError, "无法读取套餐信息") return } projects, err := s.projectsForUser(r, user) if err != nil { writeError(w, http.StatusInternalServerError, "无法读取项目") return } activities, err := s.store.Activities(r.Context(), *user.TenantID, 10) if err != nil { writeError(w, http.StatusInternalServerError, "无法读取操作日志") return } writeJSON(w, http.StatusOK, map[string]any{"plan": plan, "usage": usage, "projects": projects, "activities": activities, "hostScoped": user.ProjectID != nil, "domainApproval": s.store.DomainApproval(r.Context(), s.cfg.DomainApproval), "rootDomain": s.cfg.RootDomain, "projectRoot": s.cfg.TenantRoot}) } func (s *Server) activities(w http.ResponseWriter, r *http.Request) { user := currentUser(r) if value := r.URL.Query().Get("projectId"); value != "" { id, err := strconv.ParseUint(value, 10, 64) if err != nil || id != scope.Project(r.Context()) { writeError(w, http.StatusNotFound, "主机不存在") return } } items, err := s.store.Activities(r.Context(), *user.TenantID, 100) if err != nil { writeError(w, http.StatusInternalServerError, "无法读取操作日志") return } writeJSON(w, http.StatusOK, map[string]any{"activities": items}) } func (s *Server) runtimeCatalog(w http.ResponseWriter, r *http.Request) { user := currentUser(r) if allowed, retryAfter := s.catalogLimiter.allow(fmt.Sprintf("runtime:%d", user.ID), time.Now(), 30); !allowed { seconds := (retryAfter + time.Second - 1) / time.Second w.Header().Set("Retry-After", strconv.FormatInt(int64(seconds), 10)) writeError(w, http.StatusTooManyRequests, "运行环境查询过于频繁,请稍后再试") return } catalog, err := s.projects.RuntimeCatalog(r.Context()) if err != nil { writeError(w, http.StatusBadGateway, "无法读取宝塔运行环境") return } writeJSON(w, http.StatusOK, map[string]any{"versions": catalog}) } func (s *Server) listProjects(w http.ResponseWriter, r *http.Request) { user := currentUser(r) projects, err := s.projectsForUser(r, user) if err != nil { writeError(w, http.StatusInternalServerError, "无法读取项目") return } writeJSON(w, http.StatusOK, map[string]any{"projects": projects}) } func (s *Server) projectsForUser(r *http.Request, user store.User) ([]store.Project, error) { if user.ProjectID != nil { project, err := s.store.Project(r.Context(), *user.TenantID, *user.ProjectID) if err != nil { return nil, err } projects := []store.Project{project} return projects, s.store.FillProjectBandwidth(r.Context(), projects) } projects, err := s.store.ListProjects(r.Context(), *user.TenantID) if err != nil { return nil, err } return projects, s.store.FillProjectBandwidth(r.Context(), projects) } func (s *Server) createProject(w http.ResponseWriter, r *http.Request) { var input service.CreateProjectInput if err := decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) result, err := s.projects.Create(r.Context(), user, input) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "create", "project", input.Name, requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(scope.WithProject(r.Context(), result.Project.ID, result.Project.NodeID), user.TenantID, user.ID, "create", "project", strconv.FormatUint(result.Project.ID, 10), requestClientIP(r), true, "创建项目 "+result.Project.Slug) writeJSON(w, http.StatusCreated, result) } func projectID(r *http.Request) (uint64, error) { return strconv.ParseUint(r.PathValue("id"), 10, 64) } func writeProjectMutationError(w http.ResponseWriter, status int, err error) { if errors.Is(err, store.ErrManagementLocked) { writeCodedError(w, 423, "management_locked", err.Error()) return } if errors.Is(err, store.ErrHostNotWritable) { writeCodedError(w, http.StatusLocked, "host_not_writable", err.Error()) return } if errors.Is(err, service.ErrQuotaSuspended) { writeCodedError(w, http.StatusLocked, "quota_suspended", "资源套餐已超额,当前仅允许读取、停止服务或清理数据") return } writeError(w, status, err.Error()) } func (s *Server) setProjectState(w http.ResponseWriter, r *http.Request) { id, err := projectID(r) if err != nil { writeError(w, http.StatusBadRequest, "项目编号无效") return } var input struct { Enabled bool `json:"enabled"` } if err = decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) if err = s.projects.SetState(r.Context(), user, id, input.Enabled); err != nil { writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "state", "project", strconv.FormatUint(id, 10), requestClientIP(r), true, map[bool]string{true: "启动项目", false: "停止项目"}[input.Enabled]) w.WriteHeader(http.StatusNoContent) } func (s *Server) setProjectRunPath(w http.ResponseWriter, r *http.Request) { id, err := projectID(r) if err != nil { writeError(w, http.StatusBadRequest, "项目编号无效") return } var input struct { RunPath string `json:"runPath"` } if err = decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) project, err := s.projects.SetRunPath(r.Context(), user, id, input.RunPath) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "update", "run_path", strconv.FormatUint(id, 10), requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "update", "run_path", strconv.FormatUint(id, 10), requestClientIP(r), true, "运行目录改为 "+project.RunPath) writeJSON(w, http.StatusOK, map[string]any{"project": project}) } func (s *Server) projectCredentials(w http.ResponseWriter, r *http.Request) { id, err := projectID(r) if err != nil { writeError(w, http.StatusBadRequest, "主机编号无效") return } result, err := s.projects.Credentials(r.Context(), currentUser(r), id) if err != nil { status := http.StatusUnprocessableEntity if errors.Is(err, store.ErrNotFound) { status = http.StatusNotFound } writeError(w, status, "无法读取主机凭据") return } writeJSON(w, http.StatusOK, map[string]any{"credentials": result.Credentials}) } func (s *Server) resetHostPassword(w http.ResponseWriter, r *http.Request) { id, err := projectID(r) if err != nil { writeError(w, http.StatusBadRequest, "主机编号无效") return } user := currentUser(r) password, err := s.projects.ResetHostPassword(r.Context(), user, id) if err != nil { status := http.StatusUnprocessableEntity if errors.Is(err, store.ErrNotFound) { status = http.StatusNotFound } s.store.Log(r.Context(), user.TenantID, user.ID, "reset", "host_password", strconv.FormatUint(id, 10), requestClientIP(r), false, err.Error()) writeError(w, status, err.Error()) return } s.store.Log(r.Context(), user.TenantID, user.ID, "reset", "host_password", strconv.FormatUint(id, 10), requestClientIP(r), true, "重置主机登录密码并注销旧会话") writeJSON(w, http.StatusOK, map[string]string{"password": password}) } func (s *Server) projectSites(w http.ResponseWriter, r *http.Request) { id, err := projectID(r) if err != nil { writeError(w, http.StatusBadRequest, "主机编号无效") return } user := currentUser(r) if r.Method == http.MethodGet { sites, readErr := s.projects.Sites(r.Context(), user, id) if readErr != nil { writeError(w, http.StatusNotFound, "虚拟主机不存在") return } writeJSON(w, http.StatusOK, map[string]any{"sites": sites}) return } var input service.CreateSiteInput if err = decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } site, err := s.projects.CreateSite(r.Context(), user, id, input) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "create", "site", input.Name, requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "create", "site", strconv.FormatUint(site.ID, 10), requestClientIP(r), true, "创建网站 "+site.Name) writeJSON(w, http.StatusCreated, map[string]any{"site": site}) } func siteID(r *http.Request) (uint64, error) { return strconv.ParseUint(r.PathValue("siteID"), 10, 64) } func (s *Server) setSiteState(w http.ResponseWriter, r *http.Request) { projectID, projectErr := projectID(r) siteID, siteErr := siteID(r) if projectErr != nil || siteErr != nil { writeError(w, http.StatusBadRequest, "网站编号无效") return } var input struct { Enabled bool `json:"enabled"` } if err := decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) site, err := s.projects.SetSiteState(r.Context(), user, projectID, siteID, input.Enabled) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "state", "site", strconv.FormatUint(siteID, 10), requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "state", "site", strconv.FormatUint(siteID, 10), requestClientIP(r), true, map[bool]string{true: "启动网站", false: "停止网站"}[input.Enabled]) writeJSON(w, http.StatusOK, map[string]any{"site": site}) } func (s *Server) checkSiteDeployment(w http.ResponseWriter, r *http.Request) { projectID, projectErr := projectID(r) siteID, siteErr := siteID(r) if projectErr != nil || siteErr != nil { writeError(w, http.StatusBadRequest, "网站编号无效") return } var input struct { EntryFile string `json:"entryFile"` } if err := decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) check, err := s.projects.CheckDeployment(r.Context(), user, projectID, siteID, input.EntryFile) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "check", "deployment", strconv.FormatUint(siteID, 10), requestClientIP(r), false, err.Error()) writeError(w, http.StatusUnprocessableEntity, err.Error()) return } s.store.Log(r.Context(), user.TenantID, user.ID, "check", "deployment", strconv.FormatUint(siteID, 10), requestClientIP(r), check.Ready, check.Message) writeJSON(w, http.StatusOK, map[string]any{"check": check}) } func (s *Server) deploySite(w http.ResponseWriter, r *http.Request) { projectID, projectErr := projectID(r) siteID, siteErr := siteID(r) if projectErr != nil || siteErr != nil { writeError(w, http.StatusBadRequest, "网站编号无效") return } var input struct { EntryFile string `json:"entryFile"` } if err := decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) site, err := s.projects.DeploySite(r.Context(), user, projectID, siteID, input.EntryFile) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "deploy", "site", strconv.FormatUint(siteID, 10), requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "deploy", "site", strconv.FormatUint(siteID, 10), requestClientIP(r), true, "语言项目已启动并接入 Nginx") writeJSON(w, http.StatusOK, map[string]any{"site": site}) } func (s *Server) deleteSite(w http.ResponseWriter, r *http.Request) { projectID, projectErr := projectID(r) siteID, siteErr := siteID(r) if projectErr != nil || siteErr != nil { writeError(w, http.StatusBadRequest, "网站编号无效") return } user := currentUser(r) if err := s.projects.DeleteSite(r.Context(), user, projectID, siteID); err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "delete", "site", strconv.FormatUint(siteID, 10), requestClientIP(r), false, err.Error()) writeError(w, http.StatusUnprocessableEntity, err.Error()) return } s.store.Log(r.Context(), user.TenantID, user.ID, "delete", "site", strconv.FormatUint(siteID, 10), requestClientIP(r), true, "删除网站配置,保留主机目录文件") w.WriteHeader(http.StatusNoContent) } func (s *Server) bindDomain(w http.ResponseWriter, r *http.Request) { id, err := projectID(r) if err != nil { writeError(w, http.StatusBadRequest, "项目编号无效") return } var input struct { Domain string `json:"domain"` } if err = decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) domain, err := s.projects.BindDomain(r.Context(), user, id, strings.TrimSpace(input.Domain)) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "bind", "domain", input.Domain, requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "bind", "domain", input.Domain, requestClientIP(r), true, "绑定自定义域名") writeJSON(w, http.StatusAccepted, map[string]any{"domain": domain}) } func (s *Server) bindSiteDomain(w http.ResponseWriter, r *http.Request) { projectID, projectErr := projectID(r) siteID, siteErr := siteID(r) if projectErr != nil || siteErr != nil { writeError(w, http.StatusBadRequest, "网站编号无效") return } var input struct { Domain string `json:"domain"` } if err := decodeJSON(r, &input); err != nil { writeError(w, http.StatusBadRequest, "请求格式错误") return } user := currentUser(r) domain, err := s.projects.BindSiteDomain(r.Context(), user, projectID, siteID, strings.TrimSpace(input.Domain)) if err != nil { s.store.Log(r.Context(), user.TenantID, user.ID, "bind", "domain", input.Domain, requestClientIP(r), false, err.Error()) writeProjectMutationError(w, http.StatusUnprocessableEntity, err) return } s.store.Log(r.Context(), user.TenantID, user.ID, "bind", "domain", input.Domain, requestClientIP(r), true, "为网站 "+strconv.FormatUint(siteID, 10)+" 绑定自定义域名") writeJSON(w, http.StatusAccepted, map[string]any{"domain": domain}) }