<commit_before>package api

import (
	"fmt"
	"net/http"
	"regexp"
	"strings"
	"time"

	"github.com/af83/edwig/core"
	"github.com/af83/edwig/logger"
	"github.com/af83/edwig/model"
	"github.com/af83/edwig/siri"
)

type Server struct {
	model.UUIDConsumer
	model.ClockConsumer
	core.ReferentialsConsumer

	bind        string
	startedTime time.Time
}

func NewServer(bind string) *Server {
	server := Server{bind: bind}
	server.startedTime = server.Clock().Now()

	return &server
}

func (server *Server) ListenAndServe() error {
	http.HandleFunc("/", server.APIHandler)

	logger.Log.Debugf("Starting server on %s", server.bind)
	return http.ListenAndServe(server.bind, nil)
}

func (server *Server) APIHandler(response http.ResponseWriter, request *http.Request) {
	path := request.URL.Path
	pathRegexp := "/([0-9a-zA-Z-_]+)(?:/([0-9a-zA-Z-_]+))?(?:/([0-9a-zA-Z-]+))?"
	pattern := regexp.MustCompile(pathRegexp)
	foundStrings := pattern.FindStringSubmatch(path)
	if foundStrings[1] == "" {
		http.Error(response, "Invalid request", 400)
		return
	}

	response.Header().Set("Content-Type", "application/json")

	if foundStrings[2] == "siri" {
		server.handleCheckStatus(response, request, foundStrings[1])
	} else if strings.HasPrefix(foundStrings[1], "_") {
		server.handleControllers(response, request, foundStrings[1], foundStrings[2])
	} else {
		server.handleWithReferentialControllers(response, request, foundStrings[1], foundStrings[2], foundStrings[3])
	}
}
<commit_msg>fix error when accessing /<commit_after>package api

import (
	"fmt"
	"net/http"
	"regexp"
	"strings"
	"time"

	"github.com/af83/edwig/core"
	"github.com/af83/edwig/logger"
	"github.com/af83/edwig/model"
	"github.com/af83/edwig/siri"
)

type Server struct {
	model.UUIDConsumer
	model.ClockConsumer
	core.ReferentialsConsumer

	bind        string
	startedTime time.Time
}

func NewServer(bind string) *Server {
	server := Server{bind: bind}
	server.startedTime = server.Clock().Now()

	return &server
}

func (server *Server) ListenAndServe() error {
	http.HandleFunc("/", server.APIHandler)

	logger.Log.Debugf("Starting server on %s", server.bind)
	return http.ListenAndServe(server.bind, nil)
}

func (server *Server) APIHandler(response http.ResponseWriter, request *http.Request) {
	path := request.URL.Path
	pathRegexp := "/([0-9a-zA-Z-_]+)(?:/([0-9a-zA-Z-_]+))?(?:/([0-9a-zA-Z-]+))?"
	pattern := regexp.MustCompile(pathRegexp)
	foundStrings := pattern.FindStringSubmatch(path)
	if foundStrings == nil || foundStrings[1] == "" {
		http.Error(response, "Invalid request", 400)
		return
	}

	response.Header().Set("Content-Type", "application/json")

	if foundStrings[2] == "siri" {
		server.handleCheckStatus(response, request, foundStrings[1])
	} else if strings.HasPrefix(foundStrings[1], "_") {
		server.handleControllers(response, request, foundStrings[1], foundStrings[2])
	} else {
		server.handleWithReferentialControllers(response, request, foundStrings[1], foundStrings[2], foundStrings[3])
	}
}

{'fraction_non_alphanumeric': 0.16320375335120643, 'fraction_numerical': 0.01742627345844504, 'mean_word_length': 5.091836734693878, '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': '708558', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1063, 'n_words': 188}