-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatic.go
More file actions
35 lines (33 loc) · 1.06 KB
/
static.go
File metadata and controls
35 lines (33 loc) · 1.06 KB
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
package ink
import (
"os"
"net/http"
"path/filepath"
)
func Static(root string) func(ctx *Context) {
return func(ctx *Context) {
// http.FileServer(http.Dir(root)).ink.Web
reqURL := ctx.Req.URL.Path
if reqURL == "" || reqURL == "/" {
indexPath := filepath.Join(root, "index.html")
if _, err := os.Stat(indexPath); err == nil {
http.ServeFile(ctx.Res, ctx.Req, indexPath)
ctx.Stop()
}
} else {
fileName := root + reqURL
filePath, _ := filepath.Abs(fileName)
rootPath, _ := filepath.Abs(root)
fileDir := filepath.Dir(filePath)
f, err := os.Stat(filePath)
if err == nil && filepath.HasPrefix(fileDir, rootPath) {
if f.IsDir() {
http.ServeFile(ctx.Res, ctx.Req, filepath.Join(filePath, "index.html"))
} else {
http.ServeFile(ctx.Res, ctx.Req, filePath)
}
ctx.Stop()
}
}
}
}