Add caching to static files based on etag
This commit is contained in:
parent
f2e52a18e5
commit
06495fa358
2 changed files with 16 additions and 19 deletions
|
|
@ -6,8 +6,10 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
|
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
||||||
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
|
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -24,12 +26,22 @@ func main() {
|
||||||
router := http.NewServeMux()
|
router := http.NewServeMux()
|
||||||
notesRouter := views.GetRoutes("/notes")
|
notesRouter := views.GetRoutes("/notes")
|
||||||
|
|
||||||
|
cacheExpiration, err := time.ParseDuration("24h")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
etag := middleware.NewETag("static", cacheExpiration)
|
||||||
|
|
||||||
router.Handle("/", logger(http.RedirectHandler("/notes/", http.StatusFound)))
|
router.Handle("/", logger(http.RedirectHandler("/notes/", http.StatusFound)))
|
||||||
router.Handle("/notes/", logger(http.StripPrefix("/notes", notesRouter)))
|
router.Handle("/notes/", logger(http.StripPrefix("/notes", notesRouter)))
|
||||||
router.Handle(
|
router.Handle(
|
||||||
"/static/",
|
"/static/",
|
||||||
logger(
|
logger(
|
||||||
http.FileServer(http.FS(conf.Static)),
|
middleware.StaticEtagMiddleware(
|
||||||
|
*etag,
|
||||||
|
http.FileServer(http.FS(conf.Static)),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/crc64"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -13,11 +12,8 @@ type ETag struct {
|
||||||
Name string
|
Name string
|
||||||
Value string
|
Value string
|
||||||
Expiration time.Duration
|
Expiration time.Duration
|
||||||
HashTime time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var etags = make([]*ETag, 0)
|
|
||||||
|
|
||||||
func (etag *ETag) Header() string {
|
func (etag *ETag) Header() string {
|
||||||
return fmt.Sprintf("\"pb-%s-%s\"", etag.Name, etag.Value)
|
return fmt.Sprintf("\"pb-%s-%s\"", etag.Name, etag.Value)
|
||||||
}
|
}
|
||||||
|
|
@ -46,21 +42,10 @@ func StaticEtagMiddleware(etag ETag, next http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateETagFromBuffer calculates etag value from one or more buffers (e.g. embedded files)
|
// GenerateETagFromBuffer calculates etag value from one or more buffers (e.g. embedded files)
|
||||||
func GenerateETagFromBuffer(name string, expiration time.Duration, buffers ...[]byte) (*ETag, error) {
|
func NewETag(name string, expiration time.Duration) *ETag {
|
||||||
start := time.Now()
|
return &ETag{
|
||||||
hash := crc64.New(crc64.MakeTable(crc64.ECMA))
|
|
||||||
for _, buffer := range buffers {
|
|
||||||
_, err := hash.Write(buffer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to generate etag from buffer: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
etag := &ETag{
|
|
||||||
Name: name,
|
Name: name,
|
||||||
Expiration: expiration,
|
Expiration: expiration,
|
||||||
Value: fmt.Sprintf("%x", hash.Sum64()),
|
Value: fmt.Sprintf("%d", time.Now().Unix()),
|
||||||
HashTime: time.Since(start),
|
|
||||||
}
|
}
|
||||||
etags = append(etags, etag)
|
|
||||||
return etag, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue