<commit_before>package routers

import (
	"fmt"
	"github.com/jpgneves/shorty/requests"
	"github.com/jpgneves/shorty/resources"
	"net/http"
)

type RoutingHandler struct {
	router Router
}

func MakeRoutingHandler(router Router) *RoutingHandler {
	return &RoutingHandler{router}
}

func (rh *RoutingHandler) SetRouter(r Router) {
	rh.router = r
}

func (rh *RoutingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var response *requests.Response
	if rh.router != nil {
		path := r.URL.Path
		match := rh.router.Route(path)
		if resource, ok := match.value.(resources.Resource); ok {
			req := &requests.Request{r, match.matches}
			switch r.Method {
			case "GET":
				response = resource.Get(req)
			case "POST":
				response = resource.Post(req)
			default:
				w.WriteHeader(http.StatusMethodNotAllowed)
				return
			}
			w.WriteHeader(response.StatusCode)
			fmt.Fprintf(w, response.Data)
		} else {
			w.WriteHeader(http.StatusNotFound)
		}
	} else {
		w.WriteHeader(http.StatusNotFound)
	}
	return
}<commit_msg>Generalize request handling a bit more<commit_after>package routers

import (
	"fmt"
	"github.com/jpgneves/shorty/requests"
	"github.com/jpgneves/shorty/resources"
	"net/http"
)

type RoutingHandler struct {
	router Router
}

func MakeRoutingHandler(router Router) *RoutingHandler {
	return &RoutingHandler{router}
}

func (rh *RoutingHandler) SetRouter(r Router) {
	rh.router = r
}

func (rh *RoutingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var response *requests.Response
	if rh.router != nil {
		path := r.URL.Path
		match := rh.router.Route(path)
		if resource, ok := match.value.(resources.Resource); ok {
			req := &requests.Request{r, match.matches}
			switch r.Method {
			case "GET":
				response = resource.Get(req)
			case "POST":
				response = resource.Post(req)
			default:
				handleError(w, http.StatusMethodNotAllowed)
				return
			}
			handleResponse(w, r, response)
		} else {
			handleError(w, http.StatusNotFound)
		}
	} else {
		handleError(w, http.StatusNotFound)
	}
	return
}

func handleResponse(w http.ResponseWriter, r *http.Request, response *requests.Response) {
	switch response.StatusCode {
	case http.StatusTemporaryRedirect:
		fallthrough
	case http.StatusMovedPermanently:
		http.Redirect(w, r, response.Data, response.StatusCode)
	default:
		w.WriteHeader(response.StatusCode)
		fmt.Fprintf(w, response.Data)
	}
}

func handleError(w http.ResponseWriter, code int) {
	w.WriteHeader(code)
	fmt.Fprintf(w, http.StatusText(code))
}
{'fraction_non_alphanumeric': 0.12286282306163022, 'fraction_numerical': 0.0, 'mean_word_length': 4.230769230769231, 'pattern_counts': {'":': 4, '<': 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': '6686791', 'n_tokens_mistral': 932, 'n_tokens_neox': 870, 'n_words': 180}