<commit_before>package main

import (
	"flag"
	"net/http"
	"os"

	"github.com/golang/glog"
	"github.com/gorilla/mux"
)

var (
	binding = flag.String("binding", ":443", "interface and port to bind to")
)

type fallibleHandler func(w http.ResponseWriter, r *http.Request) error

func catchError(fn fallibleHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if err := fn(w, r); err != nil {
			glog.Warningf("Error for %q: %v", r.URL.String(), err)
			http.Error(w, "You broke the internet 🐮💩😱", http.StatusInternalServerError)
		}
	}
}

func main() {
	flag.Parse()
	stateStore := newSecureCookieStore(
		[]byte(os.Getenv("COOKIE_HASH_KEY")),
		[]byte(os.Getenv("COOKIE_BLOCK_KEY")),
	)
	credStore, err := newBoltCredentialsStore(os.Getenv("CRED_STORE"))
	if err != nil {
		glog.Fatal(err)
	}
	oauthManager := newOauthFactory(
		os.Getenv("GITHUB_CLIENT_KEY"),
		os.Getenv("GITHUB_CLIENT_SECRET"),
	)

	h := &handler{
		state: stateStore,
		creds: credStore,
		oauth: oauthManager,
	}
	router := mux.NewRouter()
	router.HandleFunc("/", catchError(serveIndex)).Methods("GET")
	router.HandleFunc("/submit", catchError(h.submit)).Methods("POST")
	router.HandleFunc("/callback", catchError(h.callback)).Methods("GET")
	router.HandleFunc("/webhook", catchError(h.webhook)).Methods("POST")
	glog.Infof("Listening on %s", *binding)
	// TODO: implement TLS as an option.
	glog.Fatal(
		http.ListenAndServeTLS(
			*binding,
			os.Getenv("SSH_CERT"),
			os.Getenv("SSH_KEY"),
			router,
		),
	)
}
<commit_msg>Redirect to SSH, allow HTTP locally<commit_after>package main

import (
	"flag"
	"net/http"
	"os"

	"github.com/golang/glog"
	"github.com/gorilla/mux"
)

var (
	binding = flag.String("binding", ":443", "interface and port to bind to")
	devMode = flag.Bool("dev_mode", false, "Are you in Developmet mode?")
)

type fallibleHandler func(w http.ResponseWriter, r *http.Request) error

func catchError(fn fallibleHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if err := fn(w, r); err != nil {
			glog.Warningf("Error for %q: %v", r.URL.String(), err)
			http.Error(w, "You broke the internet 🐮💩😱", http.StatusInternalServerError)
		}
	}
}

func redirectToHTTPS(w http.ResponseWriter, r *http.Request) {
	r.URL.Scheme = "https"
	http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
}

func startServer(router *mux.Router) error {
	if *devMode {
		return http.ListenAndServe(*binding, router)
	}
	httpMux := http.NewServeMux()
	httpMux.HandleFunc("/", redirectToHTTPS)
	go http.ListenAndServe(":80", httpMux)
	return http.ListenAndServeTLS(
		*binding,
		os.Getenv("SSH_CERT"),
		os.Getenv("SSH_KEY"),
		router,
	)
}

func main() {
	flag.Parse()
	stateStore := newSecureCookieStore(
		[]byte(os.Getenv("COOKIE_HASH_KEY")),
		[]byte(os.Getenv("COOKIE_BLOCK_KEY")),
	)
	credStore, err := newBoltCredentialsStore(os.Getenv("CRED_STORE"))
	if err != nil {
		glog.Fatal(err)
	}
	oauthManager := newOauthFactory(
		os.Getenv("GITHUB_CLIENT_KEY"),
		os.Getenv("GITHUB_CLIENT_SECRET"),
	)

	h := &handler{
		state: stateStore,
		creds: credStore,
		oauth: oauthManager,
	}
	router := mux.NewRouter()
	router.HandleFunc("/", catchError(serveIndex)).Methods("GET")
	router.HandleFunc("/submit", catchError(h.submit)).Methods("POST")
	router.HandleFunc("/callback", catchError(h.callback)).Methods("GET")
	router.HandleFunc("/webhook", catchError(h.webhook)).Methods("POST")
	glog.Infof("Listening on %s", *binding)
	// TODO: implement TLS as an option.
	glog.Fatal(startServer(router))
}

{'fraction_non_alphanumeric': 0.16835585585585586, 'fraction_numerical': 0.0022522522522522522, 'mean_word_length': 5.042517006802721, 'pattern_counts': {'":': 3, '<': 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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5130158', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1348, 'n_words': 251}