mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 00:38:33 +02:00
refactor(autocert): extract types to dedicated package and switch to context-based provider access
Move CertInfo struct from provider.go to internal/autocert/types/cert_info.go and replace global ActiveProvider.Load() with context-based autocertctx.FromCtx() pattern in API handlers. This improves separation of concerns and eliminates global state dependency in request handling.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/yusing/godoxy/internal/autocert"
|
"github.com/yusing/godoxy/internal/autocert"
|
||||||
|
autocertctx "github.com/yusing/godoxy/internal/autocert/types"
|
||||||
apitypes "github.com/yusing/goutils/apitypes"
|
apitypes "github.com/yusing/goutils/apitypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,7 +22,7 @@ import (
|
|||||||
// @Failure 500 {object} apitypes.ErrorResponse "Internal server error"
|
// @Failure 500 {object} apitypes.ErrorResponse "Internal server error"
|
||||||
// @Router /cert/info [get]
|
// @Router /cert/info [get]
|
||||||
func Info(c *gin.Context) {
|
func Info(c *gin.Context) {
|
||||||
provider := autocert.ActiveProvider.Load()
|
provider := autocertctx.FromCtx(c.Request.Context())
|
||||||
if provider == nil {
|
if provider == nil {
|
||||||
c.JSON(http.StatusNotFound, apitypes.Error("autocert is not enabled"))
|
c.JSON(http.StatusNotFound, apitypes.Error("autocert is not enabled"))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/godoxy/internal/autocert"
|
autocertctx "github.com/yusing/godoxy/internal/autocert/types"
|
||||||
"github.com/yusing/godoxy/internal/logging/memlogger"
|
"github.com/yusing/godoxy/internal/logging/memlogger"
|
||||||
apitypes "github.com/yusing/goutils/apitypes"
|
apitypes "github.com/yusing/goutils/apitypes"
|
||||||
"github.com/yusing/goutils/http/websocket"
|
"github.com/yusing/goutils/http/websocket"
|
||||||
@@ -23,8 +23,8 @@ import (
|
|||||||
// @Failure 500 {object} apitypes.ErrorResponse
|
// @Failure 500 {object} apitypes.ErrorResponse
|
||||||
// @Router /cert/renew [get]
|
// @Router /cert/renew [get]
|
||||||
func Renew(c *gin.Context) {
|
func Renew(c *gin.Context) {
|
||||||
autocert := autocert.ActiveProvider.Load()
|
provider := autocertctx.FromCtx(c.Request.Context())
|
||||||
if autocert == nil {
|
if provider == nil {
|
||||||
c.JSON(http.StatusNotFound, apitypes.Error("autocert is not enabled"))
|
c.JSON(http.StatusNotFound, apitypes.Error("autocert is not enabled"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ func Renew(c *gin.Context) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// renewal happens in background
|
// renewal happens in background
|
||||||
ok := autocert.ForceExpiryAll()
|
ok := provider.ForceExpiryAll()
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error().Msg("cert renewal already in progress")
|
log.Error().Msg("cert renewal already in progress")
|
||||||
time.Sleep(1 * time.Second) // wait for the log above to be sent
|
time.Sleep(1 * time.Second) // wait for the log above to be sent
|
||||||
@@ -67,5 +67,5 @@ func Renew(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
log.Info().Msg("cert force renewal requested")
|
log.Info().Msg("cert force renewal requested")
|
||||||
|
|
||||||
autocert.WaitRenewalDone(manager.Context())
|
provider.WaitRenewalDone(manager.Context())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
autocert "github.com/yusing/godoxy/internal/autocert/types"
|
||||||
"github.com/yusing/godoxy/internal/common"
|
"github.com/yusing/godoxy/internal/common"
|
||||||
"github.com/yusing/godoxy/internal/notif"
|
"github.com/yusing/godoxy/internal/notif"
|
||||||
gperr "github.com/yusing/goutils/errs"
|
gperr "github.com/yusing/goutils/errs"
|
||||||
@@ -56,15 +57,6 @@ type (
|
|||||||
|
|
||||||
CertExpiries map[string]time.Time
|
CertExpiries map[string]time.Time
|
||||||
|
|
||||||
CertInfo struct {
|
|
||||||
Subject string `json:"subject"`
|
|
||||||
Issuer string `json:"issuer"`
|
|
||||||
NotBefore int64 `json:"not_before"`
|
|
||||||
NotAfter int64 `json:"not_after"`
|
|
||||||
DNSNames []string `json:"dns_names"`
|
|
||||||
EmailAddresses []string `json:"email_addresses"`
|
|
||||||
} // @name CertInfo
|
|
||||||
|
|
||||||
RenewMode uint8
|
RenewMode uint8
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -82,9 +74,6 @@ const (
|
|||||||
renewModeIfNeeded
|
renewModeIfNeeded
|
||||||
)
|
)
|
||||||
|
|
||||||
// could be nil
|
|
||||||
var ActiveProvider atomic.Pointer[Provider]
|
|
||||||
|
|
||||||
func NewProvider(cfg *Config, user *User, legoCfg *lego.Config) (*Provider, error) {
|
func NewProvider(cfg *Config, user *User, legoCfg *lego.Config) (*Provider, error) {
|
||||||
p := &Provider{
|
p := &Provider{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
@@ -119,14 +108,14 @@ func (p *Provider) GetCert(hello *tls.ClientHelloInfo) (*tls.Certificate, error)
|
|||||||
return p.tlsCert, nil
|
return p.tlsCert, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) GetCertInfos() ([]CertInfo, error) {
|
func (p *Provider) GetCertInfos() ([]autocert.CertInfo, error) {
|
||||||
allProviders := p.allProviders()
|
allProviders := p.allProviders()
|
||||||
certInfos := make([]CertInfo, 0, len(allProviders))
|
certInfos := make([]autocert.CertInfo, 0, len(allProviders))
|
||||||
for _, provider := range allProviders {
|
for _, provider := range allProviders {
|
||||||
if provider.tlsCert == nil {
|
if provider.tlsCert == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
certInfos = append(certInfos, CertInfo{
|
certInfos = append(certInfos, autocert.CertInfo{
|
||||||
Subject: provider.tlsCert.Leaf.Subject.CommonName,
|
Subject: provider.tlsCert.Leaf.Subject.CommonName,
|
||||||
Issuer: provider.tlsCert.Leaf.Issuer.CommonName,
|
Issuer: provider.tlsCert.Leaf.Issuer.CommonName,
|
||||||
NotBefore: provider.tlsCert.Leaf.NotBefore.Unix(),
|
NotBefore: provider.tlsCert.Leaf.NotBefore.Unix(),
|
||||||
|
|||||||
10
internal/autocert/types/cert_info.go
Normal file
10
internal/autocert/types/cert_info.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package autocert
|
||||||
|
|
||||||
|
type CertInfo struct {
|
||||||
|
Subject string `json:"subject"`
|
||||||
|
Issuer string `json:"issuer"`
|
||||||
|
NotBefore int64 `json:"not_before"`
|
||||||
|
NotAfter int64 `json:"not_after"`
|
||||||
|
DNSNames []string `json:"dns_names"`
|
||||||
|
EmailAddresses []string `json:"email_addresses"`
|
||||||
|
} // @name CertInfo
|
||||||
@@ -1,13 +1,17 @@
|
|||||||
package autocert
|
package autocert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
|
||||||
"github.com/yusing/goutils/task"
|
"github.com/yusing/goutils/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Provider interface {
|
type Provider interface {
|
||||||
GetCert(*tls.ClientHelloInfo) (*tls.Certificate, error)
|
GetCert(hello *tls.ClientHelloInfo) (*tls.Certificate, error)
|
||||||
ScheduleRenewalAll(task.Parent)
|
GetCertInfos() ([]CertInfo, error)
|
||||||
|
ScheduleRenewalAll(parent task.Parent)
|
||||||
ObtainCertAll() error
|
ObtainCertAll() error
|
||||||
|
ForceExpiryAll() bool
|
||||||
|
WaitRenewalDone(ctx context.Context) bool
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user