<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"net/http"
	"time"
)

type interceptingWriter struct {
	code int
	http.ResponseWriter
}

func (iw *interceptingWriter) WriteHeader(code int) {
	iw.code = code
	iw.ResponseWriter.WriteHeader(code)
}

// Log is the structure for logging
type Log struct {
	Time     time.Time `json:"time"`
	IP       string    `json:"ip"`
	Method   string    `json:"method"`
	Status   int       `json:"status"`
	Duration string    `json:"duration"`
	Path     string    `json:"path"`
	Referrer string    `json:"referrer"`
}

// Logger is a logging middleware. Path, request time and Method are recorded
func Logger(inner http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		iw := interceptingWriter{200, w}

		inner.ServeHTTP(&iw, r)

		d := fmt.Sprintf("%s", time.Since(start))
		l := Log{
			Time:     time.Now(),
			IP:       r.Header.Get("X-Forwarded-For"),
			Method:   r.Method,
			Status:   iw.code,
			Duration: d,
			Path:     r.RequestURI,
			Referrer: r.Referer(),
		}

		b, _ := json.Marshal(l)
		fmt.Println(string(b))
	})
}

func main() {
	var (
		dir = flag.String("path", "public", "Path to static site")
	)

	fs := http.FileServer(http.Dir(*dir))
	fs = Logger(fs)
	http.Handle("/", fs)
	http.ListenAndServe(":3000", nil)
}<commit_msg>add caching to improve performance<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"net/http"
	"time"
)

type interceptingWriter struct {
	code int
	http.ResponseWriter
}

func (iw *interceptingWriter) WriteHeader(code int) {
	iw.code = code
	iw.ResponseWriter.WriteHeader(code)
}

// Log is the structure for logging
type Log struct {
	Time     time.Time `json:"time"`
	IP       string    `json:"ip"`
	Method   string    `json:"method"`
	Status   int       `json:"status"`
	Duration string    `json:"duration"`
	Path     string    `json:"path"`
	Referrer string    `json:"referrer"`
}

// Logger is a logging middleware. Path, request time and Method are recorded
func Logger(inner http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		iw := interceptingWriter{200, w}

		inner.ServeHTTP(&iw, r)

		d := fmt.Sprintf("%s", time.Since(start))
		l := Log{
			Time:     time.Now(),
			IP:       r.Header.Get("X-Forwarded-For"),
			Method:   r.Method,
			Status:   iw.code,
			Duration: d,
			Path:     r.RequestURI,
			Referrer: r.Referer(),
		}

		b, _ := json.Marshal(l)
		fmt.Println(string(b))
	})
}

// Cache is a caching middleware
func Cache(inner http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Cache-Control", "public")
		w.Header().Add("Cache-Control", "maxage=86400") // One day
		inner.ServeHTTP(w, r)
	})
}

func main() {
	var (
		dir = flag.String("path", "public", "Path to static site")
	)

	fs := http.FileServer(http.Dir(*dir))
	fs = Logger(fs)
	http.Handle("/", fs)
	http.ListenAndServe(":3000", nil)
}
{'fraction_non_alphanumeric': 0.15455140798952194, 'fraction_numerical': 0.006221349050425672, 'mean_word_length': 3.427536231884058, 'pattern_counts': {'":': 2, '<': 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': '4570723', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1086, 'n_words': 266}