Skip to content

Commit

Permalink
Use environment variables properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlewi committed Apr 22, 2024
1 parent a66aafc commit 80576ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
30 changes: 30 additions & 0 deletions app/pkg/logsviewer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ package logsviewer
import (
"context"
"encoding/json"
"github.com/go-logr/zapr"
"github.com/jlewi/foyle/app/api"
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/pkg/errors"
"go.uber.org/zap"
"io"
"net/http"
"strings"
)

var (
defaultClient *LogsClient
)

// LogsClient client for the logs service.
Expand All @@ -31,3 +40,24 @@ func (c *LogsClient) GetBlockLog(ctx context.Context, blockID string) (*api.Bloc
}
return block, nil
}

func GetClient() *LogsClient {
if defaultClient == nil {
log := zapr.NewLogger(zap.L())
// N.B. I think using EndpointEnvVar is better using Window().Location().Href because of how we would deal with
// Reverse proxies. If we're behind some sort of reverse proxy we'd probably want the server to set the
// appropriate baseURL
endpoint := app.Getenv(EndpointEnvVar)
if endpoint == "" {
log.Error(errors.New("EndpointEnvVar is not set"), "Failed to create logsclient")
}
if !strings.HasSuffix(endpoint, "/") {
endpoint = endpoint + "/"
}
log.Info("Creating logs client", "endpoint", endpoint)
defaultClient = &LogsClient{
Endpoint: endpoint,
}
}
return defaultClient
}
13 changes: 1 addition & 12 deletions app/pkg/logsviewer/selector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package logsviewer

import (
"github.com/go-logr/zapr"
"github.com/maxence-charriere/go-app/v9/pkg/app"
"go.uber.org/zap"
"strings"
)

Expand All @@ -30,23 +28,14 @@ func (h *blockSelector) Render() app.UI {
app.Button().
Text("Display").
OnClick(func(ctx app.Context, e app.Event) {
// Handle button click event here
// TODO(jeremy): Using os.Getenv is not working as a way of passing values from the server to the
// client.
// endpoint := os.Getenv(EndpointEnvVar)
endpoint := "http://localhost:8080/"
client := LogsClient{
Endpoint: endpoint,
}
client := GetClient()
blockID := app.Window().GetElementByID(blockInputID).Get("value").String()
blockID = strings.TrimSpace(blockID)
if blockID == "" {
h.traceValue = "No Block ID provided"
h.Update()
return
}
log := zapr.NewLogger(zap.L())
log.Info("Fetching block log", "blockID", blockID, "endpoint", endpoint)
blockLog, err := client.GetBlockLog(ctx, blockID)
if err != nil {
ctx.SetState(getErrorState, err.Error())
Expand Down

0 comments on commit 80576ff

Please sign in to comment.