Skip to content

Commit a094c42

Browse files
committed
Refactor configuration handling by removing writable flag from ConfigInfo and related UI components. Simplify config response structure in WebUIServer and update frontend to reflect these changes. Clean up unused CSS styles for improved layout.
1 parent 8e44b57 commit a094c42

File tree

4 files changed

+26
-118
lines changed

4 files changed

+26
-118
lines changed

src/config/config.go

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,9 @@ func (k *KeyBytes) UnmarshalJSON(b []byte) error {
286286

287287
// ConfigInfo contains information about the configuration file
288288
type ConfigInfo struct {
289-
Path string `json:"path"`
290-
Format string `json:"format"`
291-
Data interface{} `json:"data"`
292-
Writable bool `json:"writable"`
289+
Path string `json:"path"`
290+
Format string `json:"format"`
291+
Data interface{} `json:"data"`
293292
}
294293

295294
// Global variables to track the current configuration state
@@ -338,20 +337,6 @@ func validateConfigPath(path string) (string, error) {
338337
}
339338
}
340339

341-
// Basic sanity check on file extension for config files
342-
ext := strings.ToLower(filepath.Ext(absPath))
343-
allowedExts := []string{".json", ".hjson", ".conf", ".config", ".yml", ".yaml", ""}
344-
validExt := false
345-
for _, allowed := range allowedExts {
346-
if ext == allowed {
347-
validExt = true
348-
break
349-
}
350-
}
351-
if !validExt {
352-
return "", fmt.Errorf("invalid file extension: %s", ext)
353-
}
354-
355340
// Additional check: ensure the path doesn't escape intended directories
356341
if strings.Count(absPath, "/") > 10 {
357342
return "", fmt.Errorf("path too deep: potential security risk")
@@ -381,7 +366,6 @@ func GetCurrentConfig() (*ConfigInfo, error) {
381366
var configPath string
382367
var configData *NodeConfig
383368
var format string = "hjson"
384-
var writable bool = false
385369

386370
// Use current config if available, otherwise try to read from default location
387371
if currentConfigPath != "" && currentConfigData != nil {
@@ -402,72 +386,33 @@ func GetCurrentConfig() (*ConfigInfo, error) {
402386
if err != nil {
403387
return nil, fmt.Errorf("invalid default config path: %v", err)
404388
}
405-
configPath = validatedDefaultPath
406389

407-
// Try to read existing config file
408-
if _, err := os.Stat(configPath); err == nil { // Path already validated above
409-
data, err := os.ReadFile(configPath) // Path already validated above
410-
if err == nil {
411-
cfg := GenerateConfig()
412-
if err := hjson.Unmarshal(data, cfg); err == nil {
413-
configData = cfg
414-
// Detect format
415-
var jsonTest interface{}
416-
if json.Unmarshal(data, &jsonTest) == nil {
417-
format = "json"
418-
}
419-
} else {
420-
return nil, fmt.Errorf("failed to parse config file: %v", err)
421-
}
422-
}
423-
} else {
424-
// No config file exists, use default
425-
configData = GenerateConfig()
426-
}
390+
configPath = validatedDefaultPath
391+
configData = GenerateConfig()
427392
}
428393

429-
// Detect format from file if path is known
430-
if configPath != "" {
431-
// Config path is already validated at this point
432-
if _, err := os.Stat(configPath); err == nil { // Path already validated above
433-
data, err := os.ReadFile(configPath) // Path already validated above
434-
if err == nil {
394+
// Try to read existing config file
395+
if _, err := os.Stat(configPath); err == nil { // Path already validated above
396+
data, err := os.ReadFile(configPath) // Path already validated above
397+
if err == nil {
398+
cfg := GenerateConfig()
399+
if err := hjson.Unmarshal(data, cfg); err == nil {
400+
configData = cfg
401+
// Detect format
435402
var jsonTest interface{}
436403
if json.Unmarshal(data, &jsonTest) == nil {
437404
format = "json"
438405
}
439-
}
440-
}
441-
}
442-
443-
// Check if writable
444-
if configPath != "" {
445-
// Config path is already validated at this point
446-
if _, err := os.Stat(configPath); err == nil { // Path already validated above
447-
// File exists, check if writable
448-
if file, err := os.OpenFile(configPath, os.O_WRONLY, 0); err == nil { // Path already validated above
449-
writable = true
450-
file.Close()
451-
}
452-
} else {
453-
// File doesn't exist, check if directory is writable
454-
dir := filepath.Clean(filepath.Dir(configPath))
455-
if stat, err := os.Stat(dir); err == nil && stat.IsDir() {
456-
testFile := filepath.Join(dir, ".yggdrasil_write_test")
457-
if file, err := os.Create(testFile); err == nil {
458-
file.Close()
459-
os.Remove(testFile)
460-
writable = true
461-
}
406+
} else {
407+
return nil, fmt.Errorf("failed to parse config file: %v", err)
462408
}
463409
}
464410
}
465411

466412
return &ConfigInfo{
467-
Path: configPath,
468-
Format: format,
469-
Data: configData,
470-
Writable: writable,
413+
Path: configPath,
414+
Format: format,
415+
Data: configData,
471416
}, nil
472417
}
473418

@@ -516,6 +461,7 @@ func SaveConfig(configData interface{}, configPath, format string) error {
516461
}
517462
}
518463
}
464+
519465
if targetFormat == "" {
520466
targetFormat = "hjson"
521467
}

src/webui/server.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ type ConfigResponse struct {
388388
ConfigPath string `json:"config_path"`
389389
ConfigFormat string `json:"config_format"`
390390
ConfigJSON string `json:"config_json"`
391-
IsWritable bool `json:"is_writable"`
392391
}
393392

394393
type ConfigSetRequest struct {
@@ -432,7 +431,6 @@ func (w *WebUIServer) getConfigHandler(rw http.ResponseWriter, r *http.Request)
432431
ConfigPath: configInfo.Path,
433432
ConfigFormat: configInfo.Format,
434433
ConfigJSON: string(configBytes),
435-
IsWritable: configInfo.Writable,
436434
}
437435

438436
rw.Header().Set("Content-Type", "application/json")

src/webui/static/config.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ async function loadConfiguration() {
3030
currentConfigJSON = data.config_json;
3131
configMeta = {
3232
path: data.config_path,
33-
format: data.config_format,
34-
isWritable: data.is_writable
33+
format: data.config_format
3534
};
3635

3736
renderConfigEditor();
@@ -54,9 +53,6 @@ function renderConfigEditor() {
5453
<div class="config-meta">
5554
<span class="config-path" title="${configMeta.path}">${configMeta.path}</span>
5655
<span class="config-format ${configMeta.format}">${configMeta.format.toUpperCase()}</span>
57-
<span class="config-status ${configMeta.isWritable ? 'writable' : 'readonly'}">
58-
${configMeta.isWritable ? '✏️ <span data-key="editable">Редактируемый</span>' : '🔒 <span data-key="readonly">Только чтение</span>'}
59-
</span>
6056
</div>
6157
</div>
6258
</div>
@@ -76,14 +72,12 @@ function renderConfigEditor() {
7672
<div onclick="validateJSON()" class="action-btn">
7773
<span data-key="validate">Проверить</span>
7874
</div>
79-
${configMeta.isWritable ? `
80-
<div onclick="saveConfiguration()" class="action-btn">
81-
<span data-key="save_config">Сохранить</span>
82-
</div>
83-
<div onclick="saveAndRestartConfiguration()" class="action-btn">
84-
<span data-key="save_and_restart">Сохранить и перезапустить</span>
85-
</div>
86-
` : ''}
75+
<div onclick="saveConfiguration()" class="action-btn">
76+
<span data-key="save_config">Сохранить</span>
77+
</div>
78+
<div onclick="saveAndRestartConfiguration()" class="action-btn">
79+
<span data-key="save_and_restart">Сохранить и перезапустить</span>
80+
</div>
8781
</div>
8882
</div>
8983
</div>
@@ -93,7 +87,6 @@ function renderConfigEditor() {
9387
id="config-json-textarea"
9488
class="json-editor"
9589
spellcheck="false"
96-
${configMeta.isWritable ? '' : 'readonly'}
9790
placeholder="Загрузка конфигурации..."
9891
oninput="onConfigChange()"
9992
onscroll="syncLineNumbers()"

src/webui/static/style.css

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,9 +1542,6 @@ button[onclick="copyNodeKey()"]:hover {
15421542
}
15431543

15441544
.config-header {
1545-
display: flex;
1546-
justify-content: space-between;
1547-
align-items: flex-start;
15481545
margin-bottom: 30px;
15491546
padding: 20px;
15501547
background: var(--bg-info-card);
@@ -1564,13 +1561,6 @@ button[onclick="copyNodeKey()"]:hover {
15641561
font-size: 1.5em;
15651562
}
15661563

1567-
.config-meta {
1568-
display: flex;
1569-
gap: 15px;
1570-
flex-wrap: wrap;
1571-
align-items: center;
1572-
}
1573-
15741564
.config-path {
15751565
font-family: 'Courier New', monospace;
15761566
background: var(--bg-nav-item);
@@ -1602,25 +1592,6 @@ button[onclick="copyNodeKey()"]:hover {
16021592
color: #7b1fa2;
16031593
}
16041594

1605-
.config-status {
1606-
padding: 4px 8px;
1607-
border-radius: 4px;
1608-
font-size: 0.8em;
1609-
font-weight: bold;
1610-
}
1611-
1612-
.config-status.writable {
1613-
background: var(--bg-success);
1614-
color: var(--text-success);
1615-
}
1616-
1617-
.config-status.readonly {
1618-
background: var(--bg-warning);
1619-
color: var(--text-warning);
1620-
}
1621-
1622-
1623-
16241595
/* Configuration Groups */
16251596
.config-groups {
16261597
display: flex;

0 commit comments

Comments
 (0)