|
| 1 | +package settings |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/JacksonTheMaster/StationeersServerUI/v5/src/config" |
| 9 | +) |
| 10 | + |
| 11 | +// package settings handles API communication with the config values in package config via getter /setter functions. |
| 12 | + |
| 13 | +// ConfigSetting represents metadata for a configuration setting |
| 14 | +type ConfigSetting struct { |
| 15 | + Name string `json:"name"` |
| 16 | + Type string `json:"type"` |
| 17 | + Group string `json:"group"` |
| 18 | + Description string `json:"description"` |
| 19 | + Value interface{} `json:"value"` |
| 20 | + Min *int `json:"min,omitempty"` |
| 21 | + Max *int `json:"max,omitempty"` |
| 22 | + Required bool `json:"required"` |
| 23 | +} |
| 24 | + |
| 25 | +// ConfigSettingsResponse represents the API response |
| 26 | +type ConfigSettingsResponse struct { |
| 27 | + Data []ConfigSetting `json:"data"` |
| 28 | + Error string `json:"error,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +func RetrieveSettings(w http.ResponseWriter, r *http.Request) { |
| 32 | + if r.Method != http.MethodGet { |
| 33 | + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + settings := []ConfigSetting{ |
| 38 | + { |
| 39 | + Name: "IsDebugMode", |
| 40 | + Type: "bool", |
| 41 | + Group: "System Settings", |
| 42 | + Description: "Enable pprof server", |
| 43 | + Value: config.GetIsDebugMode(), |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "CreateSSUILogFile", |
| 47 | + Type: "bool", |
| 48 | + Group: "System Settings", |
| 49 | + Description: "Create SSUI log files", |
| 50 | + Value: config.GetCreateSSUILogFile(), |
| 51 | + }, |
| 52 | + { |
| 53 | + Name: "LogLevel", |
| 54 | + Type: "int", |
| 55 | + Group: "System Settings", |
| 56 | + Description: "Logging verbosity level", |
| 57 | + Value: config.GetLogLevel(), |
| 58 | + Min: intPtr(0), |
| 59 | + }, |
| 60 | + //{ |
| 61 | + // Name: "BackendEndpointIP", |
| 62 | + // Type: "string", |
| 63 | + // Group: "System Settings", |
| 64 | + // Description: "IP address for backend endpoint", |
| 65 | + // Value: config.GetBackendEndpointIP(), |
| 66 | + // Required: true, |
| 67 | + //}, |
| 68 | + { |
| 69 | + Name: "BackendEndpointPort", |
| 70 | + Type: "string", |
| 71 | + Group: "System Settings", |
| 72 | + Description: "Port for backend endpoint", |
| 73 | + Value: config.GetSSUIWebPort(), // CHANGE VAR NAME TO MATCH (Get)BackendEndpointPort |
| 74 | + Required: true, |
| 75 | + }, |
| 76 | + //{ |
| 77 | + // Name: "BackupKeepLastN", |
| 78 | + // Type: "int", |
| 79 | + // Group: "Advanced Settings", |
| 80 | + // Description: "Number of recent backups to keep", |
| 81 | + // Value: config.GetBackupKeepLastN(), |
| 82 | + // Min: intPtr(0), |
| 83 | + //}, |
| 84 | + //{ |
| 85 | + // Name: "IsCleanupEnabled", |
| 86 | + // Type: "bool", |
| 87 | + // Group: "Advanced Settings", |
| 88 | + // Description: "Enable backup cleanup", |
| 89 | + // Value: config.GetIsCleanupEnabled(), |
| 90 | + //}, |
| 91 | + //{ |
| 92 | + // Name: "BackupKeepDailyFor", |
| 93 | + // Type: "int", |
| 94 | + // Group: "Advanced Settings", |
| 95 | + // Description: "Hours to keep daily backups", |
| 96 | + // Value: int(config.GetBackupKeepDailyFor() / time.Hour), |
| 97 | + // Min: intPtr(0), |
| 98 | + //}, |
| 99 | + //{ |
| 100 | + // Name: "BackupKeepWeeklyFor", |
| 101 | + // Type: "int", |
| 102 | + // Group: "Advanced Settings", |
| 103 | + // Description: "Hours to keep weekly backups", |
| 104 | + // Value: int(config.GetBackupKeepWeeklyFor() / time.Hour), |
| 105 | + // Min: intPtr(0), |
| 106 | + //}, |
| 107 | + //{ |
| 108 | + // Name: "BackupKeepMonthlyFor", |
| 109 | + // Type: "int", |
| 110 | + // Group: "Advanced Settings", |
| 111 | + // Description: "Hours to keep monthly backups", |
| 112 | + // Value: int(config.GetBackupKeepMonthlyFor() / time.Hour), |
| 113 | + // Min: intPtr(0), |
| 114 | + //}, |
| 115 | + //{ |
| 116 | + // Name: "BackupCleanupInterval", |
| 117 | + // Type: "int", |
| 118 | + // Group: "Advanced Settings", |
| 119 | + // Description: "Hours between backup cleanup runs", |
| 120 | + // Value: int(config.GetBackupCleanupInterval() / time.Hour), |
| 121 | + // Min: intPtr(0), |
| 122 | + //}, |
| 123 | + //{ |
| 124 | + // Name: "BackupWaitTime", |
| 125 | + // Type: "int", |
| 126 | + // Group: "Advanced Settings", |
| 127 | + // Description: "Seconds to wait before backup", |
| 128 | + // Value: int(config.GetBackupWaitTime() / time.Second), |
| 129 | + // Min: intPtr(0), |
| 130 | + //}, |
| 131 | + { |
| 132 | + Name: "GameBranch", |
| 133 | + Type: "string", |
| 134 | + Group: "Update Settings", |
| 135 | + Description: "Game branch for updates (Restart Required)", |
| 136 | + Value: config.GetGameBranch(), |
| 137 | + }, |
| 138 | + //{ |
| 139 | + // Name: "Users", |
| 140 | + // Type: "map", |
| 141 | + // Group: "Advanced Settings", |
| 142 | + // Description: "User authentication mappings", |
| 143 | + // Value: config.GetUsers(), |
| 144 | + //}, |
| 145 | + { |
| 146 | + Name: "AuthEnabled", |
| 147 | + Type: "bool", |
| 148 | + Group: "System Settings", |
| 149 | + Description: "Enable authentication", |
| 150 | + Value: config.GetAuthEnabled(), |
| 151 | + }, |
| 152 | + { |
| 153 | + Name: "JwtKey", |
| 154 | + Type: "string", |
| 155 | + Group: "Security Settings", |
| 156 | + Description: "Encryption key for Authentication", |
| 157 | + Value: config.GetJwtKey(), |
| 158 | + }, |
| 159 | + { |
| 160 | + Name: "AuthTokenLifetime", |
| 161 | + Type: "int", |
| 162 | + Group: "Security Settings", |
| 163 | + Description: "Token lifetime in seconds", |
| 164 | + Value: config.GetAuthTokenLifetime(), |
| 165 | + Min: intPtr(0), |
| 166 | + }, |
| 167 | + { |
| 168 | + Name: "IsUpdateEnabled", |
| 169 | + Type: "bool", |
| 170 | + Group: "System Settings", |
| 171 | + Description: "Enable automatic updates", |
| 172 | + Value: config.GetIsUpdateEnabled(), |
| 173 | + }, |
| 174 | + //{ |
| 175 | + // Name: "IsSSCMEnabled", |
| 176 | + // Type: "bool", |
| 177 | + // Group: "Advanced Settings", |
| 178 | + // Description: "Enable SSCM integration", |
| 179 | + // Value: config.GetIsSSCMEnabled(), |
| 180 | + //}, |
| 181 | + //{ |
| 182 | + // Name: "IsCodeServerEnabled", |
| 183 | + // Type: "bool", |
| 184 | + // Group: "System Settings", |
| 185 | + // Description: "Enables the Code Server integration. Linux only. Attention: Does NOT have an updater at the moment. Be careful with this feature.", |
| 186 | + // Value: config.GetIsCodeServerEnabled(), |
| 187 | + //}, |
| 188 | + { |
| 189 | + Name: "AllowPrereleaseUpdates", |
| 190 | + Type: "bool", |
| 191 | + Group: "Update Settings", |
| 192 | + Description: "Allow prerelease updates", |
| 193 | + Value: config.GetAllowPrereleaseUpdates(), |
| 194 | + }, |
| 195 | + { |
| 196 | + Name: "AllowMajorUpdates", |
| 197 | + Type: "bool", |
| 198 | + Group: "Update Settings", |
| 199 | + Description: "Allow major version updates", |
| 200 | + Value: config.GetAllowMajorUpdates(), |
| 201 | + }, |
| 202 | + // Discord Settings |
| 203 | + { |
| 204 | + Name: "IsDiscordEnabled", |
| 205 | + Type: "bool", |
| 206 | + Group: "Discord Settings", |
| 207 | + Description: "Enable Discord integration", |
| 208 | + Value: config.GetIsDiscordEnabled(), |
| 209 | + }, |
| 210 | + { |
| 211 | + Name: "DiscordToken", |
| 212 | + Type: "string", |
| 213 | + Group: "Discord Settings", |
| 214 | + Description: "Discord bot token", |
| 215 | + Value: config.GetDiscordToken(), |
| 216 | + }, |
| 217 | + { |
| 218 | + Name: "ControlChannelID", |
| 219 | + Type: "string", |
| 220 | + Group: "Discord Settings", |
| 221 | + Description: "Control channel ID", |
| 222 | + Value: config.GetControlChannelID(), |
| 223 | + }, |
| 224 | + { |
| 225 | + Name: "StatusChannelID", |
| 226 | + Type: "string", |
| 227 | + Group: "Discord Settings", |
| 228 | + Description: "Status channel ID", |
| 229 | + Value: config.GetStatusChannelID(), |
| 230 | + }, |
| 231 | + { |
| 232 | + Name: "ConnectionListChannelID", |
| 233 | + Type: "string", |
| 234 | + Group: "Discord Settings", |
| 235 | + Description: "Connection list channel ID", |
| 236 | + Value: config.GetConnectionListChannelID(), |
| 237 | + }, |
| 238 | + { |
| 239 | + Name: "LogChannelID", |
| 240 | + Type: "string", |
| 241 | + Group: "Discord Settings", |
| 242 | + Description: "Log channel ID", |
| 243 | + Value: config.GetLogChannelID(), |
| 244 | + }, |
| 245 | + { |
| 246 | + Name: "SaveChannelID", |
| 247 | + Type: "string", |
| 248 | + Group: "Discord Settings", |
| 249 | + Description: "Save channel ID", |
| 250 | + Value: config.GetSaveChannelID(), |
| 251 | + }, |
| 252 | + { |
| 253 | + Name: "ControlPanelChannelID", |
| 254 | + Type: "string", |
| 255 | + Group: "Discord Settings", |
| 256 | + Description: "Control panel channel ID", |
| 257 | + Value: config.GetControlPanelChannelID(), |
| 258 | + }, |
| 259 | + { |
| 260 | + Name: "DiscordCharBufferSize", |
| 261 | + Type: "int", |
| 262 | + Group: "Discord Settings", |
| 263 | + Description: "Discord character buffer size", |
| 264 | + Value: config.GetDiscordCharBufferSize(), |
| 265 | + }, |
| 266 | + { |
| 267 | + Name: "ErrorChannelID", |
| 268 | + Type: "string", |
| 269 | + Group: "Discord Settings", |
| 270 | + Description: "Error channel ID", |
| 271 | + Value: config.GetErrorChannelID(), |
| 272 | + }, |
| 273 | + //{ |
| 274 | + // Name: "BackupContentDir", |
| 275 | + // Type: "string", |
| 276 | + // Group: "Backup Settings", |
| 277 | + // Description: "Backup content directory", |
| 278 | + // Value: config.GetBackupContentDir(), |
| 279 | + //}, |
| 280 | + //{ |
| 281 | + // Name: "BackupsStoreDir", |
| 282 | + // Type: "string", |
| 283 | + // Group: "Backup Settings", |
| 284 | + // Description: "Backup stored backups directory", |
| 285 | + // Value: config.GetBackupsStoreDir(), |
| 286 | + //}, |
| 287 | + //{ |
| 288 | + // Name: "BackupLoopInterval", |
| 289 | + // Type: "string", |
| 290 | + // Group: "Backup Settings", |
| 291 | + // Description: "Backup loop interval", |
| 292 | + // Value: config.GetBackupLoopInterval().String(), |
| 293 | + //}, |
| 294 | + //{ |
| 295 | + // Name: "BackupMode", |
| 296 | + // Type: "string", |
| 297 | + // Group: "Backup Settings", |
| 298 | + // Description: "Backup mode", |
| 299 | + // Value: config.GetBackupMode(), |
| 300 | + //}, |
| 301 | + //{ |
| 302 | + // Name: "BackupMaxFileSize", |
| 303 | + // Type: "int", |
| 304 | + // Group: "Backup Settings", |
| 305 | + // Description: "Max file size", |
| 306 | + // Value: config.GetBackupMaxFileSize(), |
| 307 | + //}, |
| 308 | + //{ |
| 309 | + // Name: "BackupUseCompression", |
| 310 | + // Type: "bool", |
| 311 | + // Group: "Backup Settings", |
| 312 | + // Description: "Use compression", |
| 313 | + // Value: config.GetBackupUseCompression(), |
| 314 | + //}, |
| 315 | + //{ |
| 316 | + // Name: "BackupKeepSnapshot", |
| 317 | + // Type: "bool", |
| 318 | + // Group: "Backup Settings", |
| 319 | + // Description: "Keep snapshot", |
| 320 | + // Value: config.GetBackupKeepSnapshot(), |
| 321 | + //}, |
| 322 | + { |
| 323 | + Name: "IsConsoleEnabled", |
| 324 | + Type: "bool", |
| 325 | + Group: "System Settings", |
| 326 | + Description: "Expose various actions directly in the command line (Restart Required)", |
| 327 | + Value: config.GetIsConsoleEnabled(), |
| 328 | + }, |
| 329 | + } |
| 330 | + |
| 331 | + response := ConfigSettingsResponse{ |
| 332 | + Data: settings, |
| 333 | + } |
| 334 | + |
| 335 | + w.Header().Set("Content-Type", "application/json") |
| 336 | + if err := json.NewEncoder(w).Encode(response); err != nil { |
| 337 | + http.Error(w, fmt.Sprintf("Error encoding response: %v", err), http.StatusInternalServerError) |
| 338 | + return |
| 339 | + } |
| 340 | +} |
| 341 | + |
| 342 | +// intPtr creates a pointer to an int |
| 343 | +func intPtr(i int) *int { |
| 344 | + return &i |
| 345 | +} |
0 commit comments