mirror of
https://github.com/usual2970/certimate.git
synced 2025-10-05 05:54:53 +00:00
Certificate displaying and monitoring
This commit is contained in:
11
internal/domain/statistics.go
Normal file
11
internal/domain/statistics.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package domain
|
||||
|
||||
type Statistics struct {
|
||||
CertificateTotal int `json:"certificateTotal"`
|
||||
CertificateExpireSoon int `json:"certificateExpireSoon"`
|
||||
CertificateExpired int `json:"certificateExpired"`
|
||||
|
||||
WorkflowTotal int `json:"workflowTotal"`
|
||||
WorkflowEnabled int `json:"workflowEnabled"`
|
||||
WorkflowDisabled int `json:"workflowDisabled"`
|
||||
}
|
@@ -19,8 +19,8 @@ const (
|
||||
|
||||
func PushExpireMsg() {
|
||||
// 查询即将过期的证书
|
||||
records, err := app.GetApp().Dao().FindRecordsByFilter("domains", "expiredAt<{:time}&&certUrl!=''", "-created", 500, 0,
|
||||
dbx.Params{"time": xtime.GetTimeAfter(24 * time.Hour * 15)})
|
||||
records, err := app.GetApp().Dao().FindRecordsByFilter("certificate", "expireAt<{:time}&&certUrl!=''", "-created", 500, 0,
|
||||
dbx.Params{"time": xtime.GetTimeAfter(24 * time.Hour * 20)})
|
||||
if err != nil {
|
||||
app.GetApp().Logger().Error("find expired domains by filter", "error", err)
|
||||
return
|
||||
@@ -76,7 +76,7 @@ func buildMsg(records []*models.Record) *notifyMessage {
|
||||
domains := make([]string, count)
|
||||
|
||||
for i, record := range records {
|
||||
domains[i] = record.GetString("domain")
|
||||
domains[i] = record.GetString("san")
|
||||
}
|
||||
|
||||
countStr := strconv.Itoa(count)
|
||||
|
63
internal/repository/statistics.go
Normal file
63
internal/repository/statistics.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/utils/app"
|
||||
)
|
||||
|
||||
type StatisticsRepository struct{}
|
||||
|
||||
func NewStatisticsRepository() *StatisticsRepository {
|
||||
return &StatisticsRepository{}
|
||||
}
|
||||
|
||||
type totalResp struct {
|
||||
Total int `json:"total" db:"total"`
|
||||
}
|
||||
|
||||
func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, error) {
|
||||
rs := &domain.Statistics{}
|
||||
// 所有证书
|
||||
certTotal := totalResp{}
|
||||
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from certificate").One(&certTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.CertificateTotal = certTotal.Total
|
||||
|
||||
// 即将过期证书
|
||||
certExpireSoonTotal := totalResp{}
|
||||
if err := app.GetApp().Dao().DB().
|
||||
NewQuery("select count(*) as total from certificate where expireAt > datetime('now') and expireAt < datetime('now', '+20 days')").
|
||||
One(&certExpireSoonTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.CertificateExpireSoon = certExpireSoonTotal.Total
|
||||
|
||||
// 已过期证书
|
||||
certExpiredTotal := totalResp{}
|
||||
if err := app.GetApp().Dao().DB().
|
||||
NewQuery("select count(*) as total from certificate where expireAt < datetime('now')").
|
||||
One(&certExpiredTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.CertificateExpired = certExpiredTotal.Total
|
||||
|
||||
// 所有工作流
|
||||
workflowTotal := totalResp{}
|
||||
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow").One(&workflowTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.WorkflowTotal = workflowTotal.Total
|
||||
|
||||
// 已启用工作流
|
||||
workflowEnabledTotal := totalResp{}
|
||||
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow where enabled is TRUE").One(&workflowEnabledTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.WorkflowEnabled = workflowEnabledTotal.Total
|
||||
rs.WorkflowDisabled = workflowTotal.Total - workflowEnabledTotal.Total
|
||||
|
||||
return rs, nil
|
||||
}
|
35
internal/rest/statistics.go
Normal file
35
internal/rest/statistics.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/utils/resp"
|
||||
)
|
||||
|
||||
type StatisticsService interface {
|
||||
Get(ctx context.Context) (*domain.Statistics, error)
|
||||
}
|
||||
|
||||
type statisticsHandler struct {
|
||||
service StatisticsService
|
||||
}
|
||||
|
||||
func NewStatisticsHandler(route *echo.Group, service StatisticsService) {
|
||||
handler := &statisticsHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := route.Group("/statistics")
|
||||
|
||||
group.GET("/get", handler.get)
|
||||
}
|
||||
|
||||
func (handler *statisticsHandler) get(c echo.Context) error {
|
||||
if statistics, err := handler.service.Get(c.Request().Context()); err != nil {
|
||||
return resp.Err(c, err)
|
||||
} else {
|
||||
return resp.Succ(c, statistics)
|
||||
}
|
||||
}
|
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/usual2970/certimate/internal/notify"
|
||||
"github.com/usual2970/certimate/internal/repository"
|
||||
"github.com/usual2970/certimate/internal/rest"
|
||||
"github.com/usual2970/certimate/internal/statistics"
|
||||
"github.com/usual2970/certimate/internal/workflow"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
@@ -19,7 +20,12 @@ func Register(e *echo.Echo) {
|
||||
workflowRepo := repository.NewWorkflowRepository()
|
||||
workflowSvc := workflow.NewWorkflowService(workflowRepo)
|
||||
|
||||
statisticsRepo := repository.NewStatisticsRepository()
|
||||
statisticsSvc := statistics.NewStatisticsService(statisticsRepo)
|
||||
|
||||
rest.NewWorkflowHandler(group, workflowSvc)
|
||||
|
||||
rest.NewNotifyHandler(group, notifySvc)
|
||||
|
||||
rest.NewStatisticsHandler(group, statisticsSvc)
|
||||
}
|
||||
|
25
internal/statistics/service.go
Normal file
25
internal/statistics/service.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
)
|
||||
|
||||
type StatisticsRepository interface {
|
||||
Get(ctx context.Context) (*domain.Statistics, error)
|
||||
}
|
||||
|
||||
type StatisticsService struct {
|
||||
repo StatisticsRepository
|
||||
}
|
||||
|
||||
func NewStatisticsService(repo StatisticsRepository) *StatisticsService {
|
||||
return &StatisticsService{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StatisticsService) Get(ctx context.Context) (*domain.Statistics, error) {
|
||||
return s.repo.Get(ctx)
|
||||
}
|
Reference in New Issue
Block a user