<commit_before>package terms

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"github.com/jinzhu/gorm"
	dbModule "github.com/notegio/openrelay/db"
	"github.com/notegio/openrelay/types"
	"net/http"
	"time"
	"io"
	"log"
	"regexp"
	"strconv"
	"strings"
)

type TermsFormat struct {
	Text    string         `json:"text"`
	ID      uint           `json:"id"`
	Mask    *types.Uint256 `json:"mask"`
	MaskID  uint           `json:"maskId"`
}

type TermsSigPayload struct {
	TermsID   uint              `json:"terms_id"`
	MaskID    uint              `json:"mask_id"`
	Signature *types.Signature  `json:"sig"`
	Address   *types.Address    `json:"address"`
	Timestamp string            `json:"timestamp"`
	Nonce     string            `json:"nonce"`
}

type IngestError struct {
	Code             int               `json:"code"`
	Reason           string            `json:"reason"`
}


func returnError(w http.ResponseWriter, errResp IngestError, status int) {
	w.WriteHeader(status)
	w.Header().Set("Content-Type", "application/json")
	errBytes, err := json.Marshal(errResp)
	if err != nil {
		log.Printf(err.Error())
	}
	w.Write(errBytes)
}

func TermsHandler(db *gorm.DB) func(http.ResponseWriter, *http.Request) {
	tm := dbModule.NewTermsManager(db)
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			queryObject := r.URL.Query()
			lang := queryObject.Get("lang")
			if lang == "" {
				lang = "en"
			}
			terms, err := tm.GetTerms(lang)
			if err != nil {
				returnError(w, IngestError{101, err.Error()}, 404)
				return
			}
			mask, mask_id, err := tm.GetNewHashMask(terms)
			if err != nil {
				returnError(w, IngestError{101, err.Error()}, 500)
				return
			}<commit_msg>Add cache control to errors on TOS<commit_after>package terms

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"github.com/jinzhu/gorm"
	dbModule "github.com/notegio/openrelay/db"
	"github.com/notegio/openrelay/types"
	"net/http"
	"time"
	"io"
	"log"
	"regexp"
	"strconv"
	"strings"
)

type TermsFormat struct {
	Text    string         `json:"text"`
	ID      uint           `json:"id"`
	Mask    *types.Uint256 `json:"mask"`
	MaskID  uint           `json:"maskId"`
}

type TermsSigPayload struct {
	TermsID   uint              `json:"terms_id"`
	MaskID    uint              `json:"mask_id"`
	Signature *types.Signature  `json:"sig"`
	Address   *types.Address    `json:"address"`
	Timestamp string            `json:"timestamp"`
	Nonce     string            `json:"nonce"`
}

type IngestError struct {
	Code             int               `json:"code"`
	Reason           string            `json:"reason"`
}


func returnError(w http.ResponseWriter, errResp IngestError, status int) {
	w.WriteHeader(status)
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
	errBytes, err := json.Marshal(errResp)
	if err != nil {
		log.Printf(err.Error())
	}
	w.Write(errBytes)
}

func TermsHandler(db *gorm.DB) func(http.ResponseWriter, *http.Request) {
	tm := dbModule.NewTermsManager(db)
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			queryObject := r.URL.Query()
			lang := queryObject.Get("lang")
			if lang == "" {
				lang = "en"
			}
			terms, err := tm.GetTerms(lang)
			if err != nil {
				returnError(w, IngestError{101, err.Error()}, 404)
				return
			}
			mask, mask_id, err := tm.GetNewHashMask(terms)
			if err != nil {
				returnError(w, IngestError{101, err.Error()}, 500)
				return
			}
{'fraction_non_alphanumeric': 0.14815872109620326, 'fraction_numerical': 0.008564087924636025, 'mean_word_length': 2.8675496688741724, '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': '5781767', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1244, 'n_words': 262}