mirror of https://github.com/jlelse/GoBlog
Simple blogging system written in Go
https://goblog.app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
815 B
34 lines
815 B
package main |
|
|
|
import ( |
|
"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 |
|
} |
|
|
|
// Gets only called by registered paths |
|
func (a *goBlog) serveStaticFile(w http.ResponseWriter, r *http.Request) { |
|
w.Header().Set("Cache-Control", 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)) |
|
}
|
|
|