<commit_before>package main

import (
	"database/sql"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"runtime"
	"strings"

	"github.com/bgentry/speakeasy"

	"github.com/julienschmidt/httprouter"

	_ "github.com/lib/pq"
)

func post(res resource) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		dec := json.NewDecoder(r.Body.(io.Reader))
		var rep report
		err := dec.Decode(&rep)
		if err != nil {
			log.Printf("error decoding json from request: %v\n", err)
			http.Error(w, "Error. Sorry. Contact <EMAIL>. Error code A.", http.StatusBadRequest)
			return
		}

		rep.RemoteAddr = r.RemoteAddr
		if rep.Submitter != ps[0].Value {
			log.Printf("SearchReport and URL disagree about identity of submitter: SR says \"%v\", URL says \"%v\".\n", rep.Submitter, ps[0].Value)
		}
		go func(r report) {
			_, err := res.stmt.Exec(res.args(r)...)
			if err != nil {
				log.Printf("error writing to db: %v\n", err)
				return
			}
		}(rep)
	}
}

func get(stmt *sql.Stmt) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		users, err := getUserIDs(stmt, ps[0].Value)
		if err != nil {
			http.Error(w, "Error. Sorry. Contact <EMAIL>. Error code A.", http.StatusInternalServerError)
			return
		}
		enc := json.NewEncoder(w)
		err = enc.Encode(users)
		if err != nil {
			log.Printf("error encoding json and sending response: %v\n", err)
		}
	}
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	port := flag.String("port", ":8080", "port on which to run server")
	dbuser := flag.String("dbuser", "", "user for db connection")
	dbhost := flag.String("dbhost", "localhost:5432", "host and port of database server, formatted as 'host:port'")
	flag.Parse()
	dbpass, err := speakeasy.Ask("Enter password for database user '" + *dbuser + "':")
	if err != nil {
		log.Fatal(err)
	}
	credentials := mustParse(*dbuser, dbpass)
	db, err := sql.Open("postgres", "postgres://"+credentials+"@"+*dbhost+"/tsum?sslmode=disable")
	//	db, err := sql.Open("sqlite3", "users.db")
	if err != nil {
		log.Fatalf("Could not open db: %v", err)
	}

	strangers := mustPrepare(db, strangersSQL)
	reports := mustPrepare(db, reportsSQL)

	searches := resource{
		stmt: mustPrepare(db, searchSQL),
		args: report.searchArgs,
	}

	invites := resource{
		stmt: mustPrepare(db, inviteSQL),
		args: report.inviteArgs,
	}

	router := httprouter.New()

	router.GET("/tsum/:user/strangers/", get(strangers))
	router.GET("/tsum/:user/reports", get(reports))
	router.POST("/tsum/:user/reports/searches", post(searches))
	router.POST("/tsum/:user/reports/invites", post(invites))
	fmt.Println("Listening...")
	err = http.ListenAndServe(*port, router)
	if err != nil {
		fmt.Println(err)
	}
}

type report struct {
	Submitter  string
	UserID     string
	MID        string
	RemoteAddr string
}

type resource struct {<commit_msg>set up static file serving<commit_after>package main

import (
	"database/sql"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"runtime"
	"strings"

	"github.com/bgentry/speakeasy"

	"github.com/julienschmidt/httprouter"

	_ "github.com/lib/pq"
)

func post(res resource) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		dec := json.NewDecoder(r.Body.(io.Reader))
		var rep report
		err := dec.Decode(&rep)
		if err != nil {
			log.Printf("error decoding json from request: %v\n", err)
			http.Error(w, "Error. Sorry. Contact <EMAIL>. Error code A.", http.StatusBadRequest)
			return
		}

		rep.RemoteAddr = r.RemoteAddr
		if rep.Submitter != ps[0].Value {
			log.Printf("SearchReport and URL disagree about identity of submitter: SR says \"%v\", URL says \"%v\".\n", rep.Submitter, ps[0].Value)
		}
		go func(r report) {
			_, err := res.stmt.Exec(res.args(r)...)
			if err != nil {
				log.Printf("error writing to db: %v\n", err)
				return
			}
		}(rep)
	}
}

func get(stmt *sql.Stmt) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		users, err := getUserIDs(stmt, ps[0].Value)
		if err != nil {
			http.Error(w, "Error. Sorry. Contact <EMAIL>. Error code A.", http.StatusInternalServerError)
			return
		}
		enc := json.NewEncoder(w)
		err = enc.Encode(users)
		if err != nil {
			log.Printf("error encoding json and sending response: %v\n", err)
		}
	}
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	port := flag.String("port", ":8080", "port on which to run server")
	dbuser := flag.String("dbuser", "", "user for db connection")
	dbhost := flag.String("dbhost", "localhost:5432", "host and port of database server, formatted as 'host:port'")
	flag.Parse()
	dbpass, err := speakeasy.Ask("Enter password for database user '" + *dbuser + "':")
	if err != nil {
		log.Fatal(err)
	}
	credentials := mustParse(*dbuser, dbpass)
	db, err := sql.Open("postgres", "postgres://"+credentials+"@"+*dbhost+"/tsum?sslmode=disable")
	//	db, err := sql.Open("sqlite3", "users.db")
	if err != nil {
		log.Fatalf("Could not open db: %v", err)
	}

	strangers := mustPrepare(db, strangersSQL)
	reports := mustPrepare(db, reportsSQL)

	searches := resource{
		stmt: mustPrepare(db, searchSQL),
		args: report.searchArgs,
	}

	invites := resource{
		stmt: mustPrepare(db, inviteSQL),
		args: report.inviteArgs,
	}

	router := httprouter.New()

	router.GET("/tsum/:user/strangers/", get(strangers))
	router.GET("/tsum/:user/reports", get(reports))
	router.POST("/tsum/:user/reports/searches", post(searches))
	router.POST("/tsum/:user/reports/invites", post(invites))
	router.ServeFiles("/compiled/*filepath", http.Dir("$GOPATH/src/github.com/bcgraham/tsumtsum/ignore/compiled"))
	fmt.Println("Listening...")
	err = http.ListenAndServe(*port, router)
	if err != nil {
		fmt.Println(err)
	}
}

type report struct {
	Submitter  string
	UserID     string
	MID        string
	RemoteAddr string
}

type resource struct {
{'fraction_non_alphanumeric': 0.16849940788360684, 'fraction_numerical': 0.004060226695990526, 'mean_word_length': 3.939014202172097, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '3551382', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2165, 'n_words': 502}