From 6e070f49ee1e7dbf4dbd6a10f831f979ea9b9518 Mon Sep 17 00:00:00 2001 From: Charles Billette Date: Fri, 13 Sep 2024 16:42:07 -0400 Subject: [PATCH] Refactor HTTP server into dedicated package 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. --- cmd/honey-tracker/main.go | 88 +---------------------------- cmd/www/main.go | 7 +++ web/index.html | 15 ----- web/server.go | 115 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 100 deletions(-) create mode 100644 cmd/www/main.go delete mode 100644 web/index.html create mode 100644 web/server.go diff --git a/cmd/honey-tracker/main.go b/cmd/honey-tracker/main.go index 8926fbb..45325e2 100644 --- a/cmd/honey-tracker/main.go +++ b/cmd/honey-tracker/main.go @@ -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" @@ -135,7 +133,7 @@ func rootRun(cmd *cobra.Command, args []string) error { }() go func() { - serveHttp() + web.ServeHttp() }() err = sinker.Run(ctx) @@ -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 = ` - - - - - Hivemaper Dashboard - - - - -` - -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) diff --git a/cmd/www/main.go b/cmd/www/main.go new file mode 100644 index 0000000..3067d44 --- /dev/null +++ b/cmd/www/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/streamingfast/honey-tracker/web" + +func main() { + web.ServeHttp() +} diff --git a/web/index.html b/web/index.html deleted file mode 100644 index 40afa0d..0000000 --- a/web/index.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/web/server.go b/web/server.go new file mode 100644 index 0000000..d69c83a --- /dev/null +++ b/web/server.go @@ -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 = ` + + + + + Hivemaper Dashboard + + + + + +` + +//const tmpl = ` +// +// +// +// +// Hivemaper Dashboard +// +// +// +// +//` + +func ServeHttp() { + http.HandleFunc("/", handler) + log.Println("Starting server on :8080") + log.Fatal(http.ListenAndServe(":8080", nil)) +}