mirror of https://github.com/jlelse/GoBlog
parent
affa7d7d54
commit
bf9d637468
5
http.go
5
http.go
|
@ -202,6 +202,11 @@ func (a *goBlog) buildRouter() (http.Handler, error) {
|
|||
// Robots.txt
|
||||
r.With(cacheLoggedIn, a.cacheMiddleware).Get(robotsTXTPath, a.serveRobotsTXT)
|
||||
|
||||
// Favicon
|
||||
if !hasStaticPath("favicon.ico") {
|
||||
r.With(cacheLoggedIn, a.cacheMiddleware).Get("/favicon.ico", a.serve404)
|
||||
}
|
||||
|
||||
r.NotFound(a.servePostsAliasesRedirects())
|
||||
|
||||
r.MethodNotAllowed(a.serveNotAllowed)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -27,6 +28,15 @@ func allStaticPaths() (paths []string) {
|
|||
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("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))
|
||||
|
|
Loading…
Reference in New Issue