<commit_before>package wrouter

import (
	"errors"
	"fmt"
	"io"
	"net/http"
	"reflect"
	"regexp"
	"strconv"
	"strings"
)

// Route represents one callable route
type Route struct {
	Methods    []string
	Controller interface{}
	RMethod    reflect.Method
	Path       string
}

// Add an HTTP Method to the routes
// Allowed methods: "get", "post", "put", "patch", "head", "trace", "connect", "options", "delete"
func (r *Route) AddMethod(method string) {
	r.Methods = append(r.Methods, method)
}

// Router represents one implementation of the http.Handler interface
type Router struct {
	routes      []*Route
	injectors   []Injector
	preRequest  []PreRequestEvent
	postRequest []PostRequestEvent
}

// Create a new Router
func NewRouter() *Router {
	return &Router{}
}

// ServeHTTP satisfies the http.Handler interface, so that this Router can be used as the
// second parameter of http.ListenAndServe
func (r *Router) ServeHTTP(w http.ResponseWriter, h *http.Request) {

	// Execute PreRequestEvents if any
	if len(r.preRequest) != 0 {
		ctx := createPreRequestEventContext(h, w)
		for _, event := range r.preRequest {
			event.Exec(ctx)
		}
	}

	route := r.findRequestRoute(h)
	if route == nil {
		// @TODO: Implement check if an ErrorController exists
		// Define Specifications for ErrorController (ex: StatusNotFoundAction??)
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("Not Found"))
		return
	}

	r.callRoute(route, w, h)
}

// PrintRoutes will print out all routes to io.Writer
func (r *Router) PrintRoutes(writer io.Writer) {
	for i, route := range r.routes {
		ms := ""
		for _, me := range route.Methods {
			ms += me + " "
		}
		fmt.Fprintln(writer, "ID: "+strconv.Itoa(i)+"\t"+ms+"\t\t"+route.Path)
	}
}

// AddController will add a new controller to the router<commit_msg>added clitable printing of routes to avoid indent issues<commit_after>package wrouter

import (
	"errors"
	"io"
	"net/http"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"github.com/owtorg/clitable"
)

// Route represents one callable route
type Route struct {
	Methods    []string
	Controller interface{}
	RMethod    reflect.Method
	Path       string
}

// Add an HTTP Method to the routes
// Allowed methods: "get", "post", "put", "patch", "head", "trace", "connect", "options", "delete"
func (r *Route) AddMethod(method string) {
	r.Methods = append(r.Methods, method)
}

// Router represents one implementation of the http.Handler interface
type Router struct {
	routes      []*Route
	injectors   []Injector
	preRequest  []PreRequestEvent
	postRequest []PostRequestEvent
}

// Create a new Router
func NewRouter() *Router {
	return &Router{}
}

// ServeHTTP satisfies the http.Handler interface, so that this Router can be used as the
// second parameter of http.ListenAndServe
func (r *Router) ServeHTTP(w http.ResponseWriter, h *http.Request) {

	// Execute PreRequestEvents if any
	if len(r.preRequest) != 0 {
		ctx := createPreRequestEventContext(h, w)
		for _, event := range r.preRequest {
			event.Exec(ctx)
		}
	}

	route := r.findRequestRoute(h)
	if route == nil {
		// @TODO: Implement check if an ErrorController exists
		// Define Specifications for ErrorController (ex: StatusNotFoundAction??)
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("Not Found"))
		return
	}

	r.callRoute(route, w, h)
}

// PrintRoutes will print out all routes to io.Writer
func (r *Router) PrintRoutes(writer io.Writer) {
	t := clitable.New()
	t.AddRow("ID", "METHODS", "PATH")
	for i, route := range r.routes {
		ms := ""
		for _, me := range route.Methods {
			ms += me + " "
		}
		t.AddRow(strconv.Itoa(i), ms ,route.Path)
	}
	t.Fprint(writer)
}

// AddController will add a new controller to the router
{'fraction_non_alphanumeric': 0.1291542826263172, 'fraction_numerical': 0.0005403944879762226, 'mean_word_length': 3.8203125, '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': '7071901', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1226, 'n_words': 379}