app

package
v0.257.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2024 License: Apache-2.0 Imports: 11 Imported by: 8

Documentation

Overview

Package app provides utilities for creating and serving a app plugin over gRPC.

Example
package main

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 main() {
	err := app.Manage("myapp-plugin-id", newApp, app.ManageOpts{})
	if err != nil {
		backend.Logger.Error(err.Error())
		os.Exit(1)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Manage

func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpts) error

Manage starts serving the app over gPRC with automatic instance management. pluginID should match the one from plugin.json.

func NewInstanceManager

func NewInstanceManager(fn InstanceFactoryFunc) instancemgmt.InstanceManager

NewInstanceManager creates a new app instance manager,

This is a helper method for calling NewInstanceProvider and creating a new instancemgmt.InstanceProvider, and providing that to instancemgmt.New.

func NewInstanceProvider

func NewInstanceProvider(fn InstanceFactoryFunc) instancemgmt.InstanceProvider

NewInstanceProvider create a new app instance provider,

The instance provider is responsible for providing cache keys for application instances, creating new instances when needed and invalidating cached instances when they have been updated in Grafana. Cache key is based on the app plugin identifier, and the numeric Grafana organization ID. If fn is nil, NewInstanceProvider panics.

Types

type InstanceFactoryFunc

type InstanceFactoryFunc func(ctx context.Context, settings backend.AppInstanceSettings) (instancemgmt.Instance, error)

InstanceFactoryFunc factory method for creating app instances.

type ManageOpts

type ManageOpts struct {
	// GRPCSettings settings for gPRC.
	GRPCSettings backend.GRPCSettings

	// TracingOpts contains settings for tracing setup.
	TracingOpts tracing.Opts

	// Stateless admission handler
	AdmissionHandler backend.AdmissionHandler

	// Stateless conversion handler
	ConversionHandler backend.ConversionHandler
}

ManageOpts can modify Manage behavior.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL