mirror of
https://github.com/certimate-go/certimate.git
synced 2026-08-31 02:18:34 +08:00
refactor: clean code
This commit is contained in:
@@ -44,7 +44,7 @@ func (s *CertificateService) InitSchedule(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *CertificateService) DownloadArchivedFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) (*dtos.CertificateArchiveFileResp, error) {
|
||||
func (s *CertificateService) DownloadCertificate(ctx context.Context, req *dtos.CertificateDownloadReq) (*dtos.CertificateDownloadResp, error) {
|
||||
certificate, err := s.certificateRepo.GetById(ctx, req.CertificateId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -192,7 +192,7 @@ func (s *CertificateService) DownloadArchivedFile(ctx context.Context, req *dtos
|
||||
return nil, domain.ErrInvalidParams
|
||||
}
|
||||
|
||||
resp := &dtos.CertificateArchiveFileResp{
|
||||
resp := &dtos.CertificateDownloadResp{
|
||||
FileFormat: "zip",
|
||||
FileBytes: bytes,
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package dtos
|
||||
|
||||
type CertificateArchiveFileReq struct {
|
||||
type CertificateDownloadReq struct {
|
||||
CertificateId string `json:"-"`
|
||||
CertificateFormat string `json:"format"`
|
||||
}
|
||||
|
||||
type CertificateArchiveFileResp struct {
|
||||
type CertificateDownloadResp struct {
|
||||
FileBytes []byte `json:"fileBytes"`
|
||||
FileFormat string `json:"fileFormat"`
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type certificateService interface {
|
||||
DownloadArchivedFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) (*dtos.CertificateArchiveFileResp, error)
|
||||
DownloadCertificate(ctx context.Context, req *dtos.CertificateDownloadReq) (*dtos.CertificateDownloadResp, error)
|
||||
RevokeCertificate(ctx context.Context, req *dtos.CertificateRevokeReq) (*dtos.CertificateRevokeResp, error)
|
||||
}
|
||||
|
||||
@@ -25,18 +25,18 @@ func NewCertificatesHandler(router *router.RouterGroup[*core.RequestEvent], serv
|
||||
}
|
||||
|
||||
group := router.Group("/certificates")
|
||||
group.POST("/{certificateId}/archive", handler.archiveCertificate)
|
||||
group.POST("/{certificateId}/download", handler.downloadCertificate)
|
||||
group.POST("/{certificateId}/revoke", handler.revokeCertificate)
|
||||
}
|
||||
|
||||
func (handler *CertificatesHandler) archiveCertificate(e *core.RequestEvent) error {
|
||||
req := &dtos.CertificateArchiveFileReq{}
|
||||
func (handler *CertificatesHandler) downloadCertificate(e *core.RequestEvent) error {
|
||||
req := &dtos.CertificateDownloadReq{}
|
||||
req.CertificateId = e.Request.PathValue("certificateId")
|
||||
if err := e.BindBody(req); err != nil {
|
||||
return resp.Err(e, err)
|
||||
}
|
||||
|
||||
res, err := handler.service.DownloadArchivedFile(e.Request.Context(), req)
|
||||
res, err := handler.service.DownloadCertificate(e.Request.Context(), req)
|
||||
if err != nil {
|
||||
return resp.Err(e, err)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func NewStatisticsHandler(router *router.RouterGroup[*core.RequestEvent], servic
|
||||
}
|
||||
|
||||
group := router.Group("/statistics")
|
||||
group.GET("/get", handler.get)
|
||||
group.GET("/", handler.get)
|
||||
}
|
||||
|
||||
func (handler *StatisticsHandler) get(e *core.RequestEvent) error {
|
||||
|
||||
@@ -20,7 +20,7 @@ var (
|
||||
notifySvc *notify.NotifyService
|
||||
)
|
||||
|
||||
func SetupRouter(router *router.Router[*core.RequestEvent]) {
|
||||
func BindRouter(router *router.Router[*core.RequestEvent]) {
|
||||
accessRepo := repository.NewAccessRepository()
|
||||
workflowRepo := repository.NewWorkflowRepository()
|
||||
workflowRunRepo := repository.NewWorkflowRunRepository()
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := app.GetApp().(*pocketbase.PocketBase)
|
||||
pb := app.GetApp().(*pocketbase.PocketBase)
|
||||
if len(os.Args) < 2 {
|
||||
slog.Error("[CERTIMATE] missing exec args, maybe you forget the 'serve' command?")
|
||||
os.Exit(1)
|
||||
@@ -37,23 +37,23 @@ func main() {
|
||||
pflag.StringVar(&flagHttp, "http", "127.0.0.1:8090", "HTTP server address")
|
||||
pflag.Parse()
|
||||
|
||||
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
|
||||
migratecmd.MustRegister(pb, pb.RootCmd, migratecmd.Config{
|
||||
// enable auto creation of migration files when making collection changes in the Admin UI
|
||||
// (the isGoRun check is to enable it only during development)
|
||||
Automigrate: strings.HasPrefix(os.Args[0], os.TempDir()),
|
||||
})
|
||||
|
||||
app.RootCmd.AddCommand(cmd.NewInternalCommand(app))
|
||||
app.RootCmd.AddCommand(cmd.NewWinscCommand(app))
|
||||
pb.RootCmd.AddCommand(cmd.NewInternalCommand(pb))
|
||||
pb.RootCmd.AddCommand(cmd.NewWinscCommand(pb))
|
||||
|
||||
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
|
||||
pb.OnServe().BindFunc(func(e *core.ServeEvent) error {
|
||||
scheduler.Setup()
|
||||
workflow.Setup()
|
||||
routes.SetupRouter(e.Router)
|
||||
routes.BindRouter(e.Router)
|
||||
return e.Next()
|
||||
})
|
||||
|
||||
app.OnServe().Bind(&hook.Handler[*core.ServeEvent]{
|
||||
pb.OnServe().Bind(&hook.Handler[*core.ServeEvent]{
|
||||
Func: func(e *core.ServeEvent) error {
|
||||
e.Router.
|
||||
GET("/{path...}", apis.Static(ui.DistDirFS, false)).
|
||||
@@ -63,17 +63,17 @@ func main() {
|
||||
Priority: 999,
|
||||
})
|
||||
|
||||
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
|
||||
pb.OnServe().BindFunc(func(e *core.ServeEvent) error {
|
||||
slog.Info("[CERTIMATE] Visit the website: http://" + flagHttp)
|
||||
return e.Next()
|
||||
})
|
||||
|
||||
app.OnTerminate().BindFunc(func(e *core.TerminateEvent) error {
|
||||
pb.OnTerminate().BindFunc(func(e *core.TerminateEvent) error {
|
||||
workflow.Teardown()
|
||||
return e.Next()
|
||||
})
|
||||
|
||||
if err := cmd.Serve(app); err != nil {
|
||||
if err := cmd.Serve(pb); err != nil {
|
||||
slog.Error("[CERTIMATE] Start failed.", slog.Any("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ import { ClientResponseError } from "pocketbase";
|
||||
import { type CertificateFormatType } from "@/domain/certificate";
|
||||
import { getPocketBase } from "@/repository/_pocketbase";
|
||||
|
||||
export const archive = async (certificateId: string, format?: CertificateFormatType) => {
|
||||
export const download = async (certificateId: string, format?: CertificateFormatType) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
type RespData = {
|
||||
fileBytes: string;
|
||||
};
|
||||
const resp = await pb.send<BaseResponse<RespData>>(`/api/certificates/${encodeURIComponent(certificateId)}/archive`, {
|
||||
const resp = await pb.send<BaseResponse<RespData>>(`/api/certificates/${encodeURIComponent(certificateId)}/download`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
||||
@@ -6,7 +6,7 @@ import { getPocketBase } from "@/repository/_pocketbase";
|
||||
export const get = async () => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const resp = await pb.send<BaseResponse<Statistics>>("/api/statistics/get", {
|
||||
const resp = await pb.send<BaseResponse<Statistics>>("/api/statistics", {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { App, Button, Dropdown, Form, Input, Tag, Tooltip } from "antd";
|
||||
import dayjs from "dayjs";
|
||||
import { saveAs } from "file-saver";
|
||||
|
||||
import { archive as archiveCertificate } from "@/api/certificates";
|
||||
import { download as downloadCertificate } from "@/api/certificates";
|
||||
import { CERTIFICATE_FORMATS, type CertificateFormatType, type CertificateModel } from "@/domain/certificate";
|
||||
|
||||
export interface CertificateDetailProps {
|
||||
@@ -21,7 +21,7 @@ const CertificateDetail = ({ data, ...props }: CertificateDetailProps) => {
|
||||
|
||||
const handleDownloadClick = async (format: CertificateFormatType) => {
|
||||
try {
|
||||
const res = await archiveCertificate(data.id, format);
|
||||
const res = await downloadCertificate(data.id, format);
|
||||
const bstr = atob(res.data.fileBytes);
|
||||
const u8arr = Uint8Array.from(bstr, (ch) => ch.charCodeAt(0));
|
||||
const blob = new Blob([u8arr], { type: "application/zip" });
|
||||
|
||||
Reference in New Issue
Block a user