<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"github.com/dustin/go-humanize"
	"github.com/flosch/pongo2"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"net/url"
	"strconv"
	"time"
)

type Server struct {
	Url  string
	Path string
}

type Result struct {
	Servers   []Server
	Filename  string
	Size      uint64
	HumanSize string
}

type SearchConfig struct {
	Frontend *Frontend
}

type Search struct {
	cfg  SearchConfig
	tmpl *pongo2.Template
}

func CreateSearch(cfg SearchConfig) (search *Search, err error) {
	search = &Search{cfg: cfg}

	// Load the results template
	search.tmpl, err = search.cfg.Frontend.templates.FromFile("results.tmpl")
	if err != nil {
		return
	}

	return
}

func (search *Search) Handler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	start := time.Now()

	// Catch errors in the following code, log them and return a HTTP 500
	defer func() {
		if err := recover(); err != nil {
			search.cfg.Frontend.Log.Println(err)
			http.Error(w, fmt.Sprintf("%s", err), http.StatusInternalServerError)
		}
	}()

	/* Parse GET parameters
	 * q: Search query
	 * p: Current page. Is zero if not a number<commit_msg>Frontend: Handler error catcher: Use cast to (error) again to get rid of fmt. Reverts 528eeb8 a bit<commit_after>package main

import (
	"encoding/json"
	"github.com/dustin/go-humanize"
	"github.com/flosch/pongo2"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"net/url"
	"strconv"
	"time"
)

type Server struct {
	Url  string
	Path string
}

type Result struct {
	Servers   []Server
	Filename  string
	Size      uint64
	HumanSize string
}

type SearchConfig struct {
	Frontend *Frontend
}

type Search struct {
	cfg  SearchConfig
	tmpl *pongo2.Template
}

func CreateSearch(cfg SearchConfig) (search *Search, err error) {
	search = &Search{cfg: cfg}

	// Load the results template
	search.tmpl, err = search.cfg.Frontend.templates.FromFile("results.tmpl")
	if err != nil {
		return
	}

	return
}

func (search *Search) Handler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	start := time.Now()

	// Catch errors in the following code, log them and return a HTTP 500
	defer func() {
		if err, ok := recover().(error); ok {
			search.cfg.Frontend.Log.Println(err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}()

	/* Parse GET parameters
	 * q: Search query
	 * p: Current page. Is zero if not a number
{'fraction_non_alphanumeric': 0.11646090534979424, 'fraction_numerical': 0.007407407407407408, 'mean_word_length': 3.7666666666666666, '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': '1411789', 'n_tokens_mistral': 887, 'n_tokens_neox': 847, 'n_words': 244}