-
-
Notifications
You must be signed in to change notification settings - Fork 75
feat(settings): persist extension runtime settings #637
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
Open
SantiagoDePolonia
wants to merge
3
commits into
main
Choose a base branch
from
feat/runtime-extension-settings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
68 changes: 0 additions & 68 deletions
68
internal/admin/dashboard/static/dist/assets/index-B-Rv4AUL.js
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...ard/static/dist/assets/index-Dv37Tmj5.css → ...ard/static/dist/assets/index-B_MQg975.css
Large diffs are not rendered by default.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
internal/admin/dashboard/static/dist/assets/index-D6HB9TkL.js
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,56 @@ | ||
| package admin | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/labstack/echo/v5" | ||
|
|
||
| "github.com/enterpilot/gomodel/ext" | ||
| "github.com/enterpilot/gomodel/internal/core" | ||
| "github.com/enterpilot/gomodel/internal/runtimesettings" | ||
| ) | ||
|
|
||
| type runtimeSettingsResponse struct { | ||
| Settings []ext.SettingDescriptor `json:"settings"` | ||
| } | ||
|
|
||
| type updateRuntimeSettingRequest struct { | ||
| Value string `json:"value"` | ||
| } | ||
|
|
||
| // RuntimeSettings lists extension-defined settings for the Dashboard. | ||
| func (h *Handler) RuntimeSettings(c *echo.Context) error { | ||
| settings := []ext.SettingDescriptor{} | ||
| if h.runtimeSettings != nil { | ||
| settings = h.runtimeSettings.List() | ||
| } | ||
| return c.JSON(http.StatusOK, runtimeSettingsResponse{Settings: settings}) | ||
| } | ||
|
|
||
| // UpdateRuntimeSetting validates and persists one extension-defined setting. | ||
| func (h *Handler) UpdateRuntimeSetting(c *echo.Context) error { | ||
| if h.runtimeSettings == nil { | ||
| return handleError(c, featureUnavailableError("runtime settings are unavailable")) | ||
| } | ||
| var req updateRuntimeSettingRequest | ||
| if err := c.Bind(&req); err != nil { | ||
| return handleError(c, core.NewInvalidRequestError("invalid request body: "+err.Error(), err)) | ||
| } | ||
| key := strings.TrimSpace(c.Param("key")) | ||
| setting, err := h.runtimeSettings.Update(c.Request().Context(), key, req.Value) | ||
| if err != nil { | ||
| switch { | ||
| case errors.Is(err, runtimesettings.ErrNotFound): | ||
| return handleError(c, core.NewNotFoundError("runtime setting not found").WithCode("runtime_setting_not_found")) | ||
| case errors.Is(err, runtimesettings.ErrLocked): | ||
| return handleError(c, core.NewInvalidRequestError("runtime setting is managed by an environment variable", err)) | ||
| case errors.Is(err, runtimesettings.ErrInvalid): | ||
| return handleError(c, core.NewInvalidRequestError(err.Error(), err)) | ||
| default: | ||
| return handleError(c, featureUnavailableError("failed to save runtime setting: "+err.Error())) | ||
| } | ||
| } | ||
| return c.JSON(http.StatusOK, setting) | ||
| } |
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,152 @@ | ||
| package admin | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/labstack/echo/v5" | ||
|
|
||
| "github.com/enterpilot/gomodel/ext" | ||
| "github.com/enterpilot/gomodel/internal/runtimesettings" | ||
| "github.com/enterpilot/gomodel/internal/storage" | ||
| ) | ||
|
|
||
| type adminTestRuntimeSetting struct { | ||
| mu sync.Mutex | ||
| value string | ||
| locked bool | ||
| } | ||
|
|
||
| func (s *adminTestRuntimeSetting) Descriptor() ext.SettingDescriptor { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| return ext.SettingDescriptor{ | ||
| Key: "pro.compression.level", | ||
| Label: "Prompt compression level", | ||
| Value: s.value, | ||
| Locked: s.locked, | ||
| Options: []ext.SettingOption{ | ||
| {Value: "none", Label: "None"}, | ||
| {Value: "high", Label: "High"}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (s *adminTestRuntimeSetting) Apply(value string) error { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| if value != "none" && value != "high" { | ||
| return fmt.Errorf("invalid level") | ||
| } | ||
| s.value = value | ||
| return nil | ||
| } | ||
|
|
||
| func (s *adminTestRuntimeSetting) currentValue() string { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| return s.value | ||
| } | ||
|
|
||
| func newAdminRuntimeSettingsService(t *testing.T, setting ext.RuntimeSetting) *runtimesettings.Service { | ||
| t.Helper() | ||
| backend, err := storage.NewSQLite(storage.SQLiteConfig{Path: filepath.Join(t.TempDir(), "admin-settings.db")}) | ||
| if err != nil { | ||
| t.Fatalf("open sqlite: %v", err) | ||
| } | ||
| t.Cleanup(func() { _ = backend.Close() }) | ||
| service, err := runtimesettings.New(context.Background(), backend, []ext.RuntimeSetting{setting}) | ||
| if err != nil { | ||
| t.Fatalf("create runtime settings service: %v", err) | ||
| } | ||
| t.Cleanup(func() { _ = service.Close() }) | ||
| return service | ||
| } | ||
|
|
||
| func runtimeSettingsRequest(e *echo.Echo, method, path, body string) *httptest.ResponseRecorder { | ||
| req := httptest.NewRequest(method, path, strings.NewReader(body)) | ||
| if body != "" { | ||
| req.Header.Set("Content-Type", "application/json") | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| e.ServeHTTP(rec, req) | ||
| return rec | ||
| } | ||
|
|
||
| func TestRuntimeSettingsListAndUpdate(t *testing.T) { | ||
| setting := &adminTestRuntimeSetting{value: "high"} | ||
| h := NewHandler(nil, nil, WithRuntimeSettings(newAdminRuntimeSettingsService(t, setting))) | ||
|
|
||
| e := echo.New() | ||
| h.RegisterRoutes(e.Group("/admin")) | ||
| listRec := runtimeSettingsRequest(e, http.MethodGet, "/admin/runtime/settings", "") | ||
| var list runtimeSettingsResponse | ||
| if err := json.Unmarshal(listRec.Body.Bytes(), &list); err != nil { | ||
| t.Fatalf("decode list: %v", err) | ||
| } | ||
| if len(list.Settings) != 1 || list.Settings[0].Value != "high" { | ||
| t.Fatalf("settings = %+v", list.Settings) | ||
| } | ||
|
|
||
| updateRec := runtimeSettingsRequest(e, http.MethodPut, "/admin/runtime/settings/pro.compression.level", `{"value":"none"}`) | ||
| if updateRec.Code != http.StatusOK || setting.currentValue() != "none" { | ||
| t.Fatalf("update status=%d value=%q body=%s", updateRec.Code, setting.currentValue(), updateRec.Body.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntimeSettingManagedByEnvironmentIsReadOnly(t *testing.T) { | ||
| setting := &adminTestRuntimeSetting{value: "high", locked: true} | ||
| h := NewHandler(nil, nil, WithRuntimeSettings(newAdminRuntimeSettingsService(t, setting))) | ||
|
|
||
| e := echo.New() | ||
| h.RegisterRoutes(e.Group("/admin")) | ||
| rec := runtimeSettingsRequest(e, http.MethodPut, "/admin/runtime/settings/pro.compression.level", `{"value":"none"}`) | ||
| if rec.Code != http.StatusBadRequest || setting.currentValue() != "high" { | ||
| t.Fatalf("locked update status=%d value=%q body=%s", rec.Code, setting.currentValue(), rec.Body.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestUpdateRuntimeSettingRejectsUnknownKeyAndInvalidValue(t *testing.T) { | ||
| setting := &adminTestRuntimeSetting{value: "high"} | ||
| h := NewHandler(nil, nil, WithRuntimeSettings(newAdminRuntimeSettingsService(t, setting))) | ||
| e := echo.New() | ||
| h.RegisterRoutes(e.Group("/admin")) | ||
|
|
||
| unknown := runtimeSettingsRequest(e, http.MethodPut, "/admin/runtime/settings/missing", `{"value":"none"}`) | ||
| if unknown.Code != http.StatusNotFound || !strings.Contains(unknown.Body.String(), `"code":"runtime_setting_not_found"`) { | ||
| t.Fatalf("unknown update status=%d body=%s", unknown.Code, unknown.Body.String()) | ||
| } | ||
| invalid := runtimeSettingsRequest(e, http.MethodPut, "/admin/runtime/settings/pro.compression.level", `{"value":"turbo"}`) | ||
| if invalid.Code != http.StatusBadRequest { | ||
| t.Fatalf("invalid update status=%d body=%s", invalid.Code, invalid.Body.String()) | ||
| } | ||
| if setting.currentValue() != "high" { | ||
| t.Fatalf("rejected updates changed value to %q", setting.currentValue()) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntimeSettingsWithoutRegisteredExtensions(t *testing.T) { | ||
| h := NewHandler(nil, nil) | ||
| e := echo.New() | ||
| h.RegisterRoutes(e.Group("/admin")) | ||
|
|
||
| list := runtimeSettingsRequest(e, http.MethodGet, "/admin/runtime/settings", "") | ||
| var response runtimeSettingsResponse | ||
| if err := json.Unmarshal(list.Body.Bytes(), &response); err != nil { | ||
| t.Fatalf("decode empty list: %v", err) | ||
| } | ||
| if list.Code != http.StatusOK || len(response.Settings) != 0 { | ||
| t.Fatalf("empty list status=%d body=%s", list.Code, list.Body.String()) | ||
| } | ||
| update := runtimeSettingsRequest(e, http.MethodPut, "/admin/runtime/settings/pro.compression.level", `{"value":"high"}`) | ||
| if update.Code != http.StatusServiceUnavailable || !strings.Contains(update.Body.String(), `"code":"feature_unavailable"`) { | ||
| t.Fatalf("unavailable update status=%d body=%s", update.Code, update.Body.String()) | ||
| } | ||
| } | ||
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.
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.