<commit_before>package main

import (
	"bytes"
	"log"
	"net/http"
	"net/http/httptest"
	"strconv"
	"time"

	"github.com/gorilla/websocket"
	"github.com/radovskyb/watcher"
	"github.com/sontags/env"
	"github.com/unprofession-al/noip"
)

const (
	unset = "UNSET"
)

var listener, logging, dir, user, pass, host, interval, watch string

var watchChan chan string

func logger(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		handler.ServeHTTP(w, r)
		if logging != unset {
			log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
		}
	})
}

type InjectorMiddleware struct {
	handler http.Handler
}

func (m *InjectorMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	rec := httptest.NewRecorder()
	m.handler.ServeHTTP(rec, r)
	for k, v := range rec.Header() {
		w.Header()[k] = v
	}
	out := bytes.Replace(rec.Body.Bytes(), []byte("</body>"), []byte(`<script>
 (function() {
				var conn = new WebSocket("ws://127.0.0.1:8989/ws");
                conn.onclose = function(evt) {
					console.log('Connection closed');
                }
                conn.onmessage = function(evt) {
                    location.reload(true);
                }
            })();
	</script></body>`), -1)
	w.Header().Set("Content-Length", strconv.Itoa(len(out)))
	w.Write(out)
}

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
}

func serveWs(w http.ResponseWriter, r *http.Request) {
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		if _, ok := err.(websocket.HandshakeError); !ok {
			log.Println(err)
		}
		return
	}

	go func() {
		for {
			desc, ok := <-watchChan
<commit_msg>make the injected js disconnect the websocket on reload<commit_after>package main

import (
	"bytes"
	"log"
	"net/http"
	"net/http/httptest"
	"strconv"
	"time"

	"github.com/gorilla/websocket"
	"github.com/radovskyb/watcher"
	"github.com/sontags/env"
	"github.com/unprofession-al/noip"
)

const (
	unset = "UNSET"
)

var listener, logging, dir, user, pass, host, interval, watch string

var watchChan chan string

func logger(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		handler.ServeHTTP(w, r)
		if logging != unset {
			log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
		}
	})
}

type InjectorMiddleware struct {
	handler http.Handler
}

func (m *InjectorMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	rec := httptest.NewRecorder()
	m.handler.ServeHTTP(rec, r)
	for k, v := range rec.Header() {
		w.Header()[k] = v
	}
	out := bytes.Replace(rec.Body.Bytes(), []byte("</body>"), []byte(`<script>
 (function() {
				var conn = new WebSocket("ws://127.0.0.1:8989/ws");
                conn.onclose = function(evt) {
					console.log('Connection closed');
                }
                conn.onmessage = function(evt) {
                	conn.close();
                    location.reload(true);
                }
            })();
	</script></body>`), -1)
	w.Header().Set("Content-Length", strconv.Itoa(len(out)))
	w.Write(out)
}

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
}

func serveWs(w http.ResponseWriter, r *http.Request) {
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		if _, ok := err.(websocket.HandshakeError); !ok {
			log.Println(err)
		}
		return
	}

	go func() {
		for {
			desc, ok := <-watchChan

{'fraction_non_alphanumeric': 0.16400116312881652, 'fraction_numerical': 0.011049723756906077, 'mean_word_length': 3.0855106888361044, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2025121', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1272, 'n_words': 261}