~blu/artemis

c494c878fb5a7c65950bf752140208a3c0ed7dd5 — Bryant Conquest 1 year, 5 months ago 7d3e5a0
add request middleware for logging
1 files changed, 59 insertions(+), 50 deletions(-)

M main.go
M main.go => main.go +59 -50
@@ 20,65 20,74 @@ var (
	cacheLoads  = expvar.NewInt("counter_artemis_cache_loads")
)

// TODO: Purge all files
// TODO: Remove best fit file
//   - file with least amount of hits that is the smallest
//   - how handle things that need to delete a bunch of files
type Handler struct {
	Cache    *cache.Cache
	etagLock *sync.RWMutex
	etags    map[string]bool
}

func (h *Handler) Purge(w http.ResponseWriter, r *http.Request) {
	err := h.Cache.PurgeCache()
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
func Purge(h *Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		err := h.Cache.PurgeCache()
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

	w.WriteHeader(http.StatusNoContent)
		w.WriteHeader(http.StatusNoContent)
	})
}

func (h *Handler) AssetHandler(w http.ResponseWriter, r *http.Request) {
	path := strings.TrimLeft(r.URL.Path, "/assets")

	if strings.HasSuffix(path, ".svg") {
		w.Header().Set("Content-Type", "image/svg+xml")
	}

	etag := r.Header.Get("If-None-Match")
	h.etagLock.RLock()
	ok := h.etags[etag]
	h.etagLock.RUnlock()
	if ok {
		w.WriteHeader(http.StatusNotModified)
		return
	}

	data, err, hit := h.Cache.LoadOrReadAsset(path)
	if err != nil {
		log.Println(err)
		cacheErrors.Add(1)
		w.WriteHeader(http.StatusNotFound)
		return
	}
func AssetHandler(h *Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		path := strings.TrimLeft(r.URL.Path, "/assets")

		if strings.HasSuffix(path, ".svg") {
			w.Header().Set("Content-Type", "image/svg+xml")
		}

		etag := r.Header.Get("If-None-Match")
		h.etagLock.RLock()
		ok := h.etags[etag]
		h.etagLock.RUnlock()
		if ok {
			w.WriteHeader(http.StatusNotModified)
			return
		}

		data, err, hit := h.Cache.LoadOrReadAsset(path)
		if err != nil {
			log.Println(err)
			cacheErrors.Add(1)
			w.WriteHeader(http.StatusNotFound)
			return
		}

		if hit {
			cacheHits.Add(1)
		} else {
			cacheLoads.Add(1)
		}

		// Does not ever get invalidated
		// should etags be handled by the cache?
		etag = fmt.Sprintf("%x", sha256.Sum256(data))
		h.etagLock.Lock()
		h.etags[etag] = true
		h.etagLock.Unlock()
		w.Header().Set("etag", etag)
		w.Header().Set("Cache-Control", "max-age=86400")
		w.Write(data)
	})
}

	if hit {
		cacheHits.Add(1)
	} else {
		cacheLoads.Add(1)
func Logger(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		log.Println(r.Method, r.URL.String(), r.UserAgent(), r.Referer())
		next.ServeHTTP(w, r)
	}

	// Does not ever get invalidated
	// should etags be handled by the cache?
	etag = fmt.Sprintf("%x", sha256.Sum256(data))
	h.etagLock.Lock()
	h.etags[etag] = true
	h.etagLock.Unlock()
	w.Header().Set("etag", etag)
	w.Header().Set("Cache-Control", "max-age=86400")
	w.Write(data)
	return http.HandlerFunc(fn)
}

func main() {


@@ 106,9 115,9 @@ func main() {
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/assets/", h.AssetHandler)
	mux.HandleFunc("/purge", h.Purge)
	mux.Handle("/metrics/json", expvar.Handler())
	mux.Handle("/assets/", Logger(AssetHandler(&h)))
	mux.Handle("/purge", Logger(Purge(&h)))
	mux.Handle("/metrics/json", Logger(expvar.Handler()))

	log.Println("artemis started on 8080")
	log.Fatal(http.ListenAndServe(":8080", mux))