Skip to content

Commit

Permalink
feat: add gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
bomgar committed Oct 13, 2024
1 parent b1d17a5 commit d6421c2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
24 changes: 24 additions & 0 deletions server/middleware/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package middleware

import (
"net/http"
"time"
)

func CacheWhileServerIsRunning(next http.Handler) http.Handler {
startTime := time.Now().Truncate(time.Second)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ifModifiedSince := r.Header.Get("If-Modified-Since")
if ifModifiedSince != "" {
modTime, err := time.Parse(http.TimeFormat, ifModifiedSince)
if err == nil {
if !startTime.After(modTime) {
w.WriteHeader(http.StatusNotModified)
return
}
}
}
w.Header().Set("Last-Modified", startTime.UTC().Format(http.TimeFormat))
next.ServeHTTP(w, r)
})
}
36 changes: 36 additions & 0 deletions server/middleware/gzipmiddleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package middleware

import (
"compress/gzip"
"io"
"net/http"
"strings"
)


type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

func GzipMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
}

w.Header().Set("Content-Encoding", "gzip")
w.Header().Add("Vary", "Accept-Encoding")

gz := gzip.NewWriter(w)
defer gz.Close()

gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
next.ServeHTTP(gzw, r)
})
}
24 changes: 3 additions & 21 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"log/slog"
"net/http"
"os"
"time"

"github.com/brocode/neoweb/components"
"github.com/brocode/neoweb/key"
"github.com/brocode/neoweb/nvimwrapper"
"github.com/brocode/neoweb/server/middleware"
)

//go:embed static
Expand Down Expand Up @@ -113,31 +113,13 @@ func (s *Server) getEvents(w http.ResponseWriter, r *http.Request) {
slog.Info("Events client disconnected")
}

func cacheWhileServerIsRunning(next http.Handler) http.Handler {
startTime := time.Now().Truncate(time.Second)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ifModifiedSince := r.Header.Get("If-Modified-Since")
if ifModifiedSince != "" {
modTime, err := time.Parse(http.TimeFormat, ifModifiedSince)
if err == nil {
if !startTime.After(modTime) {
w.WriteHeader(http.StatusNotModified)
return
}
}
}
w.Header().Set("Last-Modified", startTime.UTC().Format(http.TimeFormat))
next.ServeHTTP(w, r)
})
}

func (s *Server) Start() {

mux := http.NewServeMux()

mux.Handle("GET /static/", cacheWhileServerIsRunning(http.FileServer(http.FS(staticFs))))
mux.Handle("GET /static/", middleware.CacheWhileServerIsRunning(middleware.GzipMiddleware(http.FileServer(http.FS(staticFs)))))

mux.HandleFunc("GET /", s.getRoot)
mux.Handle("GET /", middleware.GzipMiddleware(http.HandlerFunc(s.getRoot)))

mux.HandleFunc("POST /keypress", s.postKeypress)

Expand Down

0 comments on commit d6421c2

Please sign in to comment.