ddns: add listProviders api (#446)

* ddns: add listProviders api

* fix swagger
This commit is contained in:
UUBulb 2024-10-22 00:04:17 +08:00 committed by GitHub
parent 7e6864fa8c
commit 15585ef12e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 57 additions and 92 deletions

View File

@ -71,6 +71,7 @@ func routers(r *gin.Engine) {
auth.POST("/batch-delete/server", commonHandler(batchDeleteServer)) auth.POST("/batch-delete/server", commonHandler(batchDeleteServer))
auth.GET("/ddns", commonHandler(listDDNS)) auth.GET("/ddns", commonHandler(listDDNS))
auth.GET("/ddns/providers", commonHandler(listProviders))
auth.POST("/ddns", commonHandler(newDDNS)) auth.POST("/ddns", commonHandler(newDDNS))
auth.PATCH("/ddns/:id", commonHandler(editDDNS)) auth.PATCH("/ddns/:id", commonHandler(editDDNS))
auth.POST("/batch-delete/ddns", commonHandler(batchDeleteDDNS)) auth.POST("/batch-delete/ddns", commonHandler(batchDeleteDDNS))

View File

@ -227,3 +227,20 @@ func listDDNS(c *gin.Context) error {
}) })
return nil return nil
} }
// List DDNS Providers
// @Summary List DDNS providers
// @Schemes
// @Description List DDNS providers
// @Security BearerAuth
// @Tags auth required
// @Produce json
// @Success 200 {object} model.CommonResponse[[]string]
// @Router /ddns/providers [get]
func listProviders(c *gin.Context) error {
c.JSON(http.StatusOK, model.CommonResponse[[]string]{
Success: true,
Data: model.ProviderList,
})
return nil
}

View File

@ -720,7 +720,7 @@ type ddnsForm struct {
EnableIPv4 string EnableIPv4 string
EnableIPv6 string EnableIPv6 string
Name string Name string
Provider uint8 Provider string
DomainsRaw string DomainsRaw string
AccessID string AccessID string
AccessSecret string AccessSecret string

View File

@ -82,9 +82,9 @@ func (mp *memberPage) ddns(c *gin.Context) {
singleton.DB.Find(&data) singleton.DB.Find(&data)
c.HTML(http.StatusOK, "dashboard-", gin.H{ c.HTML(http.StatusOK, "dashboard-", gin.H{
// "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "DDNS"}), // "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "DDNS"}),
"DDNS": data, "DDNS": data,
"ProviderMap": model.ProviderMap, //"ProviderMap": model.ProviderMap,
"ProviderList": model.ProviderList, //"ProviderList": model.ProviderList,
}) })
} }

View File

@ -7,73 +7,32 @@ import (
) )
const ( const (
ProviderDummy = iota ProviderDummy = "dummy"
ProviderWebHook ProviderWebHook = "webhook"
ProviderCloudflare ProviderCloudflare = "cloudflare"
ProviderTencentCloud ProviderTencentCloud = "tencentcloud"
) )
const ( var ProviderList = []string{
_Dummy = "dummy" ProviderDummy, ProviderWebHook, ProviderCloudflare, ProviderTencentCloud,
_WebHook = "webhook"
_Cloudflare = "cloudflare"
_TencentCloud = "tencentcloud"
)
var ProviderMap = map[uint8]string{
ProviderDummy: _Dummy,
ProviderWebHook: _WebHook,
ProviderCloudflare: _Cloudflare,
ProviderTencentCloud: _TencentCloud,
}
var ProviderList = []DDNSProvider{
{
Name: _Dummy,
ID: ProviderDummy,
},
{
Name: _Cloudflare,
ID: ProviderCloudflare,
AccessSecret: true,
},
{
Name: _TencentCloud,
ID: ProviderTencentCloud,
AccessID: true,
AccessSecret: true,
},
// Least frequently used, always place this at the end
{
Name: _WebHook,
ID: ProviderWebHook,
AccessID: true,
AccessSecret: true,
WebhookURL: true,
WebhookMethod: true,
WebhookRequestType: true,
WebhookRequestBody: true,
WebhookHeaders: true,
},
} }
type DDNSProfile struct { type DDNSProfile struct {
Common Common
EnableIPv4 *bool EnableIPv4 *bool `json:"enable_ipv4,omitempty"`
EnableIPv6 *bool EnableIPv6 *bool `json:"enable_ipv6,omitempty"`
MaxRetries uint64 MaxRetries uint64 `json:"max_retries,omitempty"`
Name string Name string `json:"name,omitempty"`
Provider uint8 Provider string `json:"provider,omitempty"`
AccessID string AccessID string `json:"access_id,omitempty"`
AccessSecret string AccessSecret string `json:"access_secret,omitempty"`
WebhookURL string WebhookURL string `json:"webhook_url,omitempty"`
WebhookMethod uint8 WebhookMethod uint8 `json:"webhook_method,omitempty"`
WebhookRequestType uint8 WebhookRequestType uint8 `json:"webhook_request_type,omitempty"`
WebhookRequestBody string WebhookRequestBody string `json:"webhook_request_body,omitempty"`
WebhookHeaders string WebhookHeaders string `json:"webhook_headers,omitempty"`
Domains []string `json:"domains,omitempty" gorm:"-"`
Domains []string `gorm:"-"` DomainsRaw string `json:"domains_raw,omitempty"`
DomainsRaw string
} }
func (d DDNSProfile) TableName() string { func (d DDNSProfile) TableName() string {
@ -87,31 +46,19 @@ func (d *DDNSProfile) AfterFind(tx *gorm.DB) error {
return nil return nil
} }
type DDNSProvider struct {
Name string
ID uint8
AccessID bool
AccessSecret bool
WebhookURL bool
WebhookMethod bool
WebhookRequestType bool
WebhookRequestBody bool
WebhookHeaders bool
}
type DDNSForm struct { type DDNSForm struct {
ID uint64 ID uint64 `json:"id,omitempty"`
MaxRetries uint64 MaxRetries uint64 `json:"max_retries,omitempty"`
EnableIPv4 string EnableIPv4 string `json:"enable_ipv4,omitempty"`
EnableIPv6 string EnableIPv6 string `json:"enable_ipv6,omitempty"`
Name string Name string `json:"name,omitempty"`
Provider uint8 Provider string `json:"provider,omitempty"`
DomainsRaw string DomainsRaw string `json:"domains_raw,omitempty"`
AccessID string AccessID string `json:"access_id,omitempty"`
AccessSecret string AccessSecret string `json:"access_secret,omitempty"`
WebhookURL string WebhookURL string `json:"webhook_url,omitempty"`
WebhookMethod uint8 WebhookMethod uint8 `json:"webhook_method,omitempty"`
WebhookRequestType uint8 WebhookRequestType uint8 `json:"webhook_request_type,omitempty"`
WebhookRequestBody string WebhookRequestBody string `json:"webhook_request_body,omitempty"`
WebhookHeaders string WebhookHeaders string `json:"webhook_headers,omitempty"`
} }

View File

@ -67,7 +67,7 @@ func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Pr
provider.Setter = &tencentcloud.Provider{SecretId: profile.AccessID, SecretKey: profile.AccessSecret} provider.Setter = &tencentcloud.Provider{SecretId: profile.AccessID, SecretKey: profile.AccessSecret}
providers = append(providers, provider) providers = append(providers, provider)
default: default:
return nil, fmt.Errorf("无法找到配置的DDNS提供者ID %d", profile.Provider) return nil, fmt.Errorf("无法找到配置的DDNS提供者 %s", profile.Provider)
} }
} }
return providers, nil return providers, nil