From 5bb999afc8d5f9aa02ec5a4adc73f66aada137a0 Mon Sep 17 00:00:00 2001 From: Judah Nouriyelian Date: Fri, 18 Aug 2023 18:28:31 +0000 Subject: [PATCH 1/2] embed html files into go code --- cmd/gopogh-server/flake_chart.html | 35 ---------------- cmd/gopogh-server/main.go | 4 ++ .../handler/flake_chart.html | 42 +++++++++++++++++-- pkg/handler/handler.go | 10 +++++ 4 files changed, 53 insertions(+), 38 deletions(-) delete mode 100644 cmd/gopogh-server/flake_chart.html rename cmd/gopogh-server/flake_chart.js => pkg/handler/flake_chart.html (95%) diff --git a/cmd/gopogh-server/flake_chart.html b/cmd/gopogh-server/flake_chart.html deleted file mode 100644 index e963023..0000000 --- a/cmd/gopogh-server/flake_chart.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - -
-
- - - \ No newline at end of file diff --git a/cmd/gopogh-server/main.go b/cmd/gopogh-server/main.go index f87f649..2159033 100644 --- a/cmd/gopogh-server/main.go +++ b/cmd/gopogh-server/main.go @@ -1,6 +1,7 @@ package main import ( + _ "embed" "flag" "log" "net/http" @@ -31,6 +32,7 @@ func main() { Database: datab, } // Create an HTTP server and register the handlers + http.HandleFunc("/db", db.ServeEnvironmentTestsAndTestCases) http.HandleFunc("/env", db.ServeEnvCharts) @@ -41,6 +43,8 @@ func main() { http.HandleFunc("/version", handler.ServeGopoghVersion) + http.HandleFunc("/", handler.ServeHTML) + // Start the HTTP server err = http.ListenAndServe(":8080", nil) if err != nil { diff --git a/cmd/gopogh-server/flake_chart.js b/pkg/handler/flake_chart.html similarity index 95% rename from cmd/gopogh-server/flake_chart.js rename to pkg/handler/flake_chart.html index 192895d..25451af 100644 --- a/cmd/gopogh-server/flake_chart.js +++ b/pkg/handler/flake_chart.html @@ -1,4 +1,15 @@ -// Displays an error message to the UI. Any previous message will be erased. + + + + + + + + + + + + + + + +
+
+ + + \ No newline at end of file diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index b47a9a9..79ca4e5 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -1,6 +1,7 @@ package handler import ( + _ "embed" "encoding/json" "fmt" "net/http" @@ -14,6 +15,9 @@ type DB struct { Database db.Datab } +//go:embed flake_chart.html +var flakeChartHTML string + func (m *DB) ServeEnvironmentTestsAndTestCases(w http.ResponseWriter, _ *http.Request) { data, err := m.Database.GetEnvironmentTestsAndTestCases() if err != nil { @@ -158,3 +162,9 @@ func ServeGopoghVersion(w http.ResponseWriter, _ *http.Request) { return } } + +func ServeHTML(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Access-Control-Allow-Origin", "*") + fmt.Fprint(w, flakeChartHTML) +} From 2b5a367777462f84e15e365fb321b13a29edd9de Mon Sep 17 00:00:00 2001 From: Judah Nouriyelian Date: Fri, 18 Aug 2023 18:31:12 +0000 Subject: [PATCH 2/2] remove comment --- pkg/handler/flake_chart.html | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/handler/flake_chart.html b/pkg/handler/flake_chart.html index 25451af..56ed4f1 100644 --- a/pkg/handler/flake_chart.html +++ b/pkg/handler/flake_chart.html @@ -936,9 +936,4 @@
- \ No newline at end of file