serve index.html when present (case-sensitve)
This commit is contained in:
parent
245d6e0fa0
commit
f567a2eb83
3 changed files with 330 additions and 350 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -2,3 +2,5 @@
|
||||||
tmp
|
tmp
|
||||||
indir
|
indir
|
||||||
indir-*
|
indir-*
|
||||||
|
.zed
|
||||||
|
index.html
|
||||||
|
|
|
||||||
109
main.go
109
main.go
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
|
@ -14,16 +13,17 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
_ "embed"
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/gomarkdown/markdown/html"
|
"github.com/gomarkdown/markdown/html"
|
||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/dir.html
|
//go:embed templates/dir.html
|
||||||
var dirTemplateSrc string
|
var dirTemplateSrc string
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Directory struct {
|
Directory struct {
|
||||||
Name string
|
Name string
|
||||||
Root bool
|
Root bool
|
||||||
|
|
@ -38,12 +38,10 @@ type (
|
||||||
Size string
|
Size string
|
||||||
ModifiedDate string
|
ModifiedDate string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 { printHelp() }
|
||||||
printHelp()
|
|
||||||
}
|
|
||||||
|
|
||||||
host := "127.0.0.1"
|
host := "127.0.0.1"
|
||||||
port := 8080
|
port := 8080
|
||||||
|
|
@ -52,9 +50,7 @@ func main() {
|
||||||
filesDir := ""
|
filesDir := ""
|
||||||
i := 1
|
i := 1
|
||||||
for {
|
for {
|
||||||
if i >= len(os.Args) {
|
if i >= len(os.Args) { break }
|
||||||
break
|
|
||||||
}
|
|
||||||
switch os.Args[i] {
|
switch os.Args[i] {
|
||||||
case "-h":
|
case "-h":
|
||||||
fallthrough
|
fallthrough
|
||||||
|
|
@ -62,7 +58,7 @@ func main() {
|
||||||
printHelp()
|
printHelp()
|
||||||
|
|
||||||
case "--host":
|
case "--host":
|
||||||
if i+1 >= len(os.Args) {
|
if i + 1 >= len(os.Args) {
|
||||||
fmt.Fprintf(os.Stderr, "fatal: --host argument cannot be empty\n")
|
fmt.Fprintf(os.Stderr, "fatal: --host argument cannot be empty\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +66,7 @@ func main() {
|
||||||
host = os.Args[i]
|
host = os.Args[i]
|
||||||
|
|
||||||
case "--port":
|
case "--port":
|
||||||
if i+1 >= len(os.Args) {
|
if i + 1 >= len(os.Args) {
|
||||||
fmt.Fprintf(os.Stderr, "fatal: --port argument cannot be empty\n")
|
fmt.Fprintf(os.Stderr, "fatal: --port argument cannot be empty\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
@ -83,18 +79,14 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "--root":
|
case "--root":
|
||||||
if i+1 >= len(os.Args) {
|
if i + 1 >= len(os.Args) {
|
||||||
fmt.Fprintf(os.Stderr, "fatal: --root argument cannot be empty\n")
|
fmt.Fprintf(os.Stderr, "fatal: --root argument cannot be empty\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
root = os.Args[i]
|
root = os.Args[i]
|
||||||
if !strings.HasPrefix(root, "/") {
|
if !strings.HasPrefix(root, "/") { root = "/" + root }
|
||||||
root = "/" + root
|
if !strings.HasSuffix(root, "/") { root += "/" }
|
||||||
}
|
|
||||||
if !strings.HasSuffix(root, "/") {
|
|
||||||
root += "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if len(filesDir) > 0 {
|
if len(filesDir) > 0 {
|
||||||
|
|
@ -122,9 +114,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Now hosting \"%s\" at http://%s:%d", filesDir, host, port)
|
fmt.Printf("Now hosting \"%s\" at http://%s:%d", filesDir, host, port)
|
||||||
if root != "/" {
|
if root != "/" { fmt.Printf("%s", root) }
|
||||||
fmt.Printf("%s", root)
|
|
||||||
}
|
|
||||||
fmt.Println(".")
|
fmt.Println(".")
|
||||||
|
|
||||||
http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -178,7 +168,14 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasSuffix(r.URL.Path, "/") {
|
if !strings.HasSuffix(r.URL.Path, "/") {
|
||||||
http.Redirect(w, r, r.URL.Path+"/", http.StatusFound)
|
http.Redirect(w, r, r.URL.Path + "/", http.StatusFound)
|
||||||
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -212,14 +209,10 @@ func main() {
|
||||||
directories, err := fs.ReadDir(fsDir, ".")
|
directories, err := fs.ReadDir(fsDir, ".")
|
||||||
for _, dir := range directories {
|
for _, dir := range directories {
|
||||||
name := dir.Name()
|
name := dir.Name()
|
||||||
if slices.Contains(ignoredFiles, name) {
|
if slices.Contains(ignoredFiles, name) { continue }
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
info, err := dir.Info()
|
info, err := dir.Info()
|
||||||
if err != nil {
|
if err != nil { continue }
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var uri string
|
var uri string
|
||||||
if isRoot {
|
if isRoot {
|
||||||
|
|
@ -266,24 +259,24 @@ func main() {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
dirTemplate.Execute(w, data)
|
dirTemplate.Execute(w, data)
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoggingResponseWriter struct {
|
type LoggingResponseWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
Status int
|
Status int
|
||||||
}
|
}
|
||||||
|
|
||||||
var COL_Reset = "\033[0m"
|
var COL_Reset = "\033[0m"
|
||||||
var COL_Red = "\033[31m"
|
var COL_Red = "\033[31m"
|
||||||
var COL_Green = "\033[32m"
|
var COL_Green = "\033[32m"
|
||||||
var COL_Yellow = "\033[33m"
|
var COL_Yellow = "\033[33m"
|
||||||
var COL_Blue = "\033[34m"
|
var COL_Blue = "\033[34m"
|
||||||
var COL_Purple = "\033[35m"
|
var COL_Purple = "\033[35m"
|
||||||
var COL_Cyan = "\033[36m"
|
var COL_Cyan = "\033[36m"
|
||||||
var COL_Gray = "\033[37m"
|
var COL_Gray = "\033[37m"
|
||||||
var COL_White = "\033[97m"
|
var COL_White = "\033[97m"
|
||||||
|
|
||||||
func HTTPLog(next http.Handler) http.Handler {
|
func HTTPLog(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
|
|
@ -300,18 +293,10 @@ func HTTPLog(next http.Handler) http.Handler {
|
||||||
|
|
||||||
statusColour := COL_Reset
|
statusColour := COL_Reset
|
||||||
|
|
||||||
if lrw.Status-600 <= 0 {
|
if lrw.Status - 600 <= 0 { statusColour = COL_Red }
|
||||||
statusColour = COL_Red
|
if lrw.Status - 500 <= 0 { statusColour = COL_Yellow }
|
||||||
}
|
if lrw.Status - 400 <= 0 { statusColour = COL_White }
|
||||||
if lrw.Status-500 <= 0 {
|
if lrw.Status - 300 <= 0 { statusColour = COL_Green }
|
||||||
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",
|
fmt.Printf("[%s] %s %s - %s%d%s (%sms) (%s)\n",
|
||||||
after.Format(time.UnixDate),
|
after.Format(time.UnixDate),
|
||||||
|
|
@ -323,9 +308,9 @@ func HTTPLog(next http.Handler) http.Handler {
|
||||||
elapsed,
|
elapsed,
|
||||||
r.Header["User-Agent"][0])
|
r.Header["User-Agent"][0])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func printHelp() {
|
func printHelp() {
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
`%s [--host address] [--port port] [--root http_root] directory
|
`%s [--host address] [--port port] [--root http_root] directory
|
||||||
|
|
||||||
|
|
@ -336,4 +321,4 @@ func printHelp() {
|
||||||
os.Args[0],
|
os.Args[0],
|
||||||
)
|
)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,18 +57,13 @@
|
||||||
margin-top: 2rem;
|
margin-top: 2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.readme h1,
|
.readme h1, .readme h2, .readme h3,
|
||||||
.readme h2,
|
.readme h4, .readme h5, .readme h6 {
|
||||||
.readme h3,
|
|
||||||
.readme h4,
|
|
||||||
.readme h5,
|
|
||||||
.readme h6 {
|
|
||||||
color: #f0f0f0;
|
color: #f0f0f0;
|
||||||
margin-top: 1.2rem;
|
margin-top: 1.2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.readme p,
|
.readme p, .readme li {
|
||||||
.readme li {
|
|
||||||
line-height: 1.7;
|
line-height: 1.7;
|
||||||
color: #d0d0d0;
|
color: #d0d0d0;
|
||||||
}
|
}
|
||||||
|
|
@ -86,9 +81,9 @@
|
||||||
background: #1e1e1e;
|
background: #1e1e1e;
|
||||||
border: 1px solid #333;
|
border: 1px solid #333;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
padding: 0.1em, 0.4em;
|
padding: .1em, .4em;
|
||||||
font-family: "Monaspace Argon", monospace;
|
font-family: 'Monaspace Argon', monospace;
|
||||||
font-size: 0.9em;
|
font-size: .9em;
|
||||||
color: #b7fd79;
|
color: #b7fd79;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,7 +119,7 @@
|
||||||
.readme table td,
|
.readme table td,
|
||||||
.readme table th {
|
.readme table th {
|
||||||
border: 1px solid #333;
|
border: 1px solid #333;
|
||||||
padding: 0.3em 0.6em;
|
padding: .3em .6em;
|
||||||
width: auto;
|
width: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,8 +170,6 @@
|
||||||
<hr />
|
<hr />
|
||||||
{{if .Readme}}
|
{{if .Readme}}
|
||||||
<article class="readme">
|
<article class="readme">
|
||||||
<h2>README</h2>
|
|
||||||
<hr />
|
|
||||||
{{.Readme}}
|
{{.Readme}}
|
||||||
</article>
|
</article>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue