<commit_before>package router

import (
	"net/http"
	"net/http/httputil"
	"strconv"
	"time"

	"github.com/Ennovar/gPanel/pkg/database"
	"log"
	"sync"
)

type Router struct {
	Port int
}

var server http.Server
var domainToPort map[string]int

var mutex = &sync.Mutex{}

func RefreshMap() bool {
	ds, err := database.Open("server/" + database.DB_DOMAINS)
	if err != nil {
		return false
	}
	defer ds.Close()

	var client map[string]database.Struct_Domain

	client, err = ds.ListDomains("*")
	if err != nil {
		return false
	}

	mutex.Lock()
	domainToPort = make(map[string]int)
	for k, v := range client {
		domainToPort[k] = v.PublicPort
	}
	mutex.Unlock()

	return true
}

func New() *Router {
	if !RefreshMap() {
		return nil
	}

	r := Router{
		Port: 2080,
	}

	server = http.Server{
		Addr: "localhost:" + strconv.Itoa(r.Port),
		Handler: &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				mutex.Lock()
				if d, ok := domainToPort[req.Host]; ok {
					req.Header.Set("Host", req.Host)
					req.URL.Scheme = "http"
					req.URL.Host = "127.0.0.1:" + strconv.Itoa(d)
				}
				mutex.Unlock()
			},
		},
		ReadTimeout:    5 * time.Second,
		WriteTimeout:   5 * time.Second,
		MaxHeaderBytes: 0,
	}

	// Start scheduled map refresher
	ticker := time.NewTicker(15 * time.Minute)
	go func() {
		for {
			select {
			case <- ticker.C:
				if !RefreshMap() {
					ticker.Stop()
					log.Fatal("Error refreshing domain/bundle pairing for router")
					return
				}
				log.Print("Successfully refreshed domain/bundle pairing map")
			}
		}<commit_msg>unlock was in the wrong place for mutex in router<commit_after>package router

import (
	"net/http"
	"net/http/httputil"
	"strconv"
	"time"

	"github.com/Ennovar/gPanel/pkg/database"
	"log"
	"sync"
)

type Router struct {
	Port int
}

var server http.Server
var domainToPort map[string]int

var mutex = &sync.Mutex{}

func RefreshMap() bool {
	ds, err := database.Open("server/" + database.DB_DOMAINS)
	if err != nil {
		return false
	}
	defer ds.Close()

	var client map[string]database.Struct_Domain

	client, err = ds.ListDomains("*")
	if err != nil {
		return false
	}

	mutex.Lock()
	domainToPort = make(map[string]int)
	for k, v := range client {
		domainToPort[k] = v.PublicPort
	}
	mutex.Unlock()

	return true
}

func New() *Router {
	if !RefreshMap() {
		return nil
	}

	r := Router{
		Port: 2080,
	}

	server = http.Server{
		Addr: "localhost:" + strconv.Itoa(r.Port),
		Handler: &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				mutex.Lock()
				if d, ok := domainToPort[req.Host]; ok {
					mutex.Unlock()
					req.Header.Set("Host", req.Host)
					req.URL.Scheme = "http"
					req.URL.Host = "127.0.0.1:" + strconv.Itoa(d)
				}
			},
		},
		ReadTimeout:    5 * time.Second,
		WriteTimeout:   5 * time.Second,
		MaxHeaderBytes: 0,
	}

	// Start scheduled map refresher
	ticker := time.NewTicker(15 * time.Minute)
	go func() {
		for {
			select {
			case <- ticker.C:
				if !RefreshMap() {
					ticker.Stop()
					log.Fatal("Error refreshing domain/bundle pairing for router")
					return
				}
				log.Print("Successfully refreshed domain/bundle pairing map")
			}
		}
{'fraction_non_alphanumeric': 0.14416693012962378, 'fraction_numerical': 0.0094846664558963, 'mean_word_length': 3.0720720720720722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '194117', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1189, 'n_words': 275}