mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 05:31:44 +08:00
* Initial plan * feat: 添加操作日志记录功能和前端日志查看页面 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 修复 HttpLog 中 IP 字符串处理的类型安全问题 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * feat: 完善面板内日志记录,添加网站/数据库/项目/计划任务/备份CRUD日志 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 完善更多模块的日志记录(证书/SSH/Webhook/用户/设置/安全) Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 改进cert_dns删除方法,在删除前获取实体信息用于日志记录 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * feat: 从session获取操作员ID并在前端显示用户名 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 <haozi@loli.email>
140 lines
3.0 KiB
Go
140 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/libtnb/chix"
|
|
|
|
"github.com/acepanel/panel/internal/biz"
|
|
"github.com/acepanel/panel/internal/http/request"
|
|
)
|
|
|
|
type WebHookService struct {
|
|
webhookRepo biz.WebHookRepo
|
|
}
|
|
|
|
func NewWebHookService(webhook biz.WebHookRepo) *WebHookService {
|
|
return &WebHookService{
|
|
webhookRepo: webhook,
|
|
}
|
|
}
|
|
|
|
func (s *WebHookService) List(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.Paginate](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
webhooks, total, err := s.webhookRepo.List(req.Page, req.Limit)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, chix.M{
|
|
"total": total,
|
|
"items": webhooks,
|
|
})
|
|
}
|
|
|
|
func (s *WebHookService) Get(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.ID](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
webhook, err := s.webhookRepo.Get(req.ID)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, webhook)
|
|
}
|
|
|
|
func (s *WebHookService) Create(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.WebHookCreate](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
webhook, err := s.webhookRepo.Create(r.Context(), req)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, webhook)
|
|
}
|
|
|
|
func (s *WebHookService) Update(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.WebHookUpdate](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = s.webhookRepo.Update(r.Context(), req); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
func (s *WebHookService) Delete(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.ID](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = s.webhookRepo.Delete(r.Context(), req.ID); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
// Call 处理 webhook 调用请求
|
|
func (s *WebHookService) Call(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.WebHookKey](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
// 获取 webhook 信息以判断返回格式
|
|
webhook, err := s.webhookRepo.GetByKey(req.Key)
|
|
if err != nil {
|
|
Error(w, http.StatusNotFound, "webhook not found")
|
|
return
|
|
}
|
|
|
|
output, err := s.webhookRepo.Call(req.Key)
|
|
if err != nil {
|
|
if webhook.Raw {
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
_, _ = w.Write([]byte(output))
|
|
return
|
|
}
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
if webhook.Raw {
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
_, _ = w.Write([]byte(output))
|
|
return
|
|
}
|
|
|
|
Success(w, chix.M{
|
|
"output": output,
|
|
})
|
|
}
|