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.
 
 
 
 
 
 

26 lines
571 B

package main
import (
"net/http"
"os"
"path/filepath"
"github.com/go-chi/chi/v5"
)
const (
mediaFilePath = "data/media"
mediaFileRoute = `/{file:[0-9a-fA-F]+(\.[0-9a-zA-Z]+)?}`
)
func (a *goBlog) serveMediaFile(w http.ResponseWriter, r *http.Request) {
f := filepath.Join(mediaFilePath, chi.URLParam(r, "file"))
_, err := os.Stat(f)
if err != nil {
// Serve 404, but don't use normal serve404 method because of media domain
http.NotFound(w, r)
return
}
w.Header().Add("Cache-Control", "public,max-age=31536000,immutable")
http.ServeFile(w, r, f)
}