-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathstaticFiles.go
44 lines (39 loc) · 1 KB
/
staticFiles.go
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
39
40
41
42
43
44
package main
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
const staticFolder = "static"
func allStaticPaths() (paths []string) {
paths = []string{}
err := filepath.Walk(staticFolder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() {
paths = append(paths, strings.TrimPrefix(path, staticFolder))
}
return nil
})
if err != nil {
return
}
return
}
func hasStaticPath(path string) bool {
// Check if file exists
_, err := os.Stat(filepath.Join(staticFolder, path))
if err != nil && errors.Is(err, os.ErrNotExist) {
return false
}
return true
}
// Gets only called by registered paths
func (a *goBlog) serveStaticFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set(cacheControl, fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", a.cfg.Cache.Expiration, a.cfg.Cache.Expiration/3, a.cfg.Cache.Expiration))
http.ServeFile(w, r, filepath.Join(staticFolder, r.URL.Path))
}