<commit_before>package server

import (
	"io/ioutil"
	"log"
	"net/http"
	"path"
	"runtime"

	"github.com/asciimoo/privacyscore/checker"
	"github.com/asciimoo/privacyscore/scoredb"
)

var (
	milligramCSS []byte
	milligramURL string = "https://milligram.github.io/css/milligram.min.css"
)

var BASE_DIR string

func init() {
	_, filename, _, ok := runtime.Caller(0)
	if !ok {
		log.Fatal("No caller information")
	}
	BASE_DIR = path.Dir(filename)
	resp, err := http.Get(milligramURL)
	if err != nil {
		log.Fatal("Cannot fetch milligram.css:", err)
	}
	milligramCSS, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal("Cannot fetch milligram.css:", err)
	}
	resp.Body.Close()
	initTemplates()
}

func Run(listen *string) error {
	log.Println("listen on", *listen)
	return http.ListenAndServe(*listen, http.HandlerFunc(requestRouter))
}

func requestRouter(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("X-Xss-Protection", "1; mode=block")
	w.Header().Set("X-Content-Type-Options", "nosniff")
	switch r.URL.Path {
	case "/":
		serveIndexPage(w, r)
	case "/about":
		serveAboutPage(w, r)
	case "/check":
		checkURL(w, r)
	case "/static/milligram.min.css":
		serveMilligramCSS(w, r)
	default:
		http.NotFound(w, r)
	}
}

func serveIndexPage(w http.ResponseWriter, request *http.Request) {
	renderTemplate(w, "index.tpl", struct {
		Stats          []*scoredb.ScoreCount
		StatEntryCount uint
	}{scoredb.GetAll(), scoredb.GetTopEntryCount()})
}

func serveAboutPage(w http.ResponseWriter, request *http.Request) {
	renderTemplate(w, "about.tpl", nil)
}

func checkURL(w http.ResponseWriter, request *http.Request) {
	url := request.FormValue("url")
	results, err := checker.Run(url)
	if err != nil {<commit_msg>[enh] set X-Frame-Options DENY<commit_after>package server

import (
	"io/ioutil"
	"log"
	"net/http"
	"path"
	"runtime"

	"github.com/asciimoo/privacyscore/checker"
	"github.com/asciimoo/privacyscore/scoredb"
)

var (
	milligramCSS []byte
	milligramURL string = "https://milligram.github.io/css/milligram.min.css"
)

var BASE_DIR string

func init() {
	_, filename, _, ok := runtime.Caller(0)
	if !ok {
		log.Fatal("No caller information")
	}
	BASE_DIR = path.Dir(filename)
	resp, err := http.Get(milligramURL)
	if err != nil {
		log.Fatal("Cannot fetch milligram.css:", err)
	}
	milligramCSS, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal("Cannot fetch milligram.css:", err)
	}
	resp.Body.Close()
	initTemplates()
}

func Run(listen *string) error {
	log.Println("listen on", *listen)
	return http.ListenAndServe(*listen, http.HandlerFunc(requestRouter))
}

func requestRouter(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("X-Xss-Protection", "1; mode=block")
	w.Header().Set("X-Content-Type-Options", "nosniff")
	w.Header().Set("X-Frame-Options", "DENY")
	switch r.URL.Path {
	case "/":
		serveIndexPage(w, r)
	case "/about":
		serveAboutPage(w, r)
	case "/check":
		checkURL(w, r)
	case "/static/milligram.min.css":
		serveMilligramCSS(w, r)
	default:
		http.NotFound(w, r)
	}
}

func serveIndexPage(w http.ResponseWriter, request *http.Request) {
	renderTemplate(w, "index.tpl", struct {
		Stats          []*scoredb.ScoreCount
		StatEntryCount uint
	}{scoredb.GetAll(), scoredb.GetTopEntryCount()})
}

func serveAboutPage(w http.ResponseWriter, request *http.Request) {
	renderTemplate(w, "about.tpl", nil)
}

func checkURL(w http.ResponseWriter, request *http.Request) {
	url := request.FormValue("url")
	results, err := checker.Run(url)
	if err != nil {
{'fraction_non_alphanumeric': 0.16201594533029612, 'fraction_numerical': 0.0011389521640091116, 'mean_word_length': 4.567353407290016, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2832503', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1351, 'n_words': 261}