<commit_before>package handler

import (
	"gopkg.in/redis.v3"
	"io"
	"log"
	"net/http"
	"time"
)

type RedirectHandler struct {
	Client *redis.Client
	Logger *log.Logger
}

func (h *RedirectHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	logEntry := time.Now().UTC().String()

	if req.RequestURI == "/healthcheck" {
		logEntry += " 200 " + req.RequestURI
		h.Logger.Print(logEntry)
		io.WriteString(w, "WORKING\n")
		return
	}

	redirect, err := h.Client.Get("go-shortener:" + req.RequestURI).Result()
	if redirect == "" || err != nil {
		logEntry += " 404 " + req.RequestURI
		http.NotFound(w, req)
	} else {
		logEntry += " 301 " + req.RequestURI + " " + redirect
		h.Client.Incr("go-shortener-count:" + req.RequestURI)
		http.Redirect(w, req, redirect, 301)
	}

	h.Logger.Print(logEntry)
}<commit_msg>Healthcheck verifies connection to REDIS<commit_after>package handler

import (
	"gopkg.in/redis.v3"
	"io"
	"log"
	"net/http"
	"time"
)

type RedirectHandler struct {
	Client *redis.Client
	Logger *log.Logger
}

func (h *RedirectHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	logEntry := time.Now().UTC().String()

	if req.RequestURI == "/healthcheck" {
		err := h.Client.Ping().Err()
		if err != nil {
			http.Error(w, "Could not connect to REDIS server", 502)
			return
		}
		logEntry += " 200 " + req.RequestURI
		h.Logger.Print(logEntry)
		io.WriteString(w, "WORKING\n")
		return
	}

	redirect, err := h.Client.Get("go-shortener:" + req.RequestURI).Result()
	if redirect == "" || err != nil {
		logEntry += " 404 " + req.RequestURI
		http.NotFound(w, req)
	} else {
		logEntry += " 301 " + req.RequestURI + " " + redirect
		h.Client.Incr("go-shortener-count:" + req.RequestURI)
		http.Redirect(w, req, redirect, 301)
	}

	h.Logger.Print(logEntry)
}
{'fraction_non_alphanumeric': 0.16880939072107323, 'fraction_numerical': 0.01621017328116266, 'mean_word_length': 3.7733333333333334, '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': '4779491', 'n_tokens_mistral': 731, 'n_tokens_neox': 667, 'n_words': 136}