-
-
Notifications
You must be signed in to change notification settings - Fork 171
feat: add apistore #52
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
Merged
Merged
Changes from all commits
Commits
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
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,122 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/tidwall/gjson" | ||
|
|
||
| "github.com/mcp-ecosystem/mcp-gateway/internal/common/config" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // APIStore implements the Store interface using the remote http server | ||
| type APIStore struct { | ||
| logger *zap.Logger | ||
| url string | ||
| // read config from response(json body) using gjson | ||
| configJSONPath string | ||
| timeout int | ||
| } | ||
|
|
||
| var _ Store = (*APIStore)(nil) | ||
|
|
||
| // NewAPIStore creates a new api-based store | ||
| func NewAPIStore(logger *zap.Logger, url string, configJSONPath string, timeout int) (*APIStore, error) { | ||
| logger = logger.Named("mcp.store") | ||
|
|
||
| logger.Info("Using configuration url", zap.String("path", url)) | ||
|
|
||
| return &APIStore{ | ||
| logger: logger, | ||
| url: url, | ||
| configJSONPath: configJSONPath, | ||
| timeout: timeout, | ||
| }, nil | ||
| } | ||
|
|
||
| // Create implements Store.Create | ||
| func (s *APIStore) Create(_ context.Context, server *config.MCPConfig) error { | ||
| // only use for read config | ||
| return nil | ||
| } | ||
|
|
||
| // Get implements Store.Get | ||
| func (s *APIStore) Get(_ context.Context, name string) (*config.MCPConfig, error) { | ||
| jsonStr, err := s.request() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var config config.MCPConfig | ||
| err = json.Unmarshal([]byte(jsonStr), &config) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &config, nil | ||
| } | ||
|
|
||
| // List implements Store.List | ||
| func (s *APIStore) List(_ context.Context) ([]*config.MCPConfig, error) { | ||
| jsonStr, err := s.request() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var configs []*config.MCPConfig | ||
| err = json.Unmarshal([]byte(jsonStr), &configs) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return configs, nil | ||
| } | ||
|
|
||
| // Update implements Store.Update | ||
| func (s *APIStore) Update(_ context.Context, server *config.MCPConfig) error { | ||
| // only use for read config | ||
| return nil | ||
| } | ||
|
|
||
| // Delete implements Store.Delete | ||
| func (s *APIStore) Delete(_ context.Context, name string) error { | ||
| // only use for read config | ||
| return nil | ||
| } | ||
|
|
||
| func (s *APIStore) request() (string, error) { | ||
| client := &http.Client{ | ||
| Timeout: time.Duration(s.timeout) * time.Second, | ||
| } | ||
| resp, err := client.Get(s.url) | ||
| if err != nil { | ||
| s.logger.Error("failed to request url", | ||
| zap.String("url", s.url), | ||
| zap.Error(err)) | ||
| return "", err | ||
| } | ||
| defer resp.Body.Close() | ||
| body, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| s.logger.Error("failed to read response", | ||
| zap.String("url", s.url), | ||
| zap.Error(err)) | ||
| return "", err | ||
| } | ||
| jsonString := string(body) | ||
| s.logger.Debug("read storage api response", zap.String("body", jsonString)) | ||
| if s.configJSONPath == "" { | ||
| return jsonString, nil | ||
| } | ||
| result := gjson.Get(jsonString, s.configJSONPath) | ||
| if !result.Exists() { | ||
|
qiankunli marked this conversation as resolved.
|
||
| err = fmt.Errorf("configJSONPath is not in response: %s", s.configJSONPath) | ||
| s.logger.Error("configJSONPath is not in response", | ||
| zap.String("url", s.url), | ||
| zap.Error(err)) | ||
| return "", err | ||
| } | ||
| return result.Raw, 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Store methods for Create, Update, and Delete are no-ops.
Consider returning an explicit error (e.g., ErrNotSupported) to clearly indicate these operations aren’t supported.