Skip to content

Commit

Permalink
refactor: Look for appUrl and data paths from env vars
Browse files Browse the repository at this point in the history
* This is handy when customizations are made in on-premise deployments

Signed-off-by: Mahendra Paipuri <mahendra.paipuri@gmail.com>
  • Loading branch information
mahendrapaipuri committed Feb 2, 2024
1 parent e0f1dac commit d3096bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/plugin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

Expand All @@ -17,6 +18,7 @@ import (
"github.com/spf13/afero"
)

const GF_PATHS_DATA = "/var/lib/grafana"
const PLUGIN_NAME = "mahendrapaipuri-dashboardreporter-app"

// Make sure App implements required interfaces. This is important to do
Expand Down Expand Up @@ -44,6 +46,7 @@ type App struct {
httpClient *http.Client
grafanaAppUrl string
config *Config
pluginDir string
newGrafanaClient func(client *http.Client, grafanaAppURL string, cookie string, variables url.Values, layout string) GrafanaClient
newReport func(logger log.Logger, grafanaClient GrafanaClient, config *ReportConfig) (Report, error)
}
Expand Down Expand Up @@ -103,9 +106,9 @@ func NewApp(ctx context.Context, settings backend.AppInstanceSettings) (instance
// Ref: https://github.com/grafana/plugin-validator/blob/eb71abbbead549fd7697371b25c226faba19b252/pkg/analysis/passes/coderules/semgrep-rules.yaml#L13-L28
//
// If appURL is not found in plugin settings attempt to get it from env var
// if grafanaAppUrl == "" && os.Getenv("GF_ENTERPRISE_APP_URL") != "" {
// grafanaAppUrl = strings.TrimRight(os.Getenv("GF_ENTERPRISE_APP_URL"), "/")
// }
if grafanaAppUrl == "" && os.Getenv("GF_APP_URL") != "" {
grafanaAppUrl = strings.TrimRight(os.Getenv("GF_APP_URL"), "/")
}

if grafanaAppUrl == "" {
return nil, fmt.Errorf("Grafana app URL not configured in JSONData")
Expand All @@ -126,7 +129,12 @@ func NewApp(ctx context.Context, settings backend.AppInstanceSettings) (instance
grafana-image-renderer which indirectly depends on chromium, we can leverage the
existing chromoium to generate PDFs instead of relying on pdflatex.
*/
pluginDir := filepath.Join("/var/lib/grafana/plugins", PLUGIN_NAME)
var pluginDir string
if os.Getenv("GF_PATHS_DATA") != "" {
pluginDir = filepath.Join(os.Getenv("GF_PATHS_DATA"), "plugins", PLUGIN_NAME)
} else {
pluginDir = filepath.Join(GF_PATHS_DATA, "plugins", PLUGIN_NAME)
}
vfs := afero.NewBasePathFs(afero.NewOsFs(), pluginDir).(*afero.BasePathFs)

// Create a staging dir inside this plugin folder
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (m mockReport) Title() string { return "title" }
func TestReportResource(t *testing.T) {
// Set appURL env variable
t.Setenv("GF_APP_URL", "http://localhost:3000")
t.Setenv("GF_PATHS_DATA", t.TempDir())

// Initialize app
inst, err := NewApp(context.Background(), backend.AppInstanceSettings{})
Expand Down

0 comments on commit d3096bb

Please sign in to comment.