<commit_before>package server

import (
	"bytes"
	"io"
	"log"
	"net/http"

	"github.com/terranodo/tegola/cache"
)

//	TileCacheHandler implements a request cache for tiles on requests when the URLs
//	have a /:z/:x/:y scheme suffix (i.e. /osm/1/3/4.pbf)
func TileCacheHandler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var err error

		//	check if a cache backend exists
		if Cache == nil {
			//	nope. move on
			next.ServeHTTP(w, r)
			return
		}

		//	parse our URI into a cache key structure
		key, err := cache.ParseKey(r.URL.Path)
		if err != nil {
			log.Println("cache middleware: ParseKey err: %v", err)
			next.ServeHTTP(w, r)
			return
		}

		//	use the URL path as the key
		cachedTile, hit, err := Cache.Get(key)
		if err != nil {
			log.Printf("cache middleware: error reading from cache: %v", err)
			next.ServeHTTP(w, r)
			return
		}
		//	cache miss
		if !hit {
			//	buffer which will hold a copy of the response for writing to the cache
			var buff bytes.Buffer

			//	ovewrite our current responseWriter with a tileCacheResponseWriter
			w = newTileCacheResponseWriter(w, &buff)

			next.ServeHTTP(w, r)

			if err := Cache.Set(key, buff.Bytes()); err != nil {
				log.Println("cache response writer err: %v", err)
			}
			return
		}

		//	TODO: how configurable do we want the CORS policy to be?
		//	set CORS header
		w.Header().Add("Access-Control-Allow-Origin", "*")

		//	mimetype for protocol buffers
		w.Header().Add("Content-Type", "application/x-protobuf")<commit_msg>cehck for canceled requests before writing to cache in cache middleware. #96<commit_after>package server

import (
	"bytes"
	"io"
	"log"
	"net/http"

	"github.com/terranodo/tegola/cache"
)

//	TileCacheHandler implements a request cache for tiles on requests when the URLs
//	have a /:z/:x/:y scheme suffix (i.e. /osm/1/3/4.pbf)
func TileCacheHandler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var err error

		//	check if a cache backend exists
		if Cache == nil {
			//	nope. move on
			next.ServeHTTP(w, r)
			return
		}

		//	parse our URI into a cache key structure
		key, err := cache.ParseKey(r.URL.Path)
		if err != nil {
			log.Println("cache middleware: ParseKey err: %v", err)
			next.ServeHTTP(w, r)
			return
		}

		//	use the URL path as the key
		cachedTile, hit, err := Cache.Get(key)
		if err != nil {
			log.Printf("cache middleware: error reading from cache: %v", err)
			next.ServeHTTP(w, r)
			return
		}
		//	cache miss
		if !hit {
			//	buffer which will hold a copy of the response for writing to the cache
			var buff bytes.Buffer

			//	ovewrite our current responseWriter with a tileCacheResponseWriter
			w = newTileCacheResponseWriter(w, &buff)

			next.ServeHTTP(w, r)

			//	check if our request context has been canceled
			if r.Context().Err() != nil {
				return
			}

			if err := Cache.Set(key, buff.Bytes()); err != nil {
				log.Println("cache response writer err: %v", err)
			}
			return
		}

		//	TODO: how configurable do we want the CORS policy to be?
		//	set CORS header
		w.Header().Add("Access-Control-Allow-Origin", "*")

		//	mimetype for protocol buffers
		w.Header().Add("Content-Type", "application/x-protobuf")
{'fraction_non_alphanumeric': 0.12401215805471125, 'fraction_numerical': 0.0024316109422492403, 'mean_word_length': 3.0380368098159507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '948579', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1128, 'n_words': 360}