serve index.html when present (case-sensitve)

This commit is contained in:
claire treise 2026-03-16 22:05:29 +01:00
parent 245d6e0fa0
commit f567a2eb83
3 changed files with 330 additions and 350 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@
tmp
indir
indir-*
.zed
index.html

53
main.go
View file

@ -1,7 +1,6 @@
package main
import (
_ "embed"
"fmt"
"html/template"
"io/fs"
@ -14,6 +13,7 @@ import (
"strconv"
"strings"
"time"
_ "embed"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
@ -41,9 +41,7 @@ type (
)
func main() {
if len(os.Args) < 2 {
printHelp()
}
if len(os.Args) < 2 { printHelp() }
host := "127.0.0.1"
port := 8080
@ -52,9 +50,7 @@ func main() {
filesDir := ""
i := 1
for {
if i >= len(os.Args) {
break
}
if i >= len(os.Args) { break }
switch os.Args[i] {
case "-h":
fallthrough
@ -89,12 +85,8 @@ func main() {
}
i++
root = os.Args[i]
if !strings.HasPrefix(root, "/") {
root = "/" + root
}
if !strings.HasSuffix(root, "/") {
root += "/"
}
if !strings.HasPrefix(root, "/") { root = "/" + root }
if !strings.HasSuffix(root, "/") { root += "/" }
default:
if len(filesDir) > 0 {
@ -122,9 +114,7 @@ func main() {
}
fmt.Printf("Now hosting \"%s\" at http://%s:%d", filesDir, host, port)
if root != "/" {
fmt.Printf("%s", root)
}
if root != "/" { fmt.Printf("%s", root) }
fmt.Println(".")
http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -182,6 +172,13 @@ func main() {
return
}
// serve index.html if present (case-sensitive)
indexPath := filepath.Join(fpath, "index.html")
if _, err := os.Stat(indexPath); err == nil {
http.ServeFile(w, r, indexPath)
return
}
// embeded readme
var readmeHTML template.HTML
entries, err := os.ReadDir(fpath)
@ -212,14 +209,10 @@ func main() {
directories, err := fs.ReadDir(fsDir, ".")
for _, dir := range directories {
name := dir.Name()
if slices.Contains(ignoredFiles, name) {
continue
}
if slices.Contains(ignoredFiles, name) { continue }
info, err := dir.Info()
if err != nil {
continue
}
if err != nil { continue }
var uri string
if isRoot {
@ -300,18 +293,10 @@ func HTTPLog(next http.Handler) http.Handler {
statusColour := COL_Reset
if lrw.Status-600 <= 0 {
statusColour = COL_Red
}
if lrw.Status-500 <= 0 {
statusColour = COL_Yellow
}
if lrw.Status-400 <= 0 {
statusColour = COL_White
}
if lrw.Status-300 <= 0 {
statusColour = COL_Green
}
if lrw.Status - 600 <= 0 { statusColour = COL_Red }
if lrw.Status - 500 <= 0 { statusColour = COL_Yellow }
if lrw.Status - 400 <= 0 { statusColour = COL_White }
if lrw.Status - 300 <= 0 { statusColour = COL_Green }
fmt.Printf("[%s] %s %s - %s%d%s (%sms) (%s)\n",
after.Format(time.UnixDate),

View file

@ -57,18 +57,13 @@
margin-top: 2rem;
}
.readme h1,
.readme h2,
.readme h3,
.readme h4,
.readme h5,
.readme h6 {
.readme h1, .readme h2, .readme h3,
.readme h4, .readme h5, .readme h6 {
color: #f0f0f0;
margin-top: 1.2rem;
}
.readme p,
.readme li {
.readme p, .readme li {
line-height: 1.7;
color: #d0d0d0;
}
@ -86,9 +81,9 @@
background: #1e1e1e;
border: 1px solid #333;
border-radius: 3px;
padding: 0.1em, 0.4em;
font-family: "Monaspace Argon", monospace;
font-size: 0.9em;
padding: .1em, .4em;
font-family: 'Monaspace Argon', monospace;
font-size: .9em;
color: #b7fd79;
}
@ -124,7 +119,7 @@
.readme table td,
.readme table th {
border: 1px solid #333;
padding: 0.3em 0.6em;
padding: .3em .6em;
width: auto;
}
@ -175,8 +170,6 @@
<hr />
{{if .Readme}}
<article class="readme">
<h2>README</h2>
<hr />
{{.Readme}}
</article>
{{end}}