<commit_before>package handlers

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"net/http"

	"github.com/arschles/gbs/log"
	docker "github.com/fsouza/go-dockerclient"
	"github.com/gorilla/mux"
)

const (
	defaultBuildImg = "quay.io/arschles/gbs-env:0.0.1"
)

func BuildURL() string {
	return fmt.Sprintf("/{%s}/{%s}/{%s}", site, org, repo)
}

func Build(workdir string, dockerCl *docker.Client) http.Handler {
	type req struct {
		BuildEnv   string `json:"build_env"`
		CGOEnabled *bool  `json:"cgo_enabled"`
	}

	type resp struct {
		StatusURL string `json:"status_url"`
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		flusher, ok := w.(http.Flusher)
		if !ok {
			httpErrf(w, http.StatusInternalServerError, "server doesn't support flushing output")
			return
		}

		site, ok := mux.Vars(r)[site]
		if !ok {
			httpErrf(w, http.StatusBadRequest, "missing site in path")
			return
		}

		org, ok := mux.Vars(r)[org]
		if !ok {
			httpErrf(w, http.StatusBadRequest, "missing org in path")
			return
		}

		repo, ok := mux.Vars(r)[repo]
		if !ok {
			httpErrf(w, http.StatusBadRequest, "missing repo in path")
			return
		}

		buildImg := defaultBuildImg
		req := new(req)
		if err := json.NewDecoder(r.Body).Decode(req); err == nil {
			if req.BuildEnv != "" {
				buildImg = req.BuildEnv
			}
		}
		defer r.Body.Close()

		env := []string{}
		if req.CGOEnabled == nil || !*req.CGOEnabled {
			env = append(env, "CGO_ENABLED=0")
		} else {
			env = append(env, "CGO_ENABLED=1")
		}
		containerOpts := createContainerOpts(buildImg, workdir, site, org, repo, env...)
		container, err := dockerCl.CreateContainer(containerOpts)
		if err != nil {
			log.Errf("creating container [%s]", err)
			httpErrf(w, http.StatusInternalServerError, "error creating container [%s]", err)
			return
		}

		hostConfig := &docker.HostConfig{Binds: []string{fmt.Sprintf("%s:%s", workdir, absPwd)}}
		if err := dockerCl.StartContainer(container.ID, hostConfig); err != nil {
			log.Errf("starting container [%s]", err)
			httpErrf(w, http.StatusInternalServerError, "error starting container [%s]", err)
			return<commit_msg>consolidating the input config directives<commit_after>package handlers

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"net/http"

	"github.com/arschles/gbs/log"
	docker "github.com/fsouza/go-dockerclient"
	"github.com/gorilla/mux"
)

const (
	defaultBuildImg = "quay.io/arschles/gbs-env:0.0.1"
)

func BuildURL() string {
	return fmt.Sprintf("/{%s}/{%s}/{%s}", site, org, repo)
}

func Build(workdir string, dockerCl *docker.Client) http.Handler {
	type req struct {
		BuildImage *string `json:"build_image"`
		CGOEnabled *bool   `json:"cgo_enabled"`
	}

	type resp struct {
		StatusURL string `json:"status_url"`
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		flusher, ok := w.(http.Flusher)
		if !ok {
			httpErrf(w, http.StatusInternalServerError, "server doesn't support flushing output")
			return
		}

		site, ok := mux.Vars(r)[site]
		if !ok {
			httpErrf(w, http.StatusBadRequest, "missing site in path")
			return
		}

		org, ok := mux.Vars(r)[org]
		if !ok {
			httpErrf(w, http.StatusBadRequest, "missing org in path")
			return
		}

		repo, ok := mux.Vars(r)[repo]
		if !ok {
			httpErrf(w, http.StatusBadRequest, "missing repo in path")
			return
		}

		buildImg := defaultBuildImg
		var env []string
		req := new(req)
		if err := json.NewDecoder(r.Body).Decode(req); err == nil {
			if req.BuildImage != nil {
				buildImg = *req.BuildImage
			}
			if req.CGOEnabled == nil || !*req.CGOEnabled {
				env = append(env, "CGO_ENABLED=0")
			} else {
				env = append(env, "CGO_ENABLED=1")
			}
		}
		defer r.Body.Close()

		containerOpts := createContainerOpts(buildImg, workdir, site, org, repo, env...)
		container, err := dockerCl.CreateContainer(containerOpts)
		if err != nil {
			log.Errf("creating container [%s]", err)
			httpErrf(w, http.StatusInternalServerError, "error creating container [%s]", err)
			return
		}

		hostConfig := &docker.HostConfig{Binds: []string{fmt.Sprintf("%s:%s", workdir, absPwd)}}
		if err := dockerCl.StartContainer(container.ID, hostConfig); err != nil {
			log.Errf("starting container [%s]", err)
			httpErrf(w, http.StatusInternalServerError, "error starting container [%s]", err)
			return
{'fraction_non_alphanumeric': 0.15530829856281872, 'fraction_numerical': 0.0023180343069077423, 'mean_word_length': 3.92018244013683, 'pattern_counts': {'":': 0, '<': 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': '2049000', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1582, 'n_words': 353}