Skip to content

Commit 149bd12

Browse files
committed
fix: html files with the same name as a folder are now served
1 parent 2325515 commit 149bd12

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

toil.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import "github.com/gorilla/websocket"
2020
An optional argument to pass a root domain and find/replace
2121
it inside the html file content, so you can develop with
2222
absolute paths. Not a high priority, because my static site
23-
builder (qxoko/spindle) solves this problem for me.
23+
builder (lichendust/spindle) solves this problem for me.
2424
2525
# Get rid of gorilla/websocket
2626
2727
It's supposedly possible to do this without using a middleware
2828
library, just with net/http. I haven't tried it. I might do.
2929
*/
3030

31-
const TOIL = "Toil v0.1.1"
31+
const TOIL = "Toil v0.1.3"
3232

3333
const SERVE_PORT = ":3456"
3434
const RELOAD_PREFIX = "/_toil/"
@@ -80,17 +80,19 @@ func main() {
8080
does_exist, is_dir := exists(incoming_path)
8181

8282
if does_exist && is_dir {
83-
incoming_path = filepath.ToSlash(filepath.Join(incoming_path, "index.html"))
84-
does_exist, _ = exists(incoming_path)
85-
} else {
86-
incoming_path += ".html"
87-
does_exist, _ = exists(incoming_path)
83+
index_path := filepath.ToSlash(filepath.Join(incoming_path, "index.html"))
84+
index_exists, _ := exists(index_path)
85+
86+
if index_exists {
87+
serve_file(w, index_path)
88+
return
89+
}
8890
}
8991

92+
incoming_path += ".html"
93+
does_exist, _ = exists(incoming_path)
9094
if does_exist {
91-
// is this a good way to do it? who knows
92-
file_bytes := bytes.Replace(load_file(incoming_path), []byte("</head>"), []byte(RELOAD_SCRIPT), 1)
93-
w.Write([]byte(file_bytes))
95+
serve_file(w, incoming_path)
9496
return
9597
}
9698

@@ -160,6 +162,11 @@ func open_browser(port string) {
160162
println(url)
161163
}
162164

165+
func serve_file(w http.ResponseWriter, file_name string) {
166+
file_bytes := bytes.Replace(load_file(file_name), []byte("</head>"), []byte(RELOAD_SCRIPT), 1)
167+
w.Write([]byte(file_bytes))
168+
}
169+
163170
func exists(file string) (bool, bool) {
164171
f, err := os.Stat(file)
165172
if err != nil {

0 commit comments

Comments
 (0)