<commit_before>// Package api implements renderer HTTP API.
package api

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"github.com/golang/glog"

	"golang.org/x/net/context"

	"bitbucket.org/moovie/renderer/compiler"
	"bitbucket.org/moovie/renderer/components"
)

// Handler - Compiler API handler.
// Retrieves compiler from context using `compiler.FromContext`
func Handler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	c, ok := compiler.FromContext(ctx)
	if !ok {
		glog.Warning("[api] compiler not found in context")
		writeError(w, http.StatusInternalServerError, "compiler not found")
		return
	}
	if r.URL.Path != "/" {
		writeError(w, http.StatusNotFound, http.StatusText(http.StatusNotFound))
		return
	}
	if r.Method != "POST" {
		writeError(w, http.StatusMethodNotAllowed, fmt.Sprintf("method %q is not allowed", r.Method))
		return
	}
	cmp := new(components.Component)
	if err := json.NewDecoder(r.Body).Decode(cmp); err != nil {
		writeError(w, http.StatusBadRequest, err.Error())
		return
	}
	res, err := c.Render(ctx, cmp)
	if err != nil {
		writeError(w, http.StatusInternalServerError, fmt.Sprintf("compile error: %v", err))
		return
	}

	w.WriteHeader(200)

	if strings.Contains(r.Header.Get("Accept"), "application/json") {
		w.Header().Set("Content-Type", "application/json")
		if err := json.NewEncoder(w).Encode(res); err != nil {
			glog.Warningf("[api] response encode error: %v", err)
		}
	} else {
		w.Header().Set("Content-Type", "text/html")
		w.Write([]byte(res.Body))
	}

}

func writeError(w http.ResponseWriter, code int, err string) {
	w.WriteHeader(code)
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(struct {
		Msg  string `json:"error_msg"`
		Code int    `json:"error_code"`
	}{
		Msg:  err,
		Code: code,
	})
}<commit_msg>api: fix header write<commit_after>// Package api implements renderer HTTP API.
package api

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"github.com/golang/glog"

	"golang.org/x/net/context"

	"bitbucket.org/moovie/renderer/compiler"
	"bitbucket.org/moovie/renderer/components"
)

// Handler - Compiler API handler.
// Retrieves compiler from context using `compiler.FromContext`
func Handler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	c, ok := compiler.FromContext(ctx)
	if !ok {
		glog.Warning("[api] compiler not found in context")
		writeError(w, http.StatusInternalServerError, "compiler not found")
		return
	}
	if r.URL.Path != "/" {
		writeError(w, http.StatusNotFound, http.StatusText(http.StatusNotFound))
		return
	}
	if r.Method != "POST" {
		writeError(w, http.StatusMethodNotAllowed, fmt.Sprintf("method %q is not allowed", r.Method))
		return
	}
	cmp := new(components.Component)
	if err := json.NewDecoder(r.Body).Decode(cmp); err != nil {
		writeError(w, http.StatusBadRequest, err.Error())
		return
	}
	res, err := c.Render(ctx, cmp)
	if err != nil {
		writeError(w, http.StatusInternalServerError, fmt.Sprintf("compile error: %v", err))
		return
	}

	if strings.Contains(r.Header.Get("Accept"), "application/json") {
		w.Header().Set("Content-Type", "application/json")
		if err := json.NewEncoder(w).Encode(res); err != nil {
			glog.Warningf("[api] response encode error: %v", err)
		}
	} else {
		w.Header().Set("Content-Type", "text/html")
		w.Write([]byte(res.Body))
	}

}

func writeError(w http.ResponseWriter, code int, err string) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	json.NewEncoder(w).Encode(struct {
		Msg  string `json:"error_msg"`
		Code int    `json:"error_code"`
	}{
		Msg:  err,
		Code: code,
	})
}
{'fraction_non_alphanumeric': 0.16257922292642601, 'fraction_numerical': 0.0008266740148801323, 'mean_word_length': 4.338235294117647, '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': '4244003', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1284, 'n_words': 276}