- 新增 ACME客户端功能,支持域名注册和证书申请 - 添加数据库模型和操作,用于存储和管理域名信息 - 实现 API 接口,提供域名注册、获取和分页查询功能 -集成全局错误处理和 panic捕获 - 添加单元测试和集成测试
		
			
				
	
	
		
			74 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package acme
 | |
| 
 | |
| import (
 | |
| 	"encoding/base64"
 | |
| 	"encoding/json"
 | |
| 	"github.com/go-acme/lego/v4/certificate"
 | |
| )
 | |
| 
 | |
| type ResourceObj struct {
 | |
| 	Domain            string `json:"domain"`
 | |
| 	CertURL           string `json:"certUrl"`
 | |
| 	CertStableURL     string `json:"certStableUrl"`
 | |
| 	PrivateKey        string `json:"privateKey"` // 使用base64编码
 | |
| 	Certificate       string `json:"certificate"`
 | |
| 	IssuerCertificate string `json:"issuerCertificate"`
 | |
| 	CSR               string `json:"csr"`
 | |
| }
 | |
| 
 | |
| func ResourceToJson(resource *certificate.Resource) []byte {
 | |
| 	resourceObj := ResourceObj{
 | |
| 		Domain:            resource.Domain,
 | |
| 		CertURL:           resource.CertURL,
 | |
| 		CertStableURL:     resource.CertStableURL,
 | |
| 		PrivateKey:        base64.StdEncoding.EncodeToString(resource.PrivateKey),
 | |
| 		Certificate:       base64.StdEncoding.EncodeToString(resource.Certificate),
 | |
| 		IssuerCertificate: base64.StdEncoding.EncodeToString(resource.IssuerCertificate),
 | |
| 		CSR:               base64.StdEncoding.EncodeToString(resource.CSR),
 | |
| 	}
 | |
| 	resourceJson, err := json.Marshal(resourceObj)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	return resourceJson
 | |
| }
 | |
| 
 | |
| func ResourceFromJson(data []byte) certificate.Resource {
 | |
| 	var resourceObj ResourceObj
 | |
| 	err := json.Unmarshal(data, &resourceObj)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	privateKey, err := base64.StdEncoding.DecodeString(resourceObj.PrivateKey)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	cert, err := base64.StdEncoding.DecodeString(resourceObj.Certificate)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	issuerCertificate, err := base64.StdEncoding.DecodeString(resourceObj.IssuerCertificate)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	csr, err := base64.StdEncoding.DecodeString(resourceObj.CSR)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	return certificate.Resource{
 | |
| 		Domain:            resourceObj.Domain,
 | |
| 		CertURL:           resourceObj.CertURL,
 | |
| 		CertStableURL:     resourceObj.CertStableURL,
 | |
| 		PrivateKey:        privateKey,
 | |
| 		Certificate:       cert,
 | |
| 		IssuerCertificate: issuerCertificate,
 | |
| 		CSR:               csr,
 | |
| 	}
 | |
| 
 | |
| }
 |