Skip to content

Commit

Permalink
Refactor HTTP server into dedicated package
Browse files Browse the repository at this point in the history
Move the HTTP server logic from cmd/honey-tracker/main.go to a new web package. This improves code organization and separation of concerns by isolating web-specific functionality.
  • Loading branch information
billettc committed Sep 13, 2024
1 parent ae45ca1 commit 6e070f4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 100 deletions.
88 changes: 3 additions & 85 deletions cmd/honey-tracker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package main
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"time"

"github.com/golang-jwt/jwt/v5"
"github.com/streamingfast/honey-tracker/web"

"github.com/spf13/cobra"
"github.com/streamingfast/bstream"
"github.com/streamingfast/cli/sflags"
Expand Down Expand Up @@ -135,7 +133,7 @@ func rootRun(cmd *cobra.Command, args []string) error {
}()

go func() {
serveHttp()
web.ServeHttp()
}()

err = sinker.Run(ctx)
Expand Down Expand Up @@ -193,86 +191,6 @@ func trackPrice(db *data.Psql, logger *zap.Logger) error {
}
}

const METABASE_SITE_URL = "http://34.170.245.114:3000"

var METABASE_SECRET_KEY = os.Getenv("SECRET_KEY")

type CustomClaims struct {
Resource map[string]int `json:"resource"`
Params map[string]interface{} `json:"params"`
jwt.RegisteredClaims
}

type PageData struct {
IFrameUrl string
}

func handler(w http.ResponseWriter, r *http.Request) {

claims := CustomClaims{
Resource: map[string]int{"dashboard": 1},
Params: map[string]interface{}{},
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 10)),
},
}

// 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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("error: %v\n", err)
return
}

iframeUrl := METABASE_SITE_URL + "/embed/dashboard/" + tokenString + "#bordered=true&titled=true"

tmplData := PageData{
IFrameUrl: iframeUrl,
}

err = tmpl.Execute(w, tmplData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("error: %v\n", err)
return
}
}

const tmpl = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hivemaper Dashboard</title>
</head>
<body>
<iframe
src="{{.IFrameUrl}}"
frameborder="0"
width="90%"
height="90%"
allowtransparency
></iframe></body>
</html>
`

func serveHttp() {
http.HandleFunc("/", handler)
log.Println("Starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

func checkError(err error) {
if err != nil {
panic(err)
Expand Down
7 changes: 7 additions & 0 deletions cmd/www/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/streamingfast/honey-tracker/web"

func main() {
web.ServeHttp()
}
15 changes: 0 additions & 15 deletions web/index.html

This file was deleted.

115 changes: 115 additions & 0 deletions web/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package web

import (
"fmt"
"html/template"
"log"
"net/http"
"os"

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

const METABASE_SITE_URL = "http://34.170.245.114:3000"

var METABASE_SECRET_KEY = os.Getenv("SECRET_KEY")

type CustomClaims struct {
Resource map[string]int `json:"resource"`
Params map[string]interface{} `json:"params"`
jwt.RegisteredClaims
}

type PageData struct {
IFrameUrl string
}

func handler(w http.ResponseWriter, r *http.Request) {

//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)
//
//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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("error: %v\n", err)
return
}

//iframeUrl := METABASE_SITE_URL + "/embed/dashboard/" + tokenString + "#bordered=true&titled=true"
//
tmplData := PageData{
//IFrameUrl: iframeUrl,
}

err = tmpl.Execute(w, tmplData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("error: %v\n", err)
return
}
}

const tmpl = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hivemaper Dashboard</title>
</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="http://metabase.streamingfast.io/public/dashboard/3e029abe-66bf-4cad-895c-e39922f03927"
frameborder="0"
width="100%"
height="100%"
allow="fullscreen"
></iframe>
</body>
</html>
`

//const tmpl = `
//<!DOCTYPE html>
//<html lang="en">
//<head>
// <meta charset="UTF-8">
// <title>Hivemaper Dashboard</title>
//</head>
//<body>
//<iframe
// src="{{.IFrameUrl}}"
// frameborder="0"
// width="90%"
// height="90%"
// allowtransparency
//></iframe></body>
//</html>
//`

func ServeHttp() {
http.HandleFunc("/", handler)
log.Println("Starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

0 comments on commit 6e070f4

Please sign in to comment.