mirror of
https://github.com/h44z/wg-portal.git
synced 2025-08-12 08:12:23 +00:00
improve configuration defaults handling
This commit is contained in:
parent
0037938f9e
commit
593bb983fb
@ -107,7 +107,7 @@ func (e ConfigEndpoint) handleSettingsGet() http.HandlerFunc {
|
|||||||
names := make([]model.SettingsBackendNames, 0, len(controllers))
|
names := make([]model.SettingsBackendNames, 0, len(controllers))
|
||||||
|
|
||||||
for _, controller := range controllers {
|
for _, controller := range controllers {
|
||||||
displayName := controller.DisplayName
|
displayName := controller.GetDisplayName()
|
||||||
if displayName == "" {
|
if displayName == "" {
|
||||||
displayName = controller.Id // fallback to ID if no display name is set
|
displayName = controller.Id // fallback to ID if no display name is set
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,15 @@ type BackendBase struct {
|
|||||||
DisplayName string `yaml:"display_name"` // A display name for the backend
|
DisplayName string `yaml:"display_name"` // A display name for the backend
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDisplayName returns the display name of the backend.
|
||||||
|
// If no display name is set, it falls back to the ID.
|
||||||
|
func (b BackendBase) GetDisplayName() string {
|
||||||
|
if b.DisplayName == "" {
|
||||||
|
return b.Id // Fallback to ID if no display name is set
|
||||||
|
}
|
||||||
|
return b.DisplayName
|
||||||
|
}
|
||||||
|
|
||||||
type BackendMikrotik struct {
|
type BackendMikrotik struct {
|
||||||
BackendBase `yaml:",inline"` // Embed the base fields
|
BackendBase `yaml:",inline"` // Embed the base fields
|
||||||
|
|
||||||
@ -71,3 +80,15 @@ func (b *BackendMikrotik) GetConcurrency() int {
|
|||||||
}
|
}
|
||||||
return b.Concurrency
|
return b.Concurrency
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetApiTimeout returns the configured API timeout or a sane default (30 seconds)
|
||||||
|
// when the configured value is zero or negative.
|
||||||
|
func (b *BackendMikrotik) GetApiTimeout() time.Duration {
|
||||||
|
if b == nil {
|
||||||
|
return 30 * time.Second
|
||||||
|
}
|
||||||
|
if b.ApiTimeout <= 0 {
|
||||||
|
return 30 * time.Second
|
||||||
|
}
|
||||||
|
return b.ApiTimeout
|
||||||
|
}
|
||||||
|
@ -150,10 +150,6 @@ func NewMikrotikApiClient(coreCfg *config.Config, cfg *config.BackendMikrotik) (
|
|||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.ApiTimeout == 0 {
|
|
||||||
cfg.ApiTimeout = 30 * time.Second // Default timeout for API requests
|
|
||||||
}
|
|
||||||
|
|
||||||
err := c.setup()
|
err := c.setup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -171,7 +167,7 @@ func (m *MikrotikApiClient) setup() error {
|
|||||||
InsecureSkipVerify: !m.cfg.ApiVerifyTls,
|
InsecureSkipVerify: !m.cfg.ApiVerifyTls,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Timeout: m.cfg.ApiTimeout,
|
Timeout: m.cfg.GetApiTimeout(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.cfg.Debug {
|
if m.cfg.Debug {
|
||||||
@ -304,7 +300,7 @@ func (m *MikrotikApiClient) Query(
|
|||||||
command string,
|
command string,
|
||||||
opts *MikrotikRequestOptions,
|
opts *MikrotikRequestOptions,
|
||||||
) MikrotikApiResponse[[]GenericJsonObject] {
|
) MikrotikApiResponse[[]GenericJsonObject] {
|
||||||
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout)
|
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fullUrl := opts.GetPath(m.getFullPath(command))
|
fullUrl := opts.GetPath(m.getFullPath(command))
|
||||||
@ -327,7 +323,7 @@ func (m *MikrotikApiClient) Get(
|
|||||||
command string,
|
command string,
|
||||||
opts *MikrotikRequestOptions,
|
opts *MikrotikRequestOptions,
|
||||||
) MikrotikApiResponse[GenericJsonObject] {
|
) MikrotikApiResponse[GenericJsonObject] {
|
||||||
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout)
|
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fullUrl := opts.GetPath(m.getFullPath(command))
|
fullUrl := opts.GetPath(m.getFullPath(command))
|
||||||
@ -350,7 +346,7 @@ func (m *MikrotikApiClient) Create(
|
|||||||
command string,
|
command string,
|
||||||
payload GenericJsonObject,
|
payload GenericJsonObject,
|
||||||
) MikrotikApiResponse[GenericJsonObject] {
|
) MikrotikApiResponse[GenericJsonObject] {
|
||||||
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout)
|
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fullUrl := m.getFullPath(command)
|
fullUrl := m.getFullPath(command)
|
||||||
@ -373,7 +369,7 @@ func (m *MikrotikApiClient) Update(
|
|||||||
command string,
|
command string,
|
||||||
payload GenericJsonObject,
|
payload GenericJsonObject,
|
||||||
) MikrotikApiResponse[GenericJsonObject] {
|
) MikrotikApiResponse[GenericJsonObject] {
|
||||||
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout)
|
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fullUrl := m.getFullPath(command)
|
fullUrl := m.getFullPath(command)
|
||||||
@ -395,7 +391,7 @@ func (m *MikrotikApiClient) Delete(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
command string,
|
command string,
|
||||||
) MikrotikApiResponse[EmptyResponse] {
|
) MikrotikApiResponse[EmptyResponse] {
|
||||||
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout)
|
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fullUrl := m.getFullPath(command)
|
fullUrl := m.getFullPath(command)
|
||||||
@ -418,7 +414,7 @@ func (m *MikrotikApiClient) ExecList(
|
|||||||
command string,
|
command string,
|
||||||
payload GenericJsonObject,
|
payload GenericJsonObject,
|
||||||
) MikrotikApiResponse[[]GenericJsonObject] {
|
) MikrotikApiResponse[[]GenericJsonObject] {
|
||||||
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout)
|
apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fullUrl := m.getFullPath(command)
|
fullUrl := m.getFullPath(command)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user