use go html templates
This commit is contained in:
parent
1ff61e9b8f
commit
59230b7ad5
3 changed files with 142 additions and 80 deletions
|
@ -17,6 +17,6 @@ indir [--host address] [--port port] [--root http_root] directory
|
|||
```
|
||||
|
||||
## to-do:
|
||||
- [ ] use templates instead of hard-coded HTML (i was lazy)
|
||||
- [x] use templates instead of hard-coded HTML (i was lazy)
|
||||
- [ ] directory header from readme file
|
||||
- [ ] directory stylesheet overrides
|
||||
|
|
129
main.go
129
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
@ -13,6 +14,22 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
Directory struct {
|
||||
Name string
|
||||
Root bool
|
||||
Files []*File
|
||||
}
|
||||
|
||||
File struct {
|
||||
Name string
|
||||
URI string
|
||||
IsDir bool
|
||||
Size string
|
||||
ModifiedDate string
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 { printHelp() }
|
||||
|
||||
|
@ -72,47 +89,12 @@ func main() {
|
|||
".DS_Store",
|
||||
}
|
||||
|
||||
stylesheet := `
|
||||
html {
|
||||
background: #101010;
|
||||
color: #f0f0f0;
|
||||
font-family: 'Monaspace Argon', monospace;
|
||||
dirTemplate, err := template.ParseGlob("./templates/dir.html")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to parse directory template: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
body {
|
||||
width: min(calc(100% - 1em), 1000px);
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
tr:hover {
|
||||
background-color: #80808040;
|
||||
}
|
||||
|
||||
th {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
td {
|
||||
padding: .2em 0;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #b7fd49;
|
||||
}
|
||||
a:hover {
|
||||
color: white;
|
||||
}
|
||||
|
||||
footer {
|
||||
padding: 1em 0;
|
||||
}
|
||||
`
|
||||
|
||||
fmt.Printf("Now hosting \"%s\" at http://%s:%d", filesDir, host, port)
|
||||
if root != "/" { fmt.Printf("%s", root) }
|
||||
fmt.Println(".")
|
||||
|
@ -124,25 +106,6 @@ func main() {
|
|||
}
|
||||
isRoot := r.URL.Path == root
|
||||
|
||||
responseText := fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html lang="en-IE">
|
||||
<head>
|
||||
<title>Listing %s</title>
|
||||
<style>%s</style>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
`, r.URL.Path, stylesheet)
|
||||
|
||||
responseText += fmt.Sprintf("<h1>Files in %s</h1>\n<hr>", r.URL.Path)
|
||||
|
||||
responseText += "<table>\n"
|
||||
responseText += "<tr>\n<th>Name</th>\n<th>Size</th>\n<th>Modified</th>\n</tr>\n"
|
||||
if !isRoot {
|
||||
responseText += "<tr><td><a href=\"./..\">../</a></td><td>—</td><td>—</td></tr>\n"
|
||||
}
|
||||
|
||||
filepath := path.Join(filesDir, strings.TrimPrefix(r.URL.Path, root))
|
||||
info, err := os.Stat(filepath)
|
||||
if err != nil {
|
||||
|
@ -150,13 +113,13 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
if !info.IsDir() && strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusFound)
|
||||
} else if info.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.Redirect(w, r, r.URL.Path + "/", http.StatusFound)
|
||||
}
|
||||
|
||||
// downloading file
|
||||
if !info.IsDir() {
|
||||
if strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -186,6 +149,17 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.Redirect(w, r, r.URL.Path + "/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
data := Directory{
|
||||
Root: isRoot,
|
||||
Name: r.URL.Path,
|
||||
Files: []*File{},
|
||||
}
|
||||
|
||||
fsDir := os.DirFS(filepath)
|
||||
directories, err := fs.ReadDir(fsDir, ".")
|
||||
for _, dir := range directories {
|
||||
|
@ -202,9 +176,7 @@ func main() {
|
|||
uri = r.URL.Path + name
|
||||
}
|
||||
|
||||
responseText += fmt.Sprintf(
|
||||
"<tr><td><a href=\"%s\">%s</a></td>", uri, name)
|
||||
|
||||
sizeStr := "—"
|
||||
if !info.IsDir() {
|
||||
size := info.Size()
|
||||
sizeDenom := "B"
|
||||
|
@ -220,28 +192,27 @@ func main() {
|
|||
size /= 1000
|
||||
sizeDenom = "GB"
|
||||
}
|
||||
responseText += fmt.Sprintf("<td><code title=\"%d bytes\">%d%s</code></td>", info.Size(), size, sizeDenom)
|
||||
} else {
|
||||
responseText += "<td>—</td>"
|
||||
sizeStr = fmt.Sprintf("%d%s", size, sizeDenom)
|
||||
}
|
||||
|
||||
dateStr := info.ModTime().Format("02-Jan-2006 15:04")
|
||||
responseText += fmt.Sprintf("<td>%s</td>", dateStr)
|
||||
|
||||
responseText += "</tr>\n"
|
||||
data.Files = append(data.Files, &File{
|
||||
Name: name,
|
||||
URI: uri,
|
||||
IsDir: info.IsDir(),
|
||||
Size: sizeStr,
|
||||
ModifiedDate: dateStr,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open directory: %v\n", err)
|
||||
}
|
||||
responseText += "</table>\n"
|
||||
|
||||
responseText += "<hr>\n</main>\n"
|
||||
responseText += "<footer><em>made with <span aria-label=\"love\">♥</span> by ari, 2025 <a href=\"https://git.arimelody.me/ari/indir\" target=\"_blank\">[source]</a></em></footer>\n"
|
||||
responseText += "</body>\n</html>\n"
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(responseText))
|
||||
w.Header().Set("Server", "indir")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
dirTemplate.Execute(w, data)
|
||||
})))
|
||||
}
|
||||
|
||||
|
|
91
templates/dir.html
Normal file
91
templates/dir.html
Normal file
|
@ -0,0 +1,91 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Files in {{.Name}}</title>
|
||||
<style>
|
||||
html {
|
||||
background: #101010;
|
||||
color: #f0f0f0;
|
||||
font-family: 'Monaspace Argon', monospace;
|
||||
}
|
||||
|
||||
body {
|
||||
width: min(calc(100% - 1em), 1000px);
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
tr:hover {
|
||||
background-color: #80808040;
|
||||
}
|
||||
|
||||
th {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
td {
|
||||
width: 1%;
|
||||
max-width: 500px;
|
||||
padding: .2em 0;
|
||||
}
|
||||
|
||||
table a {
|
||||
display: block;
|
||||
line-break: anywhere;
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #b7fd49;
|
||||
text-decoration: none;
|
||||
}
|
||||
a:hover {
|
||||
color: white;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
footer {
|
||||
padding: 1em 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Files in {{.Name}}</h1>
|
||||
<hr>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Size</th>
|
||||
<th>Modified</th>
|
||||
</tr>
|
||||
{{if not .Root}}
|
||||
<tr>
|
||||
<td><a href="./..">../</a></td>
|
||||
<td>—</td>
|
||||
<td>—</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{range .Files}}
|
||||
<tr>
|
||||
<td><a href="{{.URI}}">{{.Name}}</a></td>
|
||||
<td>{{if .IsDir}}—{{else}}{{.Size}}{{end}}</td>
|
||||
<td>{{.ModifiedDate}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
<hr>
|
||||
</main>
|
||||
<footer>
|
||||
<em>made with <span aria-label="love">♥</span> by ari, 2025 <a href="https://git.arimelody.me/ari/indir" target="_blank">[source]</a></em>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue