From 423679a459ef4bf64b0fef843dd8a0037bb0f351 Mon Sep 17 00:00:00 2001 From: Raj Babu Das Date: Sat, 26 Aug 2023 22:27:54 +0530 Subject: [PATCH] removing static files like html, css & js to go embed Signed-off-by: Raj Babu Das --- pkg/diffr/handler.go | 37 ++++++------ static/css/style.css | 17 ------ static/js/script.js | 27 --------- static/{templates/template.html => static.go} | 56 ++++++++++++++++++- 4 files changed, 73 insertions(+), 64 deletions(-) delete mode 100644 static/css/style.css delete mode 100644 static/js/script.js rename static/{templates/template.html => static.go} (57%) diff --git a/pkg/diffr/handler.go b/pkg/diffr/handler.go index 1e84b12..9870a3b 100644 --- a/pkg/diffr/handler.go +++ b/pkg/diffr/handler.go @@ -2,6 +2,7 @@ package diffr import ( "fmt" + "github.com/imrajdas/diffr/static" "html/template" "net/http" "os" @@ -49,7 +50,7 @@ func RunWebServer(cmd *cobra.Command, args []string) { serverURL := fmt.Sprintf("%s:%d", Address, Port) http.HandleFunc("/", handler) - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + //http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) server := &http.Server{Addr: fmt.Sprintf(":%d", Port)} @@ -119,22 +120,24 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Printf("Time taken to analyze all files: %s\n", elapsed) - tmpl, err := template.ParseFiles("static/templates/template.html") - if err != nil { - fmt.Printf("error: %v", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + wg.Add(1) + go func() { + defer wg.Done() - data := PageData{ - Title: "Diffr - A web-based content difference analyzer", - Diff: finalStr, - } + tmpl := template.Must(template.New("html").Parse(static.HTML)) - // Execute the template with the data and write the output to the response writer - err = tmpl.Execute(w, data) - if err != nil { - fmt.Printf("error: %v", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - } + data := PageData{ + Title: "Diffr - A web-based content difference analyzer", + Diff: finalStr, + } + + // Execute the templates with the provided data + err := tmpl.Execute(w, data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }() + + wg.Wait() } diff --git a/static/css/style.css b/static/css/style.css deleted file mode 100644 index e5e30bb..0000000 --- a/static/css/style.css +++ /dev/null @@ -1,17 +0,0 @@ -.navbar { - background-color: #010b18; /* Dark blue color */ -} -.navbar-brand { - color: #fff; - font-weight: bold; -} -.github-star { - font-size: 24px; - color: #fff; - margin-right: 20px; -} - -#myDiffElement { - margin-left: 10%; - margin-right: 10%; -} diff --git a/static/js/script.js b/static/js/script.js deleted file mode 100644 index ee0d30c..0000000 --- a/static/js/script.js +++ /dev/null @@ -1,27 +0,0 @@ -var diffString = document.getElementById("diff-data").getAttribute("data-diff"); -document.addEventListener('DOMContentLoaded', function () { - var targetElement = document.getElementById('myDiffElement'); - var configuration = { - drawFileList: true, - fileListToggle: true, - fileListStartVisible: true, - fileContentToggle: true, - matching: 'lines', - outputFormat: 'side-by-side', - synchronisedScroll: true, - highlight: true, - highlightLanguages: true, - renderNothingWhenEmpty: false, - }; - var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration); - diff2htmlUi.draw(); - diff2htmlUi.highlightCode(); - - // Dark mode toggle functionality - const darkModeToggle = document.getElementById('darkModeToggle'); - const body = document.body; - - darkModeToggle.addEventListener('click', () => { - body.classList.toggle('dark-mode'); - }); -}); diff --git a/static/templates/template.html b/static/static.go similarity index 57% rename from static/templates/template.html rename to static/static.go index 1544f26..5550879 100644 --- a/static/templates/template.html +++ b/static/static.go @@ -1,4 +1,7 @@ - +package static + +// HTML is the template for the HTML page +const HTML = ` {{.Title}} @@ -12,7 +15,25 @@ - + @@ -35,6 +56,35 @@
- + +`