feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,186 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sea-vhost/internal/store"
|
||||
)
|
||||
|
||||
var tenantSlugPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{1,46}[a-z0-9]$`)
|
||||
|
||||
type adminPlanInput struct {
|
||||
Name string `json:"name"`
|
||||
StorageBytes uint64 `json:"storageBytes"`
|
||||
DatabaseBytes uint64 `json:"databaseBytes"`
|
||||
BandwidthBytes uint64 `json:"bandwidthBytes"`
|
||||
BandwidthMbps uint32 `json:"bandwidthMbps"`
|
||||
ProcessLimit uint32 `json:"processLimit"`
|
||||
FTPLimit uint32 `json:"ftpLimit"`
|
||||
DatabaseLimit uint32 `json:"databaseLimit"`
|
||||
ProjectLimit uint32 `json:"projectLimit"`
|
||||
}
|
||||
|
||||
func (s *Server) adminPlans(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
plans, err := s.store.ListPlans(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "无法读取套餐")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"plans": plans})
|
||||
return
|
||||
}
|
||||
var input adminPlanInput
|
||||
if err := decodeJSON(r, &input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
input.Name = strings.TrimSpace(input.Name)
|
||||
if err := validatePlan(input); err != nil {
|
||||
writeError(w, http.StatusUnprocessableEntity, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := s.store.CreatePlan(r.Context(), store.NewPlan{Name: input.Name, StorageBytes: input.StorageBytes, DatabaseBytes: input.DatabaseBytes, BandwidthBytes: input.BandwidthBytes, BandwidthMbps: input.BandwidthMbps, ProcessLimit: input.ProcessLimit, FTPLimit: input.FTPLimit, DatabaseLimit: input.DatabaseLimit, ProjectLimit: input.ProjectLimit})
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnprocessableEntity, "无法创建套餐,请检查名称是否重复")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
s.store.Log(r.Context(), nil, user.ID, "create", "plan", input.Name, requestClientIP(r), true, "创建套餐 "+input.Name)
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"id": id})
|
||||
}
|
||||
|
||||
func validatePlan(input adminPlanInput) error {
|
||||
if input.BandwidthMbps > 10000 {
|
||||
return errors.New("限速应在 0–10000 Mbps 之间,0 为不限速")
|
||||
}
|
||||
const maxBytes = uint64(1 << 50)
|
||||
if len([]rune(input.Name)) < 1 || len([]rune(input.Name)) > 80 {
|
||||
return errors.New("套餐名称应为 1–80 个字符")
|
||||
}
|
||||
if input.StorageBytes < 1<<20 || input.StorageBytes > maxBytes || input.DatabaseBytes < 1<<20 || input.DatabaseBytes > maxBytes || input.BandwidthBytes < 1<<20 || input.BandwidthBytes > maxBytes {
|
||||
return errors.New("资源容量必须在 1 MB 至 1 PB 之间")
|
||||
}
|
||||
if input.ProcessLimit < 1 || input.ProcessLimit > 100000 || input.FTPLimit < 1 || input.FTPLimit > 10000 || input.DatabaseLimit < 1 || input.DatabaseLimit > 10000 || input.ProjectLimit < 1 || input.ProjectLimit > 10000 {
|
||||
return errors.New("资源数量限制超出允许范围")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) adminTenants(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
items, err := s.store.ListTenants(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "无法读取用户")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"tenants": items})
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Password string `json:"password"`
|
||||
PlanID uint64 `json:"planId"`
|
||||
ExpiresAt *time.Time `json:"expiresAt"`
|
||||
}
|
||||
if err := decodeJSON(r, &input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
input.Slug = strings.ToLower(strings.TrimSpace(input.Slug))
|
||||
if !tenantSlugPattern.MatchString(input.Slug) || strings.TrimSpace(input.Name) == "" || len(input.Password) < 12 {
|
||||
writeError(w, http.StatusUnprocessableEntity, "用户标识、名称或密码不符合要求")
|
||||
return
|
||||
}
|
||||
address, err := mail.ParseAddress(strings.TrimSpace(input.Email))
|
||||
if err != nil || !strings.EqualFold(address.Address, strings.TrimSpace(input.Email)) {
|
||||
writeError(w, http.StatusUnprocessableEntity, "邮箱格式不正确")
|
||||
return
|
||||
}
|
||||
input.Name = strings.TrimSpace(input.Name)
|
||||
input.DisplayName = strings.TrimSpace(input.DisplayName)
|
||||
if len([]rune(input.Name)) > 120 || len([]rune(input.DisplayName)) < 2 || len([]rune(input.DisplayName)) > 80 || input.PlanID == 0 {
|
||||
writeError(w, http.StatusUnprocessableEntity, "用户名称、管理员名称或套餐无效")
|
||||
return
|
||||
}
|
||||
if _, err = s.store.Plan(r.Context(), input.PlanID); err != nil {
|
||||
writeError(w, http.StatusUnprocessableEntity, "所选套餐不存在")
|
||||
return
|
||||
}
|
||||
id, err := s.store.CreateTenant(r.Context(), store.NewTenant{Slug: input.Slug, Name: input.Name, Email: strings.TrimSpace(input.Email), DisplayName: input.DisplayName, Password: input.Password, PlanID: input.PlanID, ExpiresAt: input.ExpiresAt})
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnprocessableEntity, "无法创建用户,请检查标识或邮箱是否重复")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
s.store.Log(r.Context(), nil, user.ID, "create", "tenant", input.Slug, requestClientIP(r), true, "创建用户 "+input.Name)
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"id": id})
|
||||
}
|
||||
|
||||
func (s *Server) domainApprovalSetting(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"enabled": s.store.DomainApproval(r.Context(), s.cfg.DomainApproval)})
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
if err := decodeJSON(r, &input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
if err := s.store.SetDomainApproval(r.Context(), input.Enabled); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "无法保存设置")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
s.store.Log(r.Context(), nil, user.ID, "update", "domain_approval", "", requestClientIP(r), true, map[bool]string{true: "开启域名审核", false: "关闭域名审核"}[input.Enabled])
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) adminPendingDomains(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := s.store.PendingDomains(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "无法读取待审核域名")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"domains": items})
|
||||
}
|
||||
|
||||
func (s *Server) adminReviewDomain(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "域名编号无效")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Approved bool `json:"approved"`
|
||||
}
|
||||
if err = decodeJSON(r, &input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
item, err := s.projects.ReviewDomain(r.Context(), id, input.Approved)
|
||||
if err != nil {
|
||||
s.store.Log(r.Context(), nil, user.ID, "review", "domain", strconv.FormatUint(id, 10), requestClientIP(r), false, err.Error())
|
||||
writeProjectMutationError(w, http.StatusUnprocessableEntity, err)
|
||||
return
|
||||
}
|
||||
tenantID := item.TenantID
|
||||
detail := "拒绝域名 " + item.Domain
|
||||
if input.Approved {
|
||||
detail = "批准域名 " + item.Domain
|
||||
}
|
||||
s.store.Log(r.Context(), &tenantID, user.ID, "review", "domain", strconv.FormatUint(id, 10), requestClientIP(r), true, detail)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"domain": item})
|
||||
}
|
||||
Reference in new issue
Block a user