<commit_before>package api

import (
	"api/router"
	"interfaces"
	"io"
	"log"
	"time"

	"net/http"

	"bytes"

	"github.com/labstack/echo"
	lua "github.com/yuin/gopher-lua"
)

var contextMethods = map[string]lua.LGFunction{
	// "URI":        contextGetURI,
	"QueryParam": contextGetQueryParam,
	"NoContext":  contextNoContext,
	"Redirect":   contextRedirect,
	"JSON":       contextRenderJSON,
	"IsGET":      contextMethodIsGET,
	"IsPOST":     contextMethodIsPOST,
	"Set":        contextMethodSet,
	"Get":        contextMethodGet,
	"FormValue":  contextMethodFormValue,
	"FormFile":   contextMethodFormFile,
	// alias IsCurrentRoute
	"Route": contextRoute,
	// "Get":        contextMethodGet,

	"AppExport": contextAppExport,
	"AppImport": contextAppImport,
}

func contextGetPath(L *lua.LState) int {
	p := checkContext(L)
	L.Push(lua.LString(p.echoCtx.Path()))
	return 1
}

// func contextGetURI(L *lua.LState) int {
// 	p := checkContext(L)
// 	L.Push(lua.LString(p.echoCtx.Request().URI()))
// 	return 1
// }

func contextRoute(L *lua.LState) int {
	c := checkContext(L)
	route := router.MatchVRouteFromContext(c.echoCtx)

	if route == nil {
		// TODO: informing that an empty route, should not happen

		return 0
	}

	if L.GetTop() >= 2 {
		route = &interfaces.RouteMatch{
			Route: nil,
			Vars:  make(map[string]string),
		}

		foundRoute := vrouter.Get(L.CheckString(2))

		if foundRoute != nil {
			route.Route = foundRoute
			route.Handler = foundRoute.Options()
		}
	}

	// Push route
	newLuaRoute(route)(L)

	return 1
}

// Getter and setter for the Context#Queryparam
func contextGetQueryParam(L *lua.LState) int {
	p := checkContext(L)
	var value string
	if L.GetTop() == 2 {
		value = p.echoCtx.QueryParam(L.CheckString(2))
	}
	L.Push(lua.LString(value))
	return 1
}

func contextNoContext(L *lua.LState) int {
	p := checkContext(L)

	p.Err = p.echoCtx.NoContent(L.CheckInt(2))
	p.Rendered = true

	return 0
}

func contextRedirect(L *lua.LState) int {
	p := checkContext(L)

	p.Err = p.echoCtx.Redirect(http.StatusFound, L.CheckString(2))
	p.Rendered = true

	return 0
}

func contextRenderJSON(L *lua.LState) int {
	p := checkContext(L)
	status := L.CheckInt(2)
	table := L.CheckTable(3)

	jsonMap := make(map[string]interface{}, table.Len())

	table.ForEach(func(key, value lua.LValue) {
		var _key string
		var _value interface{}

		_key = key.String()

		switch value.Type() {
		case lua.LTNumber:
			_value = float64(value.(lua.LNumber))
		case lua.LTNil:
			_value = nil
		case lua.LTBool:
			_value = bool(value.(lua.LBool))
		case lua.LTString:
			_value = string(value.(lua.LString))
		case lua.LTUserData:
			_value = value.(*lua.LUserData).Value
		default:
			log.Printf(
				"[ERR] not expected type value, got %q, for field %q",
				value.Type(),
				_key,
			)
		}

		jsonMap[_key] = _value
	})

	p.Err = p.echoCtx.JSON(status, jsonMap)
	p.Rendered = true

	return 0
}

func contextResponseStatus(L *lua.LState) int {
	p := checkContext(L)
	status := L.CheckInt(2)
	p.ResponseStatus = status

	return 0
}

func contextMethodIsGET(L *lua.LState) int {
	p := checkContext(L)
	L.Push(lua.LBool(p.echoCtx.Request().Method() == echo.GET))
	return 1
}

func contextMethodIsPOST(L *lua.LState) int {
	p := checkContext(L)
	L.Push(lua.LBool(p.echoCtx.Request().Method() == echo.POST))
	return 1
}
<commit_msg>добавлен метод для получения контента файла (без кеша)<commit_after>package api

import (
	"api/router"
	"interfaces"
	"io"
	"log"
	"time"

	"net/http"

	"bytes"

	"github.com/labstack/echo"
	lua "github.com/yuin/gopher-lua"
)

var contextMethods = map[string]lua.LGFunction{
	// "URI":        contextGetURI,
	"QueryParam": contextGetQueryParam,
	"NoContext":  contextNoContext,
	"Redirect":   contextRedirect,
	"JSON":       contextRenderJSON,
	// FileContent
	"FileContent": contextMethodFileContnet,
	"IsGET":       contextMethodIsGET,
	"IsPOST":      contextMethodIsPOST,
	"Set":         contextMethodSet,
	"Get":         contextMethodGet,
	"FormValue":   contextMethodFormValue,
	"FormFile":    contextMethodFormFile,
	// alias IsCurrentRoute
	"Route": contextRoute,
	// "Get":        contextMethodGet,

	"AppExport": contextAppExport,
	"AppImport": contextAppImport,
}

func contextGetPath(L *lua.LState) int {
	p := checkContext(L)
	L.Push(lua.LString(p.echoCtx.Path()))
	return 1
}

// func contextGetURI(L *lua.LState) int {
// 	p := checkContext(L)
// 	L.Push(lua.LString(p.echoCtx.Request().URI()))
// 	return 1
// }

func contextRoute(L *lua.LState) int {
	c := checkContext(L)
	route := router.MatchVRouteFromContext(c.echoCtx)

	if route == nil {
		// TODO: informing that an empty route, should not happen

		return 0
	}

	if L.GetTop() >= 2 {
		route = &interfaces.RouteMatch{
			Route: nil,
			Vars:  make(map[string]string),
		}

		foundRoute := vrouter.Get(L.CheckString(2))

		if foundRoute != nil {
			route.Route = foundRoute
			route.Handler = foundRoute.Options()
		}
	}

	// Push route
	newLuaRoute(route)(L)

	return 1
}

// Getter and setter for the Context#Queryparam
func contextGetQueryParam(L *lua.LState) int {
	p := checkContext(L)
	var value string
	if L.GetTop() == 2 {
		value = p.echoCtx.QueryParam(L.CheckString(2))
	}
	L.Push(lua.LString(value))
	return 1
}

func contextNoContext(L *lua.LState) int {
	p := checkContext(L)

	p.Err = p.echoCtx.NoContent(L.CheckInt(2))
	p.Rendered = true

	return 0
}

func contextRedirect(L *lua.LState) int {
	p := checkContext(L)

	p.Err = p.echoCtx.Redirect(http.StatusFound, L.CheckString(2))
	p.Rendered = true

	return 0
}

func contextRenderJSON(L *lua.LState) int {
	p := checkContext(L)
	status := L.CheckInt(2)
	table := L.CheckTable(3)

	jsonMap := make(map[string]interface{}, table.Len())

	table.ForEach(func(key, value lua.LValue) {
		var _key string
		var _value interface{}

		_key = key.String()

		switch value.Type() {
		case lua.LTNumber:
			_value = float64(value.(lua.LNumber))
		case lua.LTNil:
			_value = nil
		case lua.LTBool:
			_value = bool(value.(lua.LBool))
		case lua.LTString:
			_value = string(value.(lua.LString))
		case lua.LTUserData:
			_value = value.(*lua.LUserData).Value
		default:
			log.Printf(
				"[ERR] not expected type value, got %q, for field %q",
				value.Type(),
				_key,
			)
		}

		jsonMap[_key] = _value
	})

	p.Err = p.echoCtx.JSON(status, jsonMap)
	p.Rendered = true

	return 0
}

func contextMethodFileContnet(L *lua.LState) int {
	c := checkContext(L)
	status := L.CheckInt(2)

	ud := L.CheckUserData(3)
	file, ok := ud.Value.(*luaFile)
	if !ok {
		L.ArgError(2, "file expected")
		return 0
	}

	c.Err = c.echoCtx.Blob(
		status,
		file.ContentType,
		file.RawData,
	)
	c.Rendered = true

	return 0
}

func contextResponseStatus(L *lua.LState) int {
	p := checkContext(L)
	status := L.CheckInt(2)
	p.ResponseStatus = status

	return 0
}

func contextMethodIsGET(L *lua.LState) int {
	p := checkContext(L)
	L.Push(lua.LBool(p.echoCtx.Request().Method() == echo.GET))
	return 1
}

func contextMethodIsPOST(L *lua.LState) int {
	p := checkContext(L)
	L.Push(lua.LBool(p.echoCtx.Request().Method() == echo.POST))
	return 1
}

{'fraction_non_alphanumeric': 0.1476150155235676, 'fraction_numerical': 0.0069150437482359585, 'mean_word_length': 3.668642951251647, 'pattern_counts': {'":': 31, '<': 3, '<?xml version=': 0, '>': 5, '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': '5494758', 'n_tokens_mistral': 2890, 'n_tokens_neox': 2709, 'n_words': 559}