mirror of https://github.com/h44z/wg-portal.git
				
				
				
			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)) | ||||
| 
 | ||||
| 			for _, controller := range controllers { | ||||
| 				displayName := controller.DisplayName | ||||
| 				displayName := controller.GetDisplayName() | ||||
| 				if displayName == "" { | ||||
| 					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
 | ||||
| } | ||||
| 
 | ||||
| // 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 { | ||||
| 	BackendBase `yaml:",inline"` // Embed the base fields
 | ||||
| 
 | ||||
|  | @ -71,3 +80,15 @@ func (b *BackendMikrotik) GetConcurrency() int { | |||
| 	} | ||||
| 	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, | ||||
| 	} | ||||
| 
 | ||||
| 	if cfg.ApiTimeout == 0 { | ||||
| 		cfg.ApiTimeout = 30 * time.Second // Default timeout for API requests
 | ||||
| 	} | ||||
| 
 | ||||
| 	err := c.setup() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
|  | @ -171,7 +167,7 @@ func (m *MikrotikApiClient) setup() error { | |||
| 				InsecureSkipVerify: !m.cfg.ApiVerifyTls, | ||||
| 			}, | ||||
| 		}, | ||||
| 		Timeout: m.cfg.ApiTimeout, | ||||
| 		Timeout: m.cfg.GetApiTimeout(), | ||||
| 	} | ||||
| 
 | ||||
| 	if m.cfg.Debug { | ||||
|  | @ -304,7 +300,7 @@ func (m *MikrotikApiClient) Query( | |||
| 	command string, | ||||
| 	opts *MikrotikRequestOptions, | ||||
| ) MikrotikApiResponse[[]GenericJsonObject] { | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout) | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout()) | ||||
| 	defer cancel() | ||||
| 
 | ||||
| 	fullUrl := opts.GetPath(m.getFullPath(command)) | ||||
|  | @ -327,7 +323,7 @@ func (m *MikrotikApiClient) Get( | |||
| 	command string, | ||||
| 	opts *MikrotikRequestOptions, | ||||
| ) MikrotikApiResponse[GenericJsonObject] { | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout) | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout()) | ||||
| 	defer cancel() | ||||
| 
 | ||||
| 	fullUrl := opts.GetPath(m.getFullPath(command)) | ||||
|  | @ -350,7 +346,7 @@ func (m *MikrotikApiClient) Create( | |||
| 	command string, | ||||
| 	payload GenericJsonObject, | ||||
| ) MikrotikApiResponse[GenericJsonObject] { | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout) | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout()) | ||||
| 	defer cancel() | ||||
| 
 | ||||
| 	fullUrl := m.getFullPath(command) | ||||
|  | @ -373,7 +369,7 @@ func (m *MikrotikApiClient) Update( | |||
| 	command string, | ||||
| 	payload GenericJsonObject, | ||||
| ) MikrotikApiResponse[GenericJsonObject] { | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout) | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout()) | ||||
| 	defer cancel() | ||||
| 
 | ||||
| 	fullUrl := m.getFullPath(command) | ||||
|  | @ -395,7 +391,7 @@ func (m *MikrotikApiClient) Delete( | |||
| 	ctx context.Context, | ||||
| 	command string, | ||||
| ) MikrotikApiResponse[EmptyResponse] { | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout) | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout()) | ||||
| 	defer cancel() | ||||
| 
 | ||||
| 	fullUrl := m.getFullPath(command) | ||||
|  | @ -418,7 +414,7 @@ func (m *MikrotikApiClient) ExecList( | |||
| 	command string, | ||||
| 	payload GenericJsonObject, | ||||
| ) MikrotikApiResponse[[]GenericJsonObject] { | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.ApiTimeout) | ||||
| 	apiCtx, cancel := context.WithTimeout(ctx, m.cfg.GetApiTimeout()) | ||||
| 	defer cancel() | ||||
| 
 | ||||
| 	fullUrl := m.getFullPath(command) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue