Skip to content

Commit

Permalink
deprecate datasource.Serve and backend.Serve in favor of datasource.M…
Browse files Browse the repository at this point in the history
…anage and backend.Manage (#1018)
  • Loading branch information
marefr authored Jul 5, 2024
1 parent 689c0dc commit c7cc2af
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 122 deletions.
72 changes: 72 additions & 0 deletions backend/app/manage_example_test.go
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)
}
}
84 changes: 84 additions & 0 deletions backend/datasource/manage_example_test.go
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)
}
}
6 changes: 6 additions & 0 deletions backend/datasource/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

// ServeOpts options for serving a data source plugin.
//
// Deprecated: ServeOpts exists for historical compatibility
// and might be removed in a future version. Please migrate to use [Manage] instead of [Serve].
type ServeOpts struct {
// CheckHealthHandler handler for health checks.
// Optional to implement.
Expand All @@ -29,6 +32,9 @@ type ServeOpts struct {
}

// Serve starts serving the data source over gPRC.
//
// Deprecated: Serve exists for historical compatibility
// and might be removed in a future version. Please migrate to use [Manage] instead.
func Serve(opts ServeOpts) error {
return backend.Serve(backend.ServeOpts{
CheckHealthHandler: opts.CheckHealthHandler,
Expand Down
122 changes: 0 additions & 122 deletions backend/datasource/serve_example_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions backend/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func defaultGRPCMiddlewares(opts ServeOpts) []grpc.ServerOption {
}

// Serve starts serving the plugin over gRPC.
//
// Deprecated: Serve exists for historical compatibility
// and might be removed in a future version. Please migrate to use [Manage] instead.
func Serve(opts ServeOpts) error {
pluginOpts := GRPCServeOpts(opts)
pluginOpts.GRPCServer = func(customOptions []grpc.ServerOption) *grpc.Server {
Expand Down

0 comments on commit c7cc2af

Please sign in to comment.