<commit_before>package main

import (
	"flag"
	"fmt"
	"github.com/Clever/sphinx"
	"github.com/Clever/sphinx/handlers"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
)

var (
	configfile = flag.String("config", "example.yaml", "/path/to/configuration.yaml")
	validate   = flag.Bool("validate", false, "Validate configuration and exit")
)

type Daemon struct {
	config      sphinx.Configuration
	ratelimiter sphinx.RateLimiter
	proxy       httputil.ReverseProxy
	handler     http.Handler
}

func (d *Daemon) Start() {
	log.Printf("Listening on %s", d.config.Proxy.Listen)
	log.Fatal(http.ListenAndServe(d.config.Proxy.Listen, d.handler))
	return
}

func NewDaemon(config sphinx.Configuration) (Daemon, error) {

	ratelimiter, err := sphinx.NewRateLimiter(config)
	if err != nil {
		return Daemon{}, fmt.Errorf("SPHINX_INIT_FAILED: %s", err.Error())
	}

	target, _ := url.Parse(config.Proxy.Host) // already tested for invalid Host
	proxy := httputil.NewSingleHostReverseProxy(target)

	var httplimiter http.Handler
	switch config.Proxy.Handler {
	case "http":
		httplimiter = handlers.NewHTTPLimiter(ratelimiter, proxy)
	case "httplogger":
		httplimiter = handlers.NewHTTPLogger(ratelimiter, proxy)
	default:
		return Daemon{}, fmt.Errorf("Sphinx only supports the http handler")
	}

	return Daemon{
		config:      config,
		ratelimiter: ratelimiter,
		handler:     httplimiter,
	}, nil

}

func main() {

	flag.Parse()

	config, err := sphinx.NewConfiguration(*configfile)
	if err != nil {
		log.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
	}

	daemon, err := NewDaemon(config)
	if err != nil {
		log.Fatal(err)
	}

	if *validate {
		print("Configuration parsed and Sphinx loaded fine. not starting dameon.")
		return
	}
<commit_msg>better message for invalid handler<commit_after>package main

import (
	"flag"
	"fmt"
	"github.com/Clever/sphinx"
	"github.com/Clever/sphinx/handlers"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
)

var (
	configfile = flag.String("config", "example.yaml", "/path/to/configuration.yaml")
	validate   = flag.Bool("validate", false, "Validate configuration and exit")
)

type Daemon struct {
	config      sphinx.Configuration
	ratelimiter sphinx.RateLimiter
	proxy       httputil.ReverseProxy
	handler     http.Handler
}

func (d *Daemon) Start() {
	log.Printf("Listening on %s", d.config.Proxy.Listen)
	log.Fatal(http.ListenAndServe(d.config.Proxy.Listen, d.handler))
	return
}

func NewDaemon(config sphinx.Configuration) (Daemon, error) {

	ratelimiter, err := sphinx.NewRateLimiter(config)
	if err != nil {
		return Daemon{}, fmt.Errorf("SPHINX_INIT_FAILED: %s", err.Error())
	}

	target, _ := url.Parse(config.Proxy.Host) // already tested for invalid Host
	proxy := httputil.NewSingleHostReverseProxy(target)

	var httplimiter http.Handler
	switch config.Proxy.Handler {
	case "http":
		httplimiter = handlers.NewHTTPLimiter(ratelimiter, proxy)
	case "httplogger":
		httplimiter = handlers.NewHTTPLogger(ratelimiter, proxy)
	default:
		return Daemon{}, fmt.Errorf("Unrecognized handler %s", config.Proxy.Handler)
	}

	return Daemon{
		config:      config,
		ratelimiter: ratelimiter,
		handler:     httplimiter,
	}, nil

}

func main() {

	flag.Parse()

	config, err := sphinx.NewConfiguration(*configfile)
	if err != nil {
		log.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
	}

	daemon, err := NewDaemon(config)
	if err != nil {
		log.Fatal(err)
	}

	if *validate {
		print("Configuration parsed and Sphinx loaded fine. not starting dameon.")
		return
	}

{'fraction_non_alphanumeric': 0.13238231098430814, 'fraction_numerical': 0.0, 'mean_word_length': 4.320182094081942, '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': '5469485', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1273, 'n_words': 270}