<commit_before>package main

import (
	"log"
	"net/http"
	"os"

	"github.com/alext/tablecloth"
	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"gopkg.in/unrolled/render.v1"

	"github.com/alphagov/publishing-api/request_logger"
)

var (
	arbiterHost      = getEnvDefault("URL_ARBITER", "http://url-arbiter.dev.gov.uk")
	contentStoreHost = getEnvDefault("CONTENT_STORE", "http://content-store.dev.gov.uk")
	port             = getEnvDefault("PORT", "3000")
	requestLogDest   = getEnvDefault("REQUEST_LOG", "STDOUT")

	renderer = render.New(render.Options{})
)

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
	renderer.JSON(w, http.StatusOK, map[string]string{"status": "OK"})
}

func BuildHTTPMux(arbiterURL, contentStoreURL string) http.Handler {
	httpMux := mux.NewRouter()
	httpMux.Methods("GET").Path("/healthcheck").HandlerFunc(HealthCheckHandler)
	contentStoreController := NewContentStoreController(arbiterURL, contentStoreURL)
	httpMux.Methods("PUT").Path("/content{base_path:/.*}").HandlerFunc(contentStoreController.PutContentStoreRequest)
	httpMux.Methods("PUT").Path("/publish-intent{base_path:/.*}").HandlerFunc(contentStoreController.PutContentStoreRequest)
	httpMux.Methods("GET").Path("/publish-intent{base_path:/.*}").HandlerFunc(contentStoreController.GetContentStoreRequest)
	httpMux.Methods("DELETE").Path("/publish-intent{base_path:/.*}").HandlerFunc(contentStoreController.DeleteContentStoreRequest)
	return httpMux
}

func main() {
	httpMux := BuildHTTPMux(arbiterHost, contentStoreHost)

	requestLogger, err := request_logger.New(requestLogDest)
	if err != nil {
		log.Fatal(err)
	}

	middleware := negroni.New()
	middleware.Use(requestLogger)
	middleware.UseHandler(httpMux)

	// Set working dir for tablecloth if available. This is to allow restarts
	// to pick up new versions.  See
	// http://godoc.org/github.com/alext/tablecloth#pkg-variables for details
	if wd := os.Getenv("GOVUK_APP_ROOT"); wd != "" {
		tablecloth.WorkingDir = wd
	}

	err = tablecloth.ListenAndServe(":"+port, middleware)
	if err != nil {
		log.Fatal(err)
	}
}

func getEnvDefault(key string, defaultVal string) string {<commit_msg>Replace HealthCheckHandler with inlined function<commit_after>package main

import (
	"log"
	"net/http"
	"os"

	"github.com/alext/tablecloth"
	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"gopkg.in/unrolled/render.v1"

	"github.com/alphagov/publishing-api/request_logger"
)

var (
	arbiterHost      = getEnvDefault("URL_ARBITER", "http://url-arbiter.dev.gov.uk")
	contentStoreHost = getEnvDefault("CONTENT_STORE", "http://content-store.dev.gov.uk")
	port             = getEnvDefault("PORT", "3000")
	requestLogDest   = getEnvDefault("REQUEST_LOG", "STDOUT")

	renderer = render.New(render.Options{})
)

func BuildHTTPMux(arbiterURL, contentStoreURL string) http.Handler {
	httpMux := mux.NewRouter()

	httpMux.Methods("GET").Path("/healthcheck").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		renderer.JSON(w, http.StatusOK, map[string]string{"status": "OK"})
	})

	contentStoreController := NewContentStoreController(arbiterURL, contentStoreURL)
	httpMux.Methods("PUT").Path("/content{base_path:/.*}").HandlerFunc(contentStoreController.PutContentStoreRequest)
	httpMux.Methods("PUT").Path("/publish-intent{base_path:/.*}").HandlerFunc(contentStoreController.PutContentStoreRequest)
	httpMux.Methods("GET").Path("/publish-intent{base_path:/.*}").HandlerFunc(contentStoreController.GetContentStoreRequest)
	httpMux.Methods("DELETE").Path("/publish-intent{base_path:/.*}").HandlerFunc(contentStoreController.DeleteContentStoreRequest)

	return httpMux
}

func main() {
	httpMux := BuildHTTPMux(arbiterHost, contentStoreHost)

	requestLogger, err := request_logger.New(requestLogDest)
	if err != nil {
		log.Fatal(err)
	}

	middleware := negroni.New()
	middleware.Use(requestLogger)
	middleware.UseHandler(httpMux)

	// Set working dir for tablecloth if available. This is to allow restarts
	// to pick up new versions.  See
	// http://godoc.org/github.com/alext/tablecloth#pkg-variables for details
	if wd := os.Getenv("GOVUK_APP_ROOT"); wd != "" {
		tablecloth.WorkingDir = wd
	}

	err = tablecloth.ListenAndServe(":"+port, middleware)
	if err != nil {
		log.Fatal(err)
	}
}

func getEnvDefault(key string, defaultVal string) string {
{'fraction_non_alphanumeric': 0.14890173410404625, 'fraction_numerical': 0.0023121387283236996, 'mean_word_length': 5.706976744186046, 'pattern_counts': {'":': 4, '<': 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': '1123940', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1459, 'n_words': 218}