embed template file, improve cmd arg handling

This commit is contained in:
ari melody 2025-07-25 17:02:01 +01:00
parent 59230b7ad5
commit d5206362fa
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E

16
main.go
View file

@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"time"
_ "embed"
)
type (
@ -30,6 +31,9 @@ type (
}
)
//go:embed templates/dir.html
var dirHTML string
func main() {
if len(os.Args) < 2 { printHelp() }
@ -37,7 +41,7 @@ func main() {
port := 8080
root := "/"
filesDir := "."
filesDir := ""
i := 1
for {
if i >= len(os.Args) { break }
@ -79,17 +83,25 @@ func main() {
if !strings.HasSuffix(root, "/") { root += "/" }
default:
if len(filesDir) > 0 {
fmt.Fprintf(os.Stderr, "unsupported argument: %s\n", os.Args[i])
os.Exit(1)
}
filesDir = os.Args[i]
}
i++
}
if len(filesDir) == 0 {
filesDir = "."
}
ignoredFiles := []string{
".",
".DS_Store",
}
dirTemplate, err := template.ParseGlob("./templates/dir.html")
dirTemplate, err := template.New("dir").Parse(dirHTML)
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: failed to parse directory template: %v\n", err)
os.Exit(1)