<commit_before>package web

import (
	"crypto/tls"
	"log"
	"net"
	"net/http"
	"net/http/pprof"
)

func (s *Server) createServeMux(addr string) (*http.ServeMux, error) {
	mux := http.NewServeMux()
	mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
	mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
	mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
	mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
	mux.Handle("/", s)

	l, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal("Listen:", err)
	}
	s.l = l

	return mux, err
}

//Runs the web application and serves http requests
func (s *Server) Run(addr string) {
	s.initServer()

	mux := http.NewServeMux()
	mux.Handle("/", s)

	s.Logger.Printf("web.go serving %s\n", addr)

	l, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}
	s.l = l
	err = http.Serve(s.l, mux)
	s.l.Close()
}

//Runs the secure web application and serves https requests
func (s *Server) RunSecure(addr string, config tls.Config) error {
	s.initServer()
	mux := http.NewServeMux()
	mux.Handle("/", s)

	l, err := tls.Listen("tcp4", addr, &config)
	if err != nil {
		return err
	}

	s.l = l
	return http.Serve(s.l, mux)
}
func (s *Server) RunTLS(addr string, cert string, key string) {
	s.initServer()

	mux := http.NewServeMux()
	mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
	mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
	mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
	mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
	mux.Handle("/", s)

	s.Logger.Printf("web.go serving %s\n", addr)
	/*
	   l, err := net.Listen("tcp", addr)
	   if err != nil {
	       log.Fatal("ListenAndServe:", err)
	   }
	   s.l = l
	   err = http.Serve(s.l, mux)
	   s.l.Close()
	*/
	err := http.ListenAndServeTLS(addr, cert, key, mux)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}

}

//Runs the web application and serves http requests
func Run(addr string) {
	mainServer.Run(addr)
}

//Runs the secure web application and serves https requests
func RunSecure(addr string, config tls.Config) {
	mainServer.RunSecure(addr, config)
}

func (s *Server) runTLS(addr, certFile, keyFile string) {
	s.initServer()

	mux, err := s.createServeMux(addr)
	s.Logger.Printf("web.go serving with TLS %s\n", addr)

	srv := &http.Server{Handler: mux}

	config := &tls.Config{}
	config.Certificates = make([]tls.Certificate, 1)
	config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)

	if err != nil {
		log.Fatal("TLS error:", err)
	}

	tlsListener := tls.NewListener(s.l, config)
	err = srv.Serve(tlsListener)
	s.l.Close()
}

func RunTLS(addr, certFile, keyFile string) {
	mainServer.runTLS(addr, certFile, keyFile)
}
<commit_msg>Return error if Run fails + more reorganizations<commit_after>package web

import (
	"crypto/tls"
	"net"
	"net/http"
	"net/http/pprof"
)

// Create a http.Handler from this server that also provides some debug
// resources
func (s *Server) createDebugHandler() http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
	mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
	mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
	mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
	mux.Handle("/", s)
	return mux
}

// Runs the web application and serves http requests
func (s *Server) Run(addr string) error {
	s.initServer()
	l, err := net.Listen("tcp", addr)
	if err != nil {
		s.Logger.Print("run web.go: ", err)
		return err
	}
	defer l.Close()
	s.Logger.Printf("web.go serving %s\n", addr)
	s.l = l
	return http.Serve(l, s)
}

//Runs the secure web application and serves https requests
func (s *Server) RunSecure(addr string, config tls.Config) error {
	s.initServer()
	l, err := tls.Listen("tcp4", addr, &config)
	if err != nil {
		s.Logger.Print("run web.go: ", err)
		return err
	}
	defer l.Close()
	s.l = l
	return http.Serve(l, s)
}

func (s *Server) RunTLS(addr string, cert string, key string) error {
	s.initServer()
	debugh := s.createDebugHandler()
	s.Logger.Print("web.go serving ", addr)
	err := http.ListenAndServeTLS(addr, cert, key, debugh)
	if err != nil {
		s.Logger.Print("ListenAndServe: ", err)
		return err
	}
	return nil
}

func (s *Server) runTLS(addr, certFile, keyFile string) error {
	s.initServer()
	l, err := net.Listen("tcp", addr)
	if err != nil {
		s.Logger.Print("start web.go: ", err)
		return err
	}
	defer l.Close()
	s.l = l
	debugh := s.createDebugHandler()
	s.Logger.Print("web.go serving with TLS ", addr)

	srv := &http.Server{Handler: debugh}

	config := &tls.Config{}
	config.Certificates = make([]tls.Certificate, 1)
	config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)

	if err != nil {
		s.Logger.Print("TLS error: ", err)
		return err
	}

	tlsListener := tls.NewListener(s.l, config)
	return srv.Serve(tlsListener)
}

//Runs the web application and serves http requests
func Run(addr string) {
	mainServer.Run(addr)
}

//Runs the secure web application and serves https requests
func RunSecure(addr string, config tls.Config) {
	mainServer.RunSecure(addr, config)
}

func RunTLS(addr, certFile, keyFile string) {
	mainServer.runTLS(addr, certFile, keyFile)
}

{'fraction_non_alphanumeric': 0.1630659402592523, 'fraction_numerical': 0.0022543678376855157, 'mean_word_length': 4.085004775549188, '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': '5942624', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2020, 'n_words': 486}