<commit_before>package server

import (
	"github.com/smancke/guble/protocol"

	"github.com/julienschmidt/httprouter"
	"github.com/rs/xid"

	"bytes"
	"io/ioutil"
	"net/http"
	"strings"
)

const X_HEADER_PREFIX = "x-guble-"

type RestMessageApi struct {
	Router
	mux    http.Handler
	prefix string
}

func NewRestMessageApi(router Router, prefix string) *RestMessageApi {
	mux := httprouter.New()
	api := &RestMessageApi{router, mux, prefix}

	p := removeTrailingSlash(prefix)
	mux.POST(p+"/message/*topic", api.PostMessage)

	return api
}

func (api *RestMessageApi) GetPrefix() string {
	return api.prefix
}

func (api *RestMessageApi) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	api.mux.ServeHTTP(w, r)
}

func (api *RestMessageApi) PostMessage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, `Can not read body`, http.StatusBadRequest)
		return
	}

	msg := &protocol.Message{
		Path:          protocol.Path(params.ByName(`topic`)),
		Body:          body,
		UserID:        q(r, `userId`),
		ApplicationID: xid.New().String(),
		MessageID:     q(r, `messageId`),
		HeaderJSON:    headersToJson(r.Header),
	}

	api.HandleMessage(msg)
}

// returns a query parameter
func q(r *http.Request, name string) string {
	params := r.URL.Query()[name]
	if len(params) > 0 {
		return params[0]
	}
	return ""
}

func headersToJson(header http.Header) string {
	buff := &bytes.Buffer{}
	buff.WriteString("{")

	count := 0
	for key, valueList := range header {
		if strings.HasPrefix(strings.ToLower(key), X_HEADER_PREFIX) && len(valueList) > 0 {<commit_msg>adding health-check in REST-API<commit_after>package server

import (
	"github.com/smancke/guble/protocol"

	"github.com/julienschmidt/httprouter"
	"github.com/rs/xid"

	"bytes"
	"io/ioutil"
	"net/http"
	"strings"
)

const X_HEADER_PREFIX = "x-guble-"

type RestMessageApi struct {
	Router
	mux    http.Handler
	prefix string
}

func NewRestMessageApi(router Router, prefix string) *RestMessageApi {
	mux := httprouter.New()
	api := &RestMessageApi{router, mux, prefix}

	p := removeTrailingSlash(prefix)
	mux.POST(p+"/message/*topic", api.PostMessage)

	return api
}

func (api *RestMessageApi) GetPrefix() string {
	return api.prefix
}

func (api *RestMessageApi) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	api.mux.ServeHTTP(w, r)
}

func (api *RestMessageApi) PostMessage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, `Can not read body`, http.StatusBadRequest)
		return
	}

	msg := &protocol.Message{
		Path:          protocol.Path(params.ByName(`topic`)),
		Body:          body,
		UserID:        q(r, `userId`),
		ApplicationID: xid.New().String(),
		MessageID:     q(r, `messageId`),
		HeaderJSON:    headersToJson(r.Header),
	}

	api.HandleMessage(msg)
}

func (api *RestMessageApi) Check() error {
	return nil
}

// returns a query parameter
func q(r *http.Request, name string) string {
	params := r.URL.Query()[name]
	if len(params) > 0 {
		return params[0]
	}
	return ""
}

func headersToJson(header http.Header) string {
	buff := &bytes.Buffer{}
	buff.WriteString("{")

	count := 0
	for key, valueList := range header {
		if strings.HasPrefix(strings.ToLower(key), X_HEADER_PREFIX) && len(valueList) > 0 {
{'fraction_non_alphanumeric': 0.14366448542534205, 'fraction_numerical': 0.002379535990481856, 'mean_word_length': 4.134351145038168, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2330359', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1219, 'n_words': 272}