Skip to content

Commit

Permalink
removing static files like html, css & js to go embed
Browse files Browse the repository at this point in the history
Signed-off-by: Raj Babu Das <[email protected]>
  • Loading branch information
imrajdas committed Aug 26, 2023
1 parent 6f8bfb4 commit 423679a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 64 deletions.
37 changes: 20 additions & 17 deletions pkg/diffr/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package diffr

import (
"fmt"
"github.com/imrajdas/diffr/static"
"html/template"
"net/http"
"os"
Expand Down Expand Up @@ -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)}

Expand Down Expand Up @@ -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()
}
17 changes: 0 additions & 17 deletions static/css/style.css

This file was deleted.

27 changes: 0 additions & 27 deletions static/js/script.js

This file was deleted.

56 changes: 53 additions & 3 deletions static/templates/template.html → static/static.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<!DOCTYPE html>
package static

// HTML is the template for the HTML page
const HTML = `<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
Expand All @@ -12,7 +15,25 @@
<!-- Include Bootstrap Icons (for GitHub icon) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
<!-- Include custom CSS -->
<link rel="stylesheet" type="text/css" href="/static/css/style.css" />
<style>
.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%;
}
</style>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</head>
Expand All @@ -35,6 +56,35 @@
<div id="myDiffElement" class="container-left"></div>
<div id="diff-data" data-diff="{{.Diff}}" hidden></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="/static/js/script.js"></script>
<script>
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');
});
});
</script>
</body>
</html>
`

0 comments on commit 423679a

Please sign in to comment.