feat: AutoSSL certificate management tool with Web UI
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Certificate struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
|
||||
Domain string `json:"domain" gorm:"uniqueIndex;size:255"`
|
||||
Email string `json:"email" gorm:"size:255"`
|
||||
Provider string `json:"provider" gorm:"size:50;default:letsencrypt"` // letsencrypt, zerossl
|
||||
ChallengeType string `json:"challenge_type" gorm:"size:20;default:http"` // http, dns
|
||||
DNSProvider string `json:"dns_provider,omitempty" gorm:"size:50"` // alidns, cloudflare, etc.
|
||||
DNSConfig string `json:"dns_config,omitempty" gorm:"type:text"` // JSON config for DNS provider
|
||||
|
||||
Status string `json:"status" gorm:"size:20;default:pending"` // pending, active, expired, error
|
||||
CertURL string `json:"cert_url,omitempty" gorm:"size:512"`
|
||||
ExpiresAt *time.Time `json:"expires_at,omitempty"`
|
||||
LastRenewedAt *time.Time `json:"last_renewed_at,omitempty"`
|
||||
ErrorMessage string `json:"error_message,omitempty" gorm:"type:text"`
|
||||
|
||||
// Auto renew settings
|
||||
AutoRenew bool `json:"auto_renew" gorm:"default:true"`
|
||||
RenewDays int `json:"renew_days" gorm:"default:30"` // Renew when expires within this many days
|
||||
|
||||
// ACME account key
|
||||
AccountKeyID uint `json:"account_key_id,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
type Config struct {
|
||||
Port string
|
||||
DBPath string
|
||||
CertDir string
|
||||
AccountsDir string
|
||||
}
|
||||
|
||||
func Load() *Config {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
dbPath := os.Getenv("DB_PATH")
|
||||
if dbPath == "" {
|
||||
dbPath = "./data/autossl.db"
|
||||
}
|
||||
certDir := os.Getenv("CERT_DIR")
|
||||
if certDir == "" {
|
||||
certDir = "./data/certs"
|
||||
}
|
||||
accountsDir := os.Getenv("ACCOUNTS_DIR")
|
||||
if accountsDir == "" {
|
||||
accountsDir = "./data/accounts"
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Port: port,
|
||||
DBPath: dbPath,
|
||||
CertDir: certDir,
|
||||
AccountsDir: accountsDir,
|
||||
}
|
||||
}
|
||||
|
||||
func InitDB(cfg *Config) {
|
||||
// Ensure data directories exist
|
||||
dirs := []string{"./data", cfg.CertDir, cfg.AccountsDir}
|
||||
for _, d := range dirs {
|
||||
if err := os.MkdirAll(d, 0700); err != nil {
|
||||
log.Fatalf("Failed to create directory %s: %v", d, err)
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
DB, err = gorm.Open(sqlite.Open(cfg.DBPath), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Warn),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect database: %v", err)
|
||||
}
|
||||
|
||||
// Auto migrate
|
||||
if err := DB.AutoMigrate(&Certificate{}); err != nil {
|
||||
log.Fatalf("Failed to migrate database: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Database initialized successfully")
|
||||
}
|
||||
Reference in New Issue
Block a user