60 lines
2.5 KiB
Go
60 lines
2.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
var installableRuntimes = []string{"Node.js", "Python", "Go", "Java"}
|
|
var installableRuntimeComponents = []string{"Node.js", "Python"}
|
|
|
|
func (s *Server) adminRuntimeEnvironments(w http.ResponseWriter, r *http.Request) {
|
|
items, err := s.projects.RuntimeEnvironments(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusBadGateway, "无法读取宝塔运行环境")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"environments": items})
|
|
}
|
|
|
|
func (s *Server) adminInstallRuntimeComponent(w http.ResponseWriter, r *http.Request) {
|
|
runtimeName := strings.TrimSpace(r.PathValue("runtime"))
|
|
if !slices.Contains(installableRuntimeComponents, runtimeName) {
|
|
writeError(w, http.StatusUnprocessableEntity, "仅支持安装官方免费 Node.js 或 Python 管理组件")
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
if err := s.projects.InstallRuntimeComponent(r.Context(), runtimeName); err != nil {
|
|
s.store.Log(r.Context(), nil, user.ID, "install", "runtime_component", runtimeName, requestClientIP(r), false, err.Error())
|
|
writeError(w, http.StatusBadGateway, "运行环境组件安装失败")
|
|
return
|
|
}
|
|
s.store.Log(r.Context(), nil, user.ID, "install", "runtime_component", runtimeName, requestClientIP(r), true, "安装官方免费 "+runtimeName+" 管理组件")
|
|
writeJSON(w, http.StatusAccepted, map[string]bool{"ok": true})
|
|
}
|
|
|
|
func (s *Server) adminInstallRuntimeEnvironment(w http.ResponseWriter, r *http.Request) {
|
|
runtimeName := strings.TrimSpace(r.PathValue("runtime"))
|
|
if !slices.Contains(installableRuntimes, runtimeName) {
|
|
writeError(w, http.StatusUnprocessableEntity, "不支持安装该运行环境")
|
|
return
|
|
}
|
|
var input struct {
|
|
Version string `json:"version"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil || strings.TrimSpace(input.Version) == "" {
|
|
writeError(w, http.StatusBadRequest, "请选择运行环境版本")
|
|
return
|
|
}
|
|
input.Version = strings.TrimSpace(input.Version)
|
|
user := currentUser(r)
|
|
if err := s.projects.InstallRuntimeEnvironment(r.Context(), runtimeName, input.Version); err != nil {
|
|
s.store.Log(r.Context(), nil, user.ID, "install", "runtime_environment", runtimeName+"/"+input.Version, requestClientIP(r), false, err.Error())
|
|
writeError(w, http.StatusBadGateway, "运行环境安装任务提交失败")
|
|
return
|
|
}
|
|
s.store.Log(r.Context(), nil, user.ID, "install", "runtime_environment", runtimeName+"/"+input.Version, requestClientIP(r), true, "提交 "+runtimeName+" "+input.Version+" 安装任务")
|
|
writeJSON(w, http.StatusAccepted, map[string]bool{"ok": true})
|
|
}
|