feat: download certificate archive

This commit is contained in:
Fu Diwei
2025-01-18 07:07:50 +08:00
parent d28b89f03e
commit d5e4ea385d
16 changed files with 265 additions and 139 deletions

View File

@@ -0,0 +1,42 @@
package handlers
import (
"context"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router"
"github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/rest/resp"
)
type certificateService interface {
ArchiveFile(ctx context.Context, req *domain.CertificateArchiveFileReq) ([]byte, error)
}
type CertificateHandler struct {
service certificateService
}
func NewCertificateHandler(router *router.RouterGroup[*core.RequestEvent], service certificateService) {
handler := &CertificateHandler{
service: service,
}
group := router.Group("/certificates")
group.POST("/{id}/archive", handler.run)
}
func (handler *CertificateHandler) run(e *core.RequestEvent) error {
req := &domain.CertificateArchiveFileReq{}
req.CertificateId = e.Request.PathValue("id")
if err := e.BindBody(req); err != nil {
return resp.Err(e, err)
}
if bt, err := handler.service.ArchiveFile(e.Request.Context(), req); err != nil {
return resp.Err(e, err)
} else {
return resp.Ok(e, bt)
}
}

View File

@@ -24,12 +24,13 @@ func NewWorkflowHandler(router *router.RouterGroup[*core.RequestEvent], service
service: service,
}
group := router.Group("/workflow")
group.POST("/run", handler.run)
group := router.Group("/workflows")
group.POST("/{id}/run", handler.run)
}
func (handler *WorkflowHandler) run(e *core.RequestEvent) error {
req := &domain.WorkflowRunReq{}
req.WorkflowId = e.Request.PathValue("id")
if err := e.BindBody(req); err != nil {
return resp.Err(e, err)
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router"
"github.com/usual2970/certimate/internal/certificate"
"github.com/usual2970/certimate/internal/notify"
"github.com/usual2970/certimate/internal/repository"
"github.com/usual2970/certimate/internal/rest/handlers"
@@ -15,14 +16,15 @@ import (
)
var (
notifySvc *notify.NotifyService
workflowSvc *workflow.WorkflowService
statisticsSvc *statistics.StatisticsService
certificateSvc *certificate.CertificateService
workflowSvc *workflow.WorkflowService
statisticsSvc *statistics.StatisticsService
notifySvc *notify.NotifyService
)
func Register(router *router.Router[*core.RequestEvent]) {
notifyRepo := repository.NewSettingsRepository()
notifySvc = notify.NewNotifyService(notifyRepo)
certificateRepo := repository.NewCertificateRepository()
certificateSvc = certificate.NewCertificateService(certificateRepo)
workflowRepo := repository.NewWorkflowRepository()
workflowSvc = workflow.NewWorkflowService(workflowRepo)
@@ -30,11 +32,15 @@ func Register(router *router.Router[*core.RequestEvent]) {
statisticsRepo := repository.NewStatisticsRepository()
statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
notifyRepo := repository.NewSettingsRepository()
notifySvc = notify.NewNotifyService(notifyRepo)
group := router.Group("/api")
group.Bind(apis.RequireSuperuserAuth())
handlers.NewCertificateHandler(group, certificateSvc)
handlers.NewWorkflowHandler(group, workflowSvc)
handlers.NewNotifyHandler(group, notifySvc)
handlers.NewStatisticsHandler(group, statisticsSvc)
handlers.NewNotifyHandler(group, notifySvc)
}
func Unregister() {