-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deprecate datasource.Serve and backend.Serve in favor of datasource.M…
…anage and backend.Manage (#1018)
- Loading branch information
Showing
5 changed files
with
165 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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,72 @@ | ||
package app_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/app" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter" | ||
) | ||
|
||
type testApp struct { | ||
httpClient *http.Client | ||
backend.CallResourceHandler | ||
} | ||
|
||
func newApp(ctx context.Context, settings backend.AppInstanceSettings) (instancemgmt.Instance, error) { | ||
opts, err := settings.HTTPClientOptions(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := httpclient.New(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
app := &testApp{ | ||
httpClient: client, | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/test", app.handleTest) | ||
app.CallResourceHandler = httpadapter.New(mux) | ||
|
||
return app, nil | ||
} | ||
|
||
func (ds *testApp) Dispose() { | ||
// Cleanup | ||
} | ||
|
||
func (ds *testApp) CheckHealth(_ context.Context, _ *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { | ||
// Handle request | ||
resp, err := ds.httpClient.Get("http://") | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp.Body.Close() | ||
return nil, nil | ||
} | ||
|
||
func (ds *testApp) handleTest(rw http.ResponseWriter, _ *http.Request) { | ||
// Handle request | ||
resp, err := ds.httpClient.Get("http://") | ||
if err != nil { | ||
rw.WriteHeader(500) | ||
return | ||
} | ||
resp.Body.Close() | ||
} | ||
|
||
func Example() { | ||
err := app.Manage("myapp-plugin-id", newApp, app.ManageOpts{}) | ||
if err != nil { | ||
backend.Logger.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
This file contains 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,84 @@ | ||
package datasource_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter" | ||
) | ||
|
||
type testDataSource struct { | ||
httpClient *http.Client | ||
backend.CallResourceHandler | ||
} | ||
|
||
func newDataSource(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { | ||
opts, err := settings.HTTPClientOptions(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := httpclient.New(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ds := &testDataSource{ | ||
httpClient: client, | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/test", ds.handleTest) | ||
ds.CallResourceHandler = httpadapter.New(mux) | ||
|
||
return ds, nil | ||
} | ||
|
||
func (ds *testDataSource) Dispose() { | ||
// Cleanup | ||
} | ||
|
||
func (ds *testDataSource) CheckHealth(_ context.Context, _ *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { | ||
// Handle request | ||
resp, err := ds.httpClient.Get("http://") | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp.Body.Close() | ||
return nil, nil | ||
} | ||
|
||
func (ds *testDataSource) QueryData(_ context.Context, _ *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { | ||
var resp *backend.QueryDataResponse | ||
// Handle request | ||
httpResp, err := ds.httpClient.Get("http://") | ||
if err != nil { | ||
return nil, err | ||
} | ||
httpResp.Body.Close() | ||
|
||
return resp, err | ||
} | ||
|
||
func (ds *testDataSource) handleTest(rw http.ResponseWriter, _ *http.Request) { | ||
// Handle request | ||
resp, err := ds.httpClient.Get("http://") | ||
if err != nil { | ||
rw.WriteHeader(500) | ||
return | ||
} | ||
resp.Body.Close() | ||
} | ||
|
||
func Example() { | ||
err := datasource.Manage("myds-plugin-id", newDataSource, datasource.ManageOpts{}) | ||
if err != nil { | ||
backend.Logger.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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