<commit_before>package main

import (
	
	"code.google.com/p/gorilla/mux"
	"encoding/json"
	"fmt"
	"github.com/darkhelmet/env"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func webRespond(resp http.ResponseWriter, status int, data interface{}) {
	b, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		panic(err)
	}
	resp.Header().Set("Content-Type", "application/json; charset=utf-8")
	resp.WriteHeader(status)
	resp.Write(b)
	resp.Write([]byte("\n"))
}

func webPinList(resp http.ResponseWriter, req *http.Request) {
	pins, err := dataPinList()
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pins)
}

func webPinCreate(resp http.ResponseWriter, req *http.Request) {
	pinReq := pin{}
	err := json.NewDecoder(req.Body).Decode(&pinReq)
	if err != nil {
		err = pgpinError{Id: "bad-request", Message: "malformed JSON body"}
		webErr(resp, err)
		return
	}
	pin, err := dataPinCreate(pinReq.DbId, pinReq.Name, pinReq.Query)
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pin)
}

func webPinGet(resp http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	pin, err := dataPinGet(id)
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pin)
}

func webPinDestroy(resp http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	pin, err := dataPinGet(id)
	if err != nil {
		webErr(resp, err)
		return
	}
	err = dataPinDelete(pin)
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pin)
}

func webStatus(resp http.ResponseWriter, req *http.Request) {
	err := dataTest()
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, &map[string]string{"message": "ok"})
}

func webNotFound(resp http.ResponseWriter, req *http.Request) {
	err := pgpinError{Id: "not-found", Message: "not found"}
	webErr(resp, err)
}

func webErr(resp http.ResponseWriter, err error) {
	fmt.Println("error:", err)
	switch err.(type) {
	case pgpinError:
		webRespond(resp, 500, err)
	default:
		webRespond(resp, 500, &map[string]string{"id": "internal-error", "message": "internal server error"})
	}
}

func webRouterHandler() http.HandlerFunc {
	router := mux.NewRouter()
	router.HandleFunc("/pins", webPinList).Methods("GET")
	router.HandleFunc("/pins", webPinCreate).Methods("POST")
	router.HandleFunc("/pins/{id}", webPinGet).Methods("GET")
	router.HandleFunc("/pins/{id}", webPinDestroy).Methods("DELETE")
	router.HandleFunc("/status", webStatus).Methods("GET")
	router.NotFoundHandler = http.HandlerFunc(webNotFound)
	return func(res http.ResponseWriter, req *http.Request) {
		router.ServeHTTP(res, req)
	}
}

type webStatusingResponseWriter struct {
	status int
	http.ResponseWriter
}

func (w *webStatusingResponseWriter) WriteHeader(s int) {
	w.status = s
	w.ResponseWriter.WriteHeader(s)
}

func webWrapLogging(f http.HandlerFunc) http.HandlerFunc {
	return func(res http.ResponseWriter, req *http.Request) {
		start := time.Now()
		method := req.Method
		path := req.URL.Path
		log("web.request.start", "method=%s path=%s", method, path)
		wres := webStatusingResponseWriter{-1, res}
		f(&wres, req)
		elapsed := float64(time.Since(start)) / 1000000.0
		log("web.request.finish", "method=%s path=%s status=%d elapsed=%f", method, path, wres.status, elapsed)
	}
}

func webTrap() {
	log("web.trap.set")
	trap := make(chan os.Signal)
	go func() {
		<- trap
		log("web.trap")
		log("web.exit")
		os.Exit(0)
	}()
	signal.Notify(trap, syscall.SIGINT, syscall.SIGTERM)
}

func webStart() {
	log("web.start")
	dataStart()
	handler := webRouterHandler()<commit_msg>no need<commit_after>package main

import (
	
	"code.google.com/p/gorilla/mux"
	"encoding/json"
	"fmt"
	"github.com/darkhelmet/env"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func webRespond(resp http.ResponseWriter, status int, data interface{}) {
	b, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		panic(err)
	}
	resp.Header().Set("Content-Type", "application/json; charset=utf-8")
	resp.WriteHeader(status)
	resp.Write(b)
	resp.Write([]byte("\n"))
}

func webPinList(resp http.ResponseWriter, req *http.Request) {
	pins, err := dataPinList()
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pins)
}

func webPinCreate(resp http.ResponseWriter, req *http.Request) {
	pinReq := pin{}
	err := json.NewDecoder(req.Body).Decode(&pinReq)
	if err != nil {
		err = pgpinError{Id: "bad-request", Message: "malformed JSON body"}
		webErr(resp, err)
		return
	}
	pin, err := dataPinCreate(pinReq.DbId, pinReq.Name, pinReq.Query)
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pin)
}

func webPinGet(resp http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	pin, err := dataPinGet(id)
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pin)
}

func webPinDestroy(resp http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	pin, err := dataPinGet(id)
	if err != nil {
		webErr(resp, err)
		return
	}
	err = dataPinDelete(pin)
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, pin)
}

func webStatus(resp http.ResponseWriter, req *http.Request) {
	err := dataTest()
	if err != nil {
		webErr(resp, err)
		return
	}
	webRespond(resp, 200, &map[string]string{"message": "ok"})
}

func webNotFound(resp http.ResponseWriter, req *http.Request) {
	err := pgpinError{Id: "not-found", Message: "not found"}
	webErr(resp, err)
}

func webErr(resp http.ResponseWriter, err error) {
	fmt.Println("error:", err)
	switch err.(type) {
	case pgpinError:
		webRespond(resp, 500, err)
	default:
		webRespond(resp, 500, &map[string]string{"id": "internal-error", "message": "internal server error"})
	}
}

func webRouterHandler() http.HandlerFunc {
	router := mux.NewRouter()
	router.HandleFunc("/pins", webPinList).Methods("GET")
	router.HandleFunc("/pins", webPinCreate).Methods("POST")
	router.HandleFunc("/pins/{id}", webPinGet).Methods("GET")
	router.HandleFunc("/pins/{id}", webPinDestroy).Methods("DELETE")
	router.HandleFunc("/status", webStatus).Methods("GET")
	router.NotFoundHandler = http.HandlerFunc(webNotFound)
	return func(res http.ResponseWriter, req *http.Request) {
		router.ServeHTTP(res, req)
	}
}

type webStatusingResponseWriter struct {
	status int
	http.ResponseWriter
}

func (w *webStatusingResponseWriter) WriteHeader(s int) {
	w.status = s
	w.ResponseWriter.WriteHeader(s)
}

func webWrapLogging(f http.HandlerFunc) http.HandlerFunc {
	return func(res http.ResponseWriter, req *http.Request) {
		start := time.Now()
		method := req.Method
		path := req.URL.Path
		log("web.request.start", "method=%s path=%s", method, path)
		wres := webStatusingResponseWriter{-1, res}
		f(&wres, req)
		elapsed := float64(time.Since(start)) / 1000000.0
		log("web.request.finish", "method=%s path=%s status=%d elapsed=%f", method, path, wres.status, elapsed)
	}
}

func webTrap() {
	log("web.trap.set")
	trap := make(chan os.Signal)
	go func() {
		<- trap
		log("web.exit")
		os.Exit(0)
	}()
	signal.Notify(trap, syscall.SIGINT, syscall.SIGTERM)
}

func webStart() {
	log("web.start")
	dataStart()
	handler := webRouterHandler()
{'fraction_non_alphanumeric': 0.16806605093758747, 'fraction_numerical': 0.009515813042261404, 'mean_word_length': 4.708466453674121, 'pattern_counts': {'":': 6, '<': 5, '<?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': '1254108', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2642, 'n_words': 559}