-
Notifications
You must be signed in to change notification settings - Fork 216
Add a config editor page #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // WriteFileAtomic writes data to path atomically. | ||
| // It writes to a temporary file in the same directory, fsyncs it, | ||
| // sets the desired mode, closes it, fsyncs the directory (best-effort), | ||
| // then renames over the destination. | ||
| // | ||
| // Mode preservation: | ||
| // - If the destination path already exists, its mode is preserved regardless of the provided mode. | ||
| // - If the destination does not exist and the provided mode is non-zero, that mode is used. | ||
| // - Otherwise, 0644 is used. | ||
| func WriteFileAtomic(path string, data []byte, mode os.FileMode) error { | ||
| dir := filepath.Dir(path) | ||
|
|
||
| // Determine effective mode | ||
| effectiveMode := mode | ||
| if fi, err := os.Stat(path); err == nil { | ||
| effectiveMode = fi.Mode() | ||
| } else { | ||
| if effectiveMode == 0 { | ||
| effectiveMode = 0o644 | ||
| } | ||
| } | ||
|
|
||
| // Create a temp file in the same directory for atomic rename | ||
| tmpFile, err := os.CreateTemp(dir, ".tmp-config-*.yaml") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tmpName := tmpFile.Name() | ||
| cleanup := func() { | ||
| _ = tmpFile.Close() | ||
| _ = os.Remove(tmpName) | ||
| } | ||
|
|
||
| // Ensure cleanup on any failure path | ||
| defer func() { | ||
| // If tmpFile still exists (e.g., rename failed), best-effort remove it | ||
| _ = os.Remove(tmpName) | ||
| }() | ||
|
|
||
| // Write data | ||
| if _, err = tmpFile.Write(data); err != nil { | ||
| cleanup() | ||
| return err | ||
| } | ||
|
|
||
| // Flush file contents | ||
| if err = tmpFile.Sync(); err != nil { | ||
| cleanup() | ||
| return err | ||
| } | ||
|
|
||
| // Set mode | ||
| if err = tmpFile.Chmod(effectiveMode); err != nil { | ||
| cleanup() | ||
| return err | ||
| } | ||
|
|
||
| // Close the temp file before rename | ||
| if err = tmpFile.Close(); err != nil { | ||
| cleanup() | ||
| return err | ||
| } | ||
|
|
||
| // Best-effort fsync the directory before rename (not strictly required) | ||
| if dirFD, err := os.Open(dir); err == nil { | ||
| _ = dirFD.Sync() | ||
| _ = dirFD.Close() | ||
| } | ||
|
|
||
| // Atomic rename | ||
| if err = os.Rename(tmpName, path); err != nil { | ||
| // best-effort cleanup | ||
| _ = os.Remove(tmpName) | ||
| return err | ||
| } | ||
|
|
||
| // Best-effort fsync the directory after rename to strengthen durability | ||
| if dirFD, err := os.Open(dir); err == nil { | ||
| _ = dirFD.Sync() | ||
| _ = dirFD.Close() | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/mostlygeek/llama-swap/event" | ||
| ) | ||
|
|
||
| // apiGetConfig returns the raw YAML configuration file contents. | ||
| func (pm *ProxyManager) apiGetConfig(c *gin.Context) { | ||
| data, err := os.ReadFile(pm.configPath) | ||
| if err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| return | ||
| } | ||
| c.Data(http.StatusOK, "text/plain; charset=utf-8", data) | ||
| } | ||
|
|
||
| // apiPutConfig validates and atomically writes the configuration file. | ||
| // It triggers reload behavior based on the watchConfigEnabled setting. | ||
| func (pm *ProxyManager) apiPutConfig(c *gin.Context) { | ||
| // Read entire body as text (accept text/plain or application/x-yaml, but don't hard fail on content-type) | ||
| body, err := io.ReadAll(c.Request.Body) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"}) | ||
| return | ||
| } | ||
|
|
||
| // Validate YAML using existing loader | ||
| if _, err := LoadConfigFromReader(bytes.NewReader(body)); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| // Preserve existing file mode when possible; default to 0644 | ||
| var mode os.FileMode = 0o644 | ||
| if fi, err := os.Stat(pm.configPath); err == nil { | ||
| mode = fi.Mode() | ||
| } | ||
|
|
||
| // Atomic write | ||
| if err := WriteFileAtomic(pm.configPath, body, mode); err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to write config file"}) | ||
| return | ||
| } | ||
|
|
||
| // Trigger reload based on watch behavior | ||
| if pm.watchConfigEnabled { | ||
| // Do not call reloadCallback; fsnotify watcher will emit start event and handle reload | ||
| } else { | ||
| // Emit start event then call reload callback | ||
| event.Emit(ConfigFileChangedEvent{ | ||
| ReloadingState: ReloadingStateStart, | ||
| }) | ||
| if pm.reloadCallback != nil { | ||
| pm.reloadCallback() | ||
| } | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{"msg": "ok"}) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.