2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:40:59 +08:00
Files
panel/internal/service/environment_go.go

91 lines
2.5 KiB
Go

package service
import (
"fmt"
"net/http"
"strings"
"github.com/leonelquinteros/gotext"
"github.com/acepanel/panel/internal/app"
"github.com/acepanel/panel/internal/biz"
"github.com/acepanel/panel/internal/http/request"
"github.com/acepanel/panel/pkg/shell"
)
type EnvironmentGoService struct {
t *gotext.Locale
environmentRepo biz.EnvironmentRepo
}
func NewEnvironmentGoService(t *gotext.Locale, environmentRepo biz.EnvironmentRepo) *EnvironmentGoService {
return &EnvironmentGoService{
t: t,
environmentRepo: environmentRepo,
}
}
func (s *EnvironmentGoService) SetCli(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.EnvironmentSlug](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
if !s.environmentRepo.IsInstalled("go", req.Slug) {
Error(w, http.StatusUnprocessableEntity, s.t.Get("Go-%s is not installed", req.Slug))
return
}
binPath := fmt.Sprintf("%s/server/go/%s/bin", app.Root, req.Slug)
if _, err = shell.Execf("ln -sf %s/go /usr/local/bin/go", binPath); err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}
if _, err = shell.Execf("ln -sf %s/gofmt /usr/local/bin/gofmt", binPath); err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}
Success(w, nil)
}
func (s *EnvironmentGoService) GetProxy(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.EnvironmentSlug](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
if !s.environmentRepo.IsInstalled("go", req.Slug) {
Error(w, http.StatusUnprocessableEntity, s.t.Get("Go-%s is not installed", req.Slug))
return
}
goBin := fmt.Sprintf("%s/server/go/%s/bin/go", app.Root, req.Slug)
proxy, err := shell.Execf("%s env GOPROXY", goBin)
if err != nil {
proxy = "https://proxy.golang.org,direct"
}
Success(w, strings.TrimSpace(proxy))
}
func (s *EnvironmentGoService) SetProxy(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.EnvironmentProxy](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
if !s.environmentRepo.IsInstalled("go", req.Slug) {
Error(w, http.StatusUnprocessableEntity, s.t.Get("Go-%s is not installed", req.Slug))
return
}
goBin := fmt.Sprintf("%s/server/go/%s/bin/go", app.Root, req.Slug)
if _, err = shell.Execf("%s env -w GOPROXY=%s", goBin, req.Proxy); err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}
Success(w, nil)
}