完善证书申请续期与安装初始化流程
This commit is contained in:
1 parent
fd8d8608f5
commit
07636abe44
66 files changed
+9470
-165
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -13,20 +14,28 @@ import (
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/buildinfo"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/firewall"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/logging"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
)
|
||||
|
||||
type Dependencies struct {
|
||||
Config config.Config
|
||||
ConfigPath string
|
||||
Logger *logging.Logger
|
||||
Repository repository.DomainRepository
|
||||
SetupDNS setupDNSVerifier
|
||||
Firewall firewall.Manager
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
configMu sync.RWMutex
|
||||
config config.Config
|
||||
configPath string
|
||||
logger *logging.Logger
|
||||
repository repository.DomainRepository
|
||||
setupDNS setupDNSVerifier
|
||||
firewall firewall.Manager
|
||||
httpServer *http.Server
|
||||
router *gin.Engine
|
||||
}
|
||||
@@ -35,16 +44,22 @@ func New(deps Dependencies) *Server {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
router := gin.New()
|
||||
router.Use(gin.Recovery())
|
||||
server := &Server{config: deps.Config, logger: deps.Logger, repository: deps.Repository, router: router}
|
||||
server := &Server{
|
||||
config: deps.Config, configPath: deps.ConfigPath, logger: deps.Logger,
|
||||
repository: deps.Repository, setupDNS: deps.SetupDNS, firewall: deps.Firewall, router: router,
|
||||
}
|
||||
server.registerRoutes()
|
||||
server.registerSetupRoutes()
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) registerRoutes() {
|
||||
// 主路由保持独立,初始化路由由专用模块注册。
|
||||
s.router.GET("/healthz", s.health)
|
||||
s.router.GET("/api/v1/status", s.status)
|
||||
s.router.GET("/api/v1/domains", s.listDomains)
|
||||
s.router.POST("/api/v1/domains", s.createDomain)
|
||||
s.router.PUT("/api/v1/domains/:id", s.updateDomain)
|
||||
s.router.DELETE("/api/v1/domains/:id", s.deleteDomain)
|
||||
}
|
||||
|
||||
@@ -53,8 +68,11 @@ func (s *Server) Handler() http.Handler {
|
||||
}
|
||||
|
||||
func (s *Server) ListenAndServe() error {
|
||||
s.configMu.RLock()
|
||||
cfg := s.config
|
||||
s.configMu.RUnlock()
|
||||
s.httpServer = &http.Server{
|
||||
Addr: net.JoinHostPort(s.config.Server.Host, strconv.Itoa(s.config.Server.Port)),
|
||||
Addr: net.JoinHostPort(cfg.Server.Host, strconv.Itoa(cfg.Server.Port)),
|
||||
Handler: s.router,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
@@ -62,7 +80,7 @@ func (s *Server) ListenAndServe() error {
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
if s.logger != nil {
|
||||
s.logger.Info("HTTP 服务启动", "address", s.httpServer.Addr, "https_mode", s.config.Server.HTTPSMode)
|
||||
s.logger.Info("HTTP 服务启动", "address", s.httpServer.Addr, "https_mode", cfg.Server.HTTPSMode)
|
||||
}
|
||||
return s.httpServer.ListenAndServe()
|
||||
}
|
||||
@@ -78,10 +96,13 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
type createDomainRequest struct {
|
||||
type domainRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
CertificateType string `json:"certificate_type" binding:"required"`
|
||||
SANs []string `json:"sans"`
|
||||
AutoRenew *bool `json:"auto_renew"`
|
||||
ACMEEnvironment string `json:"acme_environment"`
|
||||
DNSProvider string `json:"dns_provider"`
|
||||
}
|
||||
|
||||
func (s *Server) health(c *gin.Context) {
|
||||
@@ -89,13 +110,16 @@ func (s *Server) health(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *Server) status(c *gin.Context) {
|
||||
s.configMu.RLock()
|
||||
cfg := s.config
|
||||
s.configMu.RUnlock()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"service": "scert",
|
||||
"version": buildinfo.Version,
|
||||
"config_version": s.config.Version,
|
||||
"setup_ready": s.config.SetupReady,
|
||||
"https_mode": s.config.Server.HTTPSMode,
|
||||
"database_driver": s.config.Database.Driver,
|
||||
"config_version": cfg.Version,
|
||||
"setup_ready": cfg.SetupReady,
|
||||
"https_mode": cfg.Server.HTTPSMode,
|
||||
"database_driver": cfg.Database.Driver,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -109,23 +133,78 @@ func (s *Server) listDomains(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *Server) createDomain(c *gin.Context) {
|
||||
var request createDomainRequest
|
||||
var request domainRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "请求参数无效", err)
|
||||
return
|
||||
}
|
||||
item, err := domain.NewDomain(request.Name, domain.CertificateType(request.CertificateType), request.SANs)
|
||||
item, err := newDomainFromRequest(request)
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "域名配置无效", err)
|
||||
return
|
||||
}
|
||||
if err := s.repository.Create(c.Request.Context(), item); err != nil {
|
||||
if repository.IsConflict(err) {
|
||||
s.writeError(c, http.StatusConflict, "域名已经存在", err)
|
||||
return
|
||||
}
|
||||
s.writeError(c, http.StatusInternalServerError, "保存域名失败", err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, item)
|
||||
}
|
||||
|
||||
func (s *Server) updateDomain(c *gin.Context) {
|
||||
var request domainRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "请求参数无效", err)
|
||||
return
|
||||
}
|
||||
existing, err := s.repository.GetByID(c.Request.Context(), c.Param("id"))
|
||||
if err != nil {
|
||||
if repository.IsNotFound(err) {
|
||||
s.writeError(c, http.StatusNotFound, "域名不存在", err)
|
||||
return
|
||||
}
|
||||
s.writeError(c, http.StatusInternalServerError, "读取域名失败", err)
|
||||
return
|
||||
}
|
||||
item, err := newDomainFromRequest(request)
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "域名配置无效", err)
|
||||
return
|
||||
}
|
||||
item.ID = existing.ID
|
||||
item.CreatedAt = existing.CreatedAt
|
||||
item.Status = "pending"
|
||||
item.ExpiresAt = nil
|
||||
if err := s.repository.Update(c.Request.Context(), item); err != nil {
|
||||
if repository.IsNotFound(err) {
|
||||
s.writeError(c, http.StatusNotFound, "域名不存在", err)
|
||||
return
|
||||
}
|
||||
if repository.IsConflict(err) {
|
||||
s.writeError(c, http.StatusConflict, "域名已经存在", err)
|
||||
return
|
||||
}
|
||||
s.writeError(c, http.StatusInternalServerError, "更新域名失败", err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, item)
|
||||
}
|
||||
|
||||
func newDomainFromRequest(request domainRequest) (domain.Domain, error) {
|
||||
autoRenew := true
|
||||
if request.AutoRenew != nil {
|
||||
autoRenew = *request.AutoRenew
|
||||
}
|
||||
acmeEnvironment := request.ACMEEnvironment
|
||||
if acmeEnvironment == "" {
|
||||
acmeEnvironment = domain.ACMEEnvironmentStaging
|
||||
}
|
||||
return domain.NewDomainWithOptions(request.Name, domain.CertificateType(request.CertificateType), request.SANs, autoRenew, acmeEnvironment, request.DNSProvider)
|
||||
}
|
||||
|
||||
func (s *Server) deleteDomain(c *gin.Context) {
|
||||
if err := s.repository.Delete(c.Request.Context(), c.Param("id")); err != nil {
|
||||
if repository.IsNotFound(err) {
|
||||
|
||||
Reference in new issue
Block a user