<commit_before>package main

import (
	"crypto/tls"
	"log"
	"net"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"github.com/lair-framework/api-server/app"
	"github.com/lair-framework/api-server/handlers"
	"github.com/lair-framework/api-server/middleware"
	"gopkg.in/mgo.v2"
)

// TLSDial sets up a TLS connection to MongoDb.
func TLSDial(addr net.Addr) (net.Conn, error) {
	return tls.Dial(addr.Network(), addr.String(), &tls.Config{InsecureSkipVerify: true})
}
func main() {
	murl := os.Getenv("MONGO_URL")
	if murl == "" {
		log.Fatal("MONGO_URL environment variable not set")

	}
	apiListener := os.Getenv("API_LISTENER")
	if apiListener == "" {
		log.Fatal("API_LISTENER environment variable not set")
	}

	u, err := url.Parse(murl)
	if err != nil {
		log.Fatal("Erorr parsing MONGO_URL", err.Error())
	}
	q, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		log.Fatal("Error parsing query parameters", err.Error())
	}
	dname := u.Path[1:]
	s := &mgo.Session{}
	log.Printf("Attempting to connect to database %s%s\n", u.Host, u.Path)
	if opt, ok := q["ssl"]; ok && opt[0] == "true" {
		var user, pass string
		if u.User != nil {
			user = u.User.Username()
			p, set := u.User.Password()
			if set {
				pass = p
			}
		}
		d := &mgo.DialInfo{
			Addrs:    []string{u.Host},
			Direct:   true,
			Database: dname,
			Username: user,
			Password: <PASSWORD>,
			Dial:     TLSDial,
			Timeout:  time.Duration(10) * time.Second,
		}
		s, err = mgo.DialWithInfo(d)
		if err != nil {
			log.Fatal("Could not connect to database. Error: ", err.Error())
		}
	} else {
		s, err = mgo.Dial(murl)
		if err != nil {
			log.Fatal("Could not connect to database. Error: ", err.Error())
		}
	}
	log.Println("Successfully connected to database")

	log.Println("Starting drone API server")

	a := app.New()

	importRouter := mux.NewRouter()
	importRouter.HandleFunc("/api/projects/{pid}", handlers.UpdateProject(a)).Methods("PATCH")
	importRouter.HandleFunc("/api/projects/{pid}", handlers.ShowProject(a)).Methods("GET")
	importRouter.HandleFunc("/api/projects", handlers.IndexProject(a)).Methods("GET")

	negImport := negroni.New(
		negroni.NewLogger(),
		negroni.NewRecovery(),
	)
	negImport.Use(middleware.MongoMiddleware(s, dname))
	negImport.Use(middleware.AuthMiddleware(a))
	negImport.UseHandler(importRouter)

	router := mux.NewRouter()
	router.Handle("/api/projects/{pid}", negImport)
	router.Handle("/api/projects", negImport)
	server := negroni.New(
		negroni.NewLogger(),
		negroni.NewRecovery(),
	)
	server.UseHandler(router)

	log.Fatal(http.ListenAndServe(apiListener, server))
}
<commit_msg>Prevent handlers from running twice<commit_after>package main

import (
	"crypto/tls"
	"log"
	"net"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"github.com/lair-framework/api-server/app"
	"github.com/lair-framework/api-server/handlers"
	"github.com/lair-framework/api-server/middleware"
	"gopkg.in/mgo.v2"
)

// TLSDial sets up a TLS connection to MongoDb.
func TLSDial(addr net.Addr) (net.Conn, error) {
	return tls.Dial(addr.Network(), addr.String(), &tls.Config{InsecureSkipVerify: true})
}
func main() {
	murl := os.Getenv("MONGO_URL")
	if murl == "" {
		log.Fatal("MONGO_URL environment variable not set")

	}
	apiListener := os.Getenv("API_LISTENER")
	if apiListener == "" {
		log.Fatal("API_LISTENER environment variable not set")
	}

	u, err := url.Parse(murl)
	if err != nil {
		log.Fatal("Erorr parsing MONGO_URL", err.Error())
	}
	q, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		log.Fatal("Error parsing query parameters", err.Error())
	}
	dname := u.Path[1:]
	s := &mgo.Session{}
	log.Printf("Attempting to connect to database %s%s\n", u.Host, u.Path)
	if opt, ok := q["ssl"]; ok && opt[0] == "true" {
		var user, pass string
		if u.User != nil {
			user = u.User.Username()
			p, set := u.User.Password()
			if set {
				pass = p
			}
		}
		d := &mgo.DialInfo{
			Addrs:    []string{u.Host},
			Direct:   true,
			Database: dname,
			Username: user,
			Password: <PASSWORD>,
			Dial:     TLSDial,
			Timeout:  time.Duration(10) * time.Second,
		}
		s, err = mgo.DialWithInfo(d)
		if err != nil {
			log.Fatal("Could not connect to database. Error: ", err.Error())
		}
	} else {
		s, err = mgo.Dial(murl)
		if err != nil {
			log.Fatal("Could not connect to database. Error: ", err.Error())
		}
	}
	log.Println("Successfully connected to database")

	log.Println("Starting drone API server")

	a := app.New()

	api := mux.NewRouter()
	api.HandleFunc("/api/projects/{pid}", handlers.UpdateProject(a)).Methods("PATCH")
	api.HandleFunc("/api/projects/{pid}", handlers.ShowProject(a)).Methods("GET")
	api.HandleFunc("/api/projects", handlers.IndexProject(a)).Methods("GET")

	n := negroni.New(
		negroni.NewLogger(),
		negroni.NewRecovery(),
	)
	n.Use(middleware.MongoMiddleware(s, dname))
	n.Use(middleware.AuthMiddleware(a))
	n.UseHandler(api)

	log.Fatal(http.ListenAndServe(apiListener, n))
}

{'fraction_non_alphanumeric': 0.1733758469509765, 'fraction_numerical': 0.0019928258270227183, 'mean_word_length': 3.9399606299212597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4747095', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1903, 'n_words': 379}