style the root serve path

This commit is contained in:
raf 2023-12-01 21:36:03 +03:00
parent ac95f3b5db
commit b42501a4f3
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
3 changed files with 129 additions and 2 deletions

1
.gitignore vendored
View file

@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
echo
# Test binary, built with `go test -c`
*.test

33
main.go
View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
@ -11,6 +12,13 @@ import (
"github.com/joho/godotenv"
)
var version string
type PageData struct {
Files []string
Version string
}
func main() {
err := godotenv.Load()
if err != nil {
@ -33,8 +41,29 @@ func main() {
return
}
filePath := filepath.Join(basePath, r.URL.Path)
http.ServeFile(w, r, filePath)
if r.URL.Path == "/" {
files, err := os.ReadDir(basePath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fileNames := make([]string, len(files))
for i, file := range files {
fileNames[i] = file.Name()
}
data := PageData{
Files: fileNames,
Version: version,
}
tmpl := template.Must(template.ParseFiles("public/template.html"))
tmpl.Execute(w, data)
} else {
filePath := filepath.Join(basePath, r.URL.Path)
http.ServeFile(w, r, filePath)
}
})
port, _ := strconv.Atoi(serverPort)

97
public/template.html Normal file
View file

@ -0,0 +1,97 @@
<!doctype html>
<html>
<head>
<title>Directory Listing</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #1e1e2e;
color: #ffffff;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
}
ul {
list-style: none;
padding-left: 20px;
}
li {
margin-bottom: 5px;
cursor: pointer;
}
li:hover {
color: #00a8cc;
}
li.folder > ul {
display: none;
}
li.folder:hover > ul {
display: block;
}
a {
text-decoration: none;
color: #00ddff;
font-weight: bold;
}
a:hover {
color: #00ccff; /* darker shade on hover */
}
footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #313244;
color: #ffffff;
text-align: center;
padding: 10px 0;
font-size: 14px;
border-top: 1px solid #555677;
box-sizing: border-box;
}
footer > div {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
</style>
</head>
<body>
<h1>Directory Listing</h1>
<ul>
{{range .Files}}
<li><a href="{{.}}">{{.}}</a></li>
{{end}}
</ul>
<footer id="footer">Served by Echo {{.Version}}</footer>
<script>
document.addEventListener("DOMContentLoaded", function () {
const footer = document.getElementById("footer");
const version = footer.textContent.split(" ").pop();
const githubURL = `https://github.com/NotAShelf/Echo/tags/${version}`;
const link = document.createElement("a");
link.href = githubURL;
link.textContent = version;
footer.innerHTML = "Served by Echo ";
footer.appendChild(link);
});
</script>
</body>
</html>