Skip to content

Commit 423679a

Browse files
committed
removing static files like html, css & js to go embed
Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com>
1 parent 6f8bfb4 commit 423679a

File tree

4 files changed

+73
-64
lines changed

4 files changed

+73
-64
lines changed

pkg/diffr/handler.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package diffr
22

33
import (
44
"fmt"
5+
"github.com/imrajdas/diffr/static"
56
"html/template"
67
"net/http"
78
"os"
@@ -49,7 +50,7 @@ func RunWebServer(cmd *cobra.Command, args []string) {
4950
serverURL := fmt.Sprintf("%s:%d", Address, Port)
5051

5152
http.HandleFunc("/", handler)
52-
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
53+
//http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
5354

5455
server := &http.Server{Addr: fmt.Sprintf(":%d", Port)}
5556

@@ -119,22 +120,24 @@ func handler(w http.ResponseWriter, r *http.Request) {
119120

120121
fmt.Printf("Time taken to analyze all files: %s\n", elapsed)
121122

122-
tmpl, err := template.ParseFiles("static/templates/template.html")
123-
if err != nil {
124-
fmt.Printf("error: %v", err)
125-
http.Error(w, err.Error(), http.StatusInternalServerError)
126-
return
127-
}
123+
wg.Add(1)
124+
go func() {
125+
defer wg.Done()
128126

129-
data := PageData{
130-
Title: "Diffr - A web-based content difference analyzer",
131-
Diff: finalStr,
132-
}
127+
tmpl := template.Must(template.New("html").Parse(static.HTML))
133128

134-
// Execute the template with the data and write the output to the response writer
135-
err = tmpl.Execute(w, data)
136-
if err != nil {
137-
fmt.Printf("error: %v", err)
138-
http.Error(w, err.Error(), http.StatusInternalServerError)
139-
}
129+
data := PageData{
130+
Title: "Diffr - A web-based content difference analyzer",
131+
Diff: finalStr,
132+
}
133+
134+
// Execute the templates with the provided data
135+
err := tmpl.Execute(w, data)
136+
if err != nil {
137+
http.Error(w, err.Error(), http.StatusInternalServerError)
138+
return
139+
}
140+
}()
141+
142+
wg.Wait()
140143
}

static/css/style.css

Lines changed: 0 additions & 17 deletions
This file was deleted.

static/js/script.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

static/templates/template.html renamed to static/static.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<!DOCTYPE html>
1+
package static
2+
3+
// HTML is the template for the HTML page
4+
const HTML = `<!DOCTYPE html>
25
<html>
36
<head>
47
<title>{{.Title}}</title>
@@ -12,7 +15,25 @@
1215
<!-- Include Bootstrap Icons (for GitHub icon) -->
1316
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
1417
<!-- Include custom CSS -->
15-
<link rel="stylesheet" type="text/css" href="/static/css/style.css" />
18+
<style>
19+
.navbar {
20+
background-color: #010b18; /* Dark blue color */
21+
}
22+
.navbar-brand {
23+
color: #fff;
24+
font-weight: bold;
25+
}
26+
.github-star {
27+
font-size: 24px;
28+
color: #fff;
29+
margin-right: 20px;
30+
}
31+
32+
#myDiffElement {
33+
margin-left: 10%;
34+
margin-right: 10%;
35+
}
36+
</style>
1637
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
1738
<script async defer src="https://buttons.github.io/buttons.js"></script>
1839
</head>
@@ -35,6 +56,35 @@
3556
<div id="myDiffElement" class="container-left"></div>
3657
<div id="diff-data" data-diff="{{.Diff}}" hidden></div>
3758
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
38-
<script src="/static/js/script.js"></script>
59+
<script>
60+
var diffString = document.getElementById("diff-data").getAttribute("data-diff");
61+
document.addEventListener('DOMContentLoaded', function () {
62+
var targetElement = document.getElementById('myDiffElement');
63+
var configuration = {
64+
drawFileList: true,
65+
fileListToggle: true,
66+
fileListStartVisible: true,
67+
fileContentToggle: true,
68+
matching: 'lines',
69+
outputFormat: 'side-by-side',
70+
synchronisedScroll: true,
71+
highlight: true,
72+
highlightLanguages: true,
73+
renderNothingWhenEmpty: false,
74+
};
75+
var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
76+
diff2htmlUi.draw();
77+
diff2htmlUi.highlightCode();
78+
79+
// Dark mode toggle functionality
80+
const darkModeToggle = document.getElementById('darkModeToggle');
81+
const body = document.body;
82+
83+
darkModeToggle.addEventListener('click', () => {
84+
body.classList.toggle('dark-mode');
85+
});
86+
});
87+
</script>
3988
</body>
4089
</html>
90+
`

0 commit comments

Comments
 (0)