-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.go
More file actions
38 lines (32 loc) · 868 Bytes
/
Copy pathpaths.go
File metadata and controls
38 lines (32 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"os"
"path/filepath"
)
// Get local file path of the requested file.
//
// If request is for a directory, try "index.html" inside it.
// If request is for a missing file, try with ".html".
//
// Note the returned path is not guaranteed to be for an existing
// file or directory.
func resolveFilepath(dir, path string) string {
fp := filepath.Join(dir, path)
// Try index.html for directory requests (only if trailing '/'!)
if path[len(path)-1] == '/' {
index := filepath.Join(fp, "index.html")
return ifExists(index, fp)
}
// Try .html for missing files (aka "pretty" urls)
if info, err := os.Stat(fp); err != nil || info.IsDir() {
pretty := fp + ".html"
return ifExists(pretty, fp)
}
return fp
}
func ifExists(path, fallback string) string {
if _, err := os.Stat(path); err == nil {
return path
}
return fallback
}