Skip to content

Commit

Permalink
add header to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Sep 16, 2024
1 parent 84e4041 commit 2d46b5f
Showing 1 changed file with 85 additions and 35 deletions.
120 changes: 85 additions & 35 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/golang-jwt/jwt/v5"
)

const METABASE_SITE_URL = "http://34.170.245.114:3000"
const METABASE_SITE_URL = "http://metabase.streamingfast.io"

var METABASE_SECRET_KEY = os.Getenv("SECRET_KEY")

Expand All @@ -25,33 +27,41 @@ type PageData struct {
}

func handler(w http.ResponseWriter, r *http.Request) {
dashboard := 1
dashboardString := r.URL.Query().Get("dashboard")
if dashboardString != "" {
d, err := strconv.Atoi(dashboardString)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("error: %v\n", err)
return
}
dashboard = d

iframeURL := r.URL.Query().Get("iframeUrl")
if iframeURL == "" {
iframeURL = "http://metabase.streamingfast.io/public/dashboard/3e029abe-66bf-4cad-895c-e39922f03927"
}
//claims := CustomClaims{
// Resource: map[string]int{"dashboard": 1},
// Params: map[string]interface{}{},
// RegisteredClaims: jwt.RegisteredClaims{
// ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 10)),
// },
//}
//
//if METABASE_SECRET_KEY == "" {
// panic("METABASE_SECRET_KEY not set")
//}
//fmt.Println("METABASE_SECRET_KEY: " + METABASE_SECRET_KEY)
//// Create a new token object
//token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
//secretKey := []byte(METABASE_SECRET_KEY)

claims := CustomClaims{
Resource: map[string]int{"dashboard": dashboard},
Params: map[string]interface{}{},
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 10)),
},
}
//
//tokenString, err := token.SignedString(secretKey)
//if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// fmt.Printf("error: %v\n", err)
// return
//}
if METABASE_SECRET_KEY == "" {
panic("METABASE_SECRET_KEY not set")
}
fmt.Println("METABASE_SECRET_KEY: " + METABASE_SECRET_KEY)
// Create a new token object
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
secretKey := []byte(METABASE_SECRET_KEY)

tokenString, err := token.SignedString(secretKey)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("error: %v\n", err)
return
}

tmpl, err := template.New("webpage").Parse(tmpl)
if err != nil {
Expand All @@ -60,8 +70,9 @@ func handler(w http.ResponseWriter, r *http.Request) {
return
}

//iframeUrl := METABASE_SITE_URL + "/embed/dashboard/" + tokenString + "#bordered=true&titled=true"
//
iframeURL := METABASE_SITE_URL + "/embed/dashboard/" + tokenString + "#bordered=false&titled=false"
fmt.Println("iframeURL: " + iframeURL)

tmplData := PageData{
IFrameUrl: iframeURL,
}
Expand All @@ -72,6 +83,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("error: %v\n", err)
return
}
fmt.Println("Done rendering")
}

const tmpl = `
Expand All @@ -80,20 +92,58 @@ const tmpl = `
<head>
<meta charset="UTF-8">
<title>Hivemaper Dashboard</title>
<style>
body, html {width: 100%; height: 100%; margin: 0; padding: 0}
.row-container {display: flex; width: 100%; height: 100%; flex-direction: column; background-color: white; overflow: hidden;}
.first-row {}
.second-row { flex-grow: 1; border: none; margin: 0; padding: 0; }
.round-button {
padding: 10px 20px;
margin: 10px;
border-radius: 25px;
border: none;
background-color: #679BDF; /* Green background */
color: white;
cursor: pointer;
font-size: 16px;
text-decoration: none;
display: inline-block;
}
.round-button:hover {
background-color: #45a049; /* Darker green on hover */
}
</style>
</head>
<body>
<iframe
style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"
src="{{ .IFrameUrl }}"
frameborder="0"
width="100%"
height="100%"
allow="fullscreen"
></iframe>
<div class="row-container">
<div class="first-row">
<img src="https://cdn.prod.website-files.com/649aef4c9068091b737a9baf/64c3e6ca809ba2f4f2d76359_streamingfast-p-500.png" width=150 height=63></img>
<span>Hivemaper Dashboard</span>
<button class="round-button" onclick="window.location.href='./'">Overview</button>
<button class="round-button" onclick="window.location.href='./?dashboard=33'">Fleet Dashboard</button>
<button class="round-button" onclick="window.location.href='./?dashboard=34'">Driver Dashboard</button>
</div>
<iframe
class="second-row"
src="{{ .IFrameUrl }}"
sandbox="allow-scripts allow-same-origin"
frameborder="0"
width="100%"
height="100%"
allow="fullscreen"
></iframe>
</div>
</body>
</html>
`

//style="position:fixed; top:0px; left:0; bottom:100; right:0; width:100%; height:100%; border:none; margin-top:100px; padding:0; overflow:hidden; z-index:999999;"
//const tmpl = `
//<!DOCTYPE html>
//<html lang="en">
Expand Down

0 comments on commit 2d46b5f

Please sign in to comment.