refactor: clean code

This commit is contained in:
Fu Diwei
2024-12-27 19:35:50 +08:00
parent 047479426a
commit 86133ba52b
41 changed files with 396 additions and 544 deletions

View File

@@ -4,109 +4,118 @@ import "time"
type Access struct {
Meta
Name string `json:"name"`
Config string `json:"config"`
ConfigType string `json:"configType"`
Deleted time.Time `json:"deleted"`
Usage string `json:"usage"`
Name string `json:"name" db:"name"`
ConfigType string `json:"configType" db:"configType"`
Config string `json:"config" db:"config"`
Usage string `json:"usage" db:"usage"`
DeletedAt time.Time `json:"deleted" db:"deleted"`
}
// 兼容一下原 pocketbase 的 record
func (a *Access) GetString(key string) string {
switch key {
case "name":
return a.Name
default:
return ""
}
}
type AccessProviderType string
type AliyunAccess struct {
AccessKeyId string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
}
/*
提供商类型常量值。
type ByteplusAccess struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
注意:如果追加新的常量值,请保持以 ASCII 排序。
NOTICE: If you add new constant, please keep ASCII order.
*/
const (
ACCESS_PROVIDER_ACMEHTTPREQ = AccessProviderType("acmehttpreq")
ACCESS_PROVIDER_ALIYUN = AccessProviderType("aliyun")
ACCESS_PROVIDER_AWS = AccessProviderType("aws")
ACCESS_PROVIDER_BAIDUCLOUD = AccessProviderType("baiducloud")
ACCESS_PROVIDER_BYTEPLUS = AccessProviderType("byteplus")
ACCESS_PROVIDER_CLOUDFLARE = AccessProviderType("cloudflare")
ACCESS_PROVIDER_DOGECLOUD = AccessProviderType("dogecloud")
ACCESS_PROVIDER_GODADDY = AccessProviderType("godaddy")
ACCESS_PROVIDER_HUAWEICLOUD = AccessProviderType("huaweicloud")
ACCESS_PROVIDER_KUBERNETES = AccessProviderType("k8s")
ACCESS_PROVIDER_LOCAL = AccessProviderType("local")
ACCESS_PROVIDER_NAMEDOTCOM = AccessProviderType("namedotcom")
ACCESS_PROVIDER_NAMESILO = AccessProviderType("namesilo")
ACCESS_PROVIDER_POWERDNS = AccessProviderType("powerdns")
ACCESS_PROVIDER_QINIU = AccessProviderType("qiniu")
ACCESS_PROVIDER_SSH = AccessProviderType("ssh")
ACCESS_PROVIDER_TENCENTCLOUD = AccessProviderType("tencentcloud")
ACCESS_PROVIDER_VOLCENGINE = AccessProviderType("volcengine")
ACCESS_PROVIDER_WEBHOOK = AccessProviderType("webhook")
)
type TencentAccess struct {
SecretId string `json:"secretId"`
SecretKey string `json:"secretKey"`
}
type HuaweiCloudAccess struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Region string `json:"region"`
}
type BaiduCloudAccess struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
}
type AwsAccess struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Region string `json:"region"`
HostedZoneId string `json:"hostedZoneId"`
}
type CloudflareAccess struct {
DnsApiToken string `json:"dnsApiToken"`
}
type QiniuAccess struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
type DogeCloudAccess struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
type NameSiloAccess struct {
ApiKey string `json:"apiKey"`
}
type GodaddyAccess struct {
ApiKey string `json:"apiKey"`
ApiSecret string `json:"apiSecret"`
}
type NameDotComAccess struct {
Username string `json:"username"`
ApiToken string `json:"apiToken"`
}
type PdnsAccess struct {
ApiUrl string `json:"apiUrl"`
ApiKey string `json:"apiKey"`
}
type VolcEngineAccess struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
// Deprecated: Use [AccessKey] and [SecretKey] instead in the future
AccessKeyId string `json:"accessKeyId"`
// Deprecated: Use [AccessKey] and [SecretKey] instead in the future
SecretAccessKey string `json:"secretAccessKey"`
}
type HttpreqAccess struct {
type ACMEHttpReqAccessConfig struct {
Endpoint string `json:"endpoint"`
Mode string `json:"mode"`
Username string `json:"username"`
Password string `json:"password"`
}
type LocalAccess struct{}
type AliyunAccessConfig struct {
AccessKeyId string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
}
type SSHAccess struct {
type AWSAccessConfig struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Region string `json:"region"`
HostedZoneId string `json:"hostedZoneId"`
}
type BaiduCloudAccessConfig struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
}
type BytePlusAccessConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
type CloudflareAccessConfig struct {
DnsApiToken string `json:"dnsApiToken"`
}
type DogeCloudAccessConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
type GoDaddyAccessConfig struct {
ApiKey string `json:"apiKey"`
ApiSecret string `json:"apiSecret"`
}
type HuaweiCloudAccessConfig struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Region string `json:"region"`
}
type LocalAccessConfig struct{}
type KubernetesAccessConfig struct {
KubeConfig string `json:"kubeConfig"`
}
type NameDotComAccessConfig struct {
Username string `json:"username"`
ApiToken string `json:"apiToken"`
}
type NameSiloAccessConfig struct {
ApiKey string `json:"apiKey"`
}
type PowerDNSAccessConfig struct {
ApiUrl string `json:"apiUrl"`
ApiKey string `json:"apiKey"`
}
type QiniuAccessConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
type SSHAccessConfig struct {
Host string `json:"host"`
Port string `json:"port"`
Username string `json:"username"`
@@ -115,10 +124,16 @@ type SSHAccess struct {
KeyPassphrase string `json:"keyPassphrase"`
}
type WebhookAccess struct {
Url string `json:"url"`
type TencentCloudAccessConfig struct {
SecretId string `json:"secretId"`
SecretKey string `json:"secretKey"`
}
type KubernetesAccess struct {
KubeConfig string `json:"kubeConfig"`
type VolcEngineAccessConfig struct {
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
}
type WebhookAccessConfig struct {
Url string `json:"url"`
}

View File

@@ -1,17 +1,13 @@
package domain
import (
"time"
"github.com/go-acme/lego/v4/registration"
)
type AcmeAccount struct {
Id string
Ca string
Email string
Resource *registration.Resource
Key string
Created time.Time
Updated time.Time
Meta
CA string `json:"ca" db:"ca"`
Email string `json:"email" db:"email"`
Resource *registration.Resource `json:"resource" db:"resource"`
Key string `json:"key" db:"key"`
}

View File

@@ -12,13 +12,13 @@ type Certificate struct {
IssuerCertificate string `json:"issuerCertificate" db:"issuerCertificate"`
CertUrl string `json:"certUrl" db:"certUrl"`
CertStableUrl string `json:"certStableUrl" db:"certStableUrl"`
Output string `json:"output" db:"output"`
Workflow string `json:"workflow" db:"workflow"`
ExpireAt time.Time `json:"ExpireAt" db:"expireAt"`
NodeId string `json:"nodeId" db:"nodeId"`
WorkflowId string `json:"workflow" db:"workflow"`
WorkflowNodeId string `json:"nodeId" db:"nodeId"`
WorkflowOutputId string `json:"output" db:"output"`
ExpireAt time.Time `json:"expireAt" db:"expireAt"`
}
type MetaData struct {
type CertificateMeta struct {
Version string `json:"version"`
SerialNumber string `json:"serialNumber"`
Validity CertificateValidity `json:"validity"`

View File

@@ -1,9 +0,0 @@
package domain
import "time"
type Meta struct {
Id string `json:"id" db:"id"`
Created time.Time `json:"created" db:"created"`
Updated time.Time `json:"updated" db:"updated"`
}

View File

@@ -1,12 +1,6 @@
package domain
import (
"encoding/json"
"strings"
"github.com/usual2970/certimate/internal/pkg/utils/maps"
)
// Deprecated: TODO: 即将废弃
type ApplyConfig struct {
Email string `json:"email"`
Access string `json:"access"`
@@ -16,6 +10,7 @@ type ApplyConfig struct {
DisableFollowCNAME bool `json:"disableFollowCNAME"`
}
// Deprecated: TODO: 即将废弃
type DeployConfig struct {
Id string `json:"id"`
Access string `json:"access"`
@@ -23,121 +18,7 @@ type DeployConfig struct {
Config map[string]any `json:"config"`
}
// Deprecated: 以字符串形式获取配置项。
//
// 入参:
// - key: 配置项的键。
//
// 出参:
// - 配置项的值。如果配置项不存在或者类型不是字符串,则返回空字符串。
func (dc *DeployConfig) GetConfigAsString(key string) string {
return maps.GetValueAsString(dc.Config, key)
}
// Deprecated: 以字符串形式获取配置项。
//
// 入参:
// - key: 配置项的键。
// - defaultValue: 默认值。
//
// 出参:
// - 配置项的值。如果配置项不存在、类型不是字符串或者值为零值,则返回默认值。
func (dc *DeployConfig) GetConfigOrDefaultAsString(key string, defaultValue string) string {
return maps.GetValueOrDefaultAsString(dc.Config, key, defaultValue)
}
// Deprecated: 以 32 位整数形式获取配置项。
//
// 入参:
// - key: 配置项的键。
//
// 出参:
// - 配置项的值。如果配置项不存在或者类型不是 32 位整数,则返回 0。
func (dc *DeployConfig) GetConfigAsInt32(key string) int32 {
return maps.GetValueAsInt32(dc.Config, key)
}
// Deprecated: 以 32 位整数形式获取配置项。
//
// 入参:
// - key: 配置项的键。
// - defaultValue: 默认值。
//
// 出参:
// - 配置项的值。如果配置项不存在、类型不是 32 位整数或者值为零值,则返回默认值。
func (dc *DeployConfig) GetConfigOrDefaultAsInt32(key string, defaultValue int32) int32 {
return maps.GetValueOrDefaultAsInt32(dc.Config, key, defaultValue)
}
// Deprecated: 以布尔形式获取配置项。
//
// 入参:
// - key: 配置项的键。
//
// 出参:
// - 配置项的值。如果配置项不存在或者类型不是布尔,则返回 false。
func (dc *DeployConfig) GetConfigAsBool(key string) bool {
return maps.GetValueAsBool(dc.Config, key)
}
// Deprecated: 以布尔形式获取配置项。
//
// 入参:
// - key: 配置项的键。
// - defaultValue: 默认值。
//
// 出参:
// - 配置项的值。如果配置项不存在或者类型不是布尔,则返回默认值。
func (dc *DeployConfig) GetConfigOrDefaultAsBool(key string, defaultValue bool) bool {
return maps.GetValueOrDefaultAsBool(dc.Config, key, defaultValue)
}
// Deprecated: 以变量字典形式获取配置项。
//
// 出参:
// - 变量字典。
func (dc *DeployConfig) GetConfigAsVariables() map[string]string {
rs := make(map[string]string)
if dc.Config != nil {
value, ok := dc.Config["variables"]
if !ok {
return rs
}
kvs := make([]KV, 0)
bts, _ := json.Marshal(value)
if err := json.Unmarshal(bts, &kvs); err != nil {
return rs
}
for _, kv := range kvs {
rs[kv.Key] = kv.Value
}
}
return rs
}
// Deprecated: GetDomain returns the domain from the deploy config,
// if the domain is a wildcard domain, and wildcard is true, return the wildcard domain
func (dc *DeployConfig) GetDomain(wildcard ...bool) string {
val := dc.GetConfigAsString("domain")
if val == "" {
return ""
}
if !strings.HasPrefix(val, "*") {
return val
}
if len(wildcard) > 0 && wildcard[0] {
return val
}
return strings.TrimPrefix(val, "*")
}
// Deprecated: TODO: 即将废弃
type KV struct {
Key string `json:"key"`
Value string `json:"value"`

9
internal/domain/meta.go Normal file
View File

@@ -0,0 +1,9 @@
package domain
import "time"
type Meta struct {
Id string `json:"id" db:"id"`
CreatedAt time.Time `json:"created" db:"created"`
UpdatedAt time.Time `json:"updated" db:"updated"`
}

View File

@@ -1,5 +1,7 @@
package domain
type NotifyChannelType string
/*
消息通知渠道常量值。
@@ -7,14 +9,14 @@ package domain
NOTICE: If you add new constant, please keep ASCII order.
*/
const (
NotifyChannelBark = "bark"
NotifyChannelDingtalk = "dingtalk"
NotifyChannelEmail = "email"
NotifyChannelLark = "lark"
NotifyChannelServerChan = "serverchan"
NotifyChannelTelegram = "telegram"
NotifyChannelWebhook = "webhook"
NotifyChannelWeCom = "wecom"
NOTIFY_CHANNEL_BARK = NotifyChannelType("bark")
NOTIFY_CHANNEL_DINGTALK = NotifyChannelType("dingtalk")
NOTIFY_CHANNEL_EMAIL = NotifyChannelType("email")
NOTIFY_CHANNEL_LARK = NotifyChannelType("lark")
NOTIFY_CHANNEL_SERVERCHAN = NotifyChannelType("serverchan")
NOTIFY_CHANNEL_TELEGRAM = NotifyChannelType("telegram")
NOTIFY_CHANNEL_WEBHOOK = NotifyChannelType("webhook")
NOTIFY_CHANNEL_WECOM = NotifyChannelType("wecom")
)
type NotifyTestPushReq struct {

View File

@@ -3,21 +3,18 @@ package domain
import (
"encoding/json"
"fmt"
"time"
)
type Setting struct {
ID string `json:"id"`
Name string `json:"name"`
Content string `json:"content"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
type Settings struct {
Meta
Name string `json:"name" db:"name"`
Content string `json:"content" db:"content"`
}
type ChannelsConfig map[string]map[string]any
type NotifyChannelsConfig map[string]map[string]any
func (s *Setting) GetChannelContent(channel string) (map[string]any, error) {
conf := &ChannelsConfig{}
func (s *Settings) GetChannelContent(channel string) (map[string]any, error) {
conf := &NotifyChannelsConfig{}
if err := json.Unmarshal([]byte(s.Content), conf); err != nil {
return nil, err
}