<commit_before>package app

import (
	"fmt"
	"html/template"
	"time"

	"github.com/patrickmn/go-cache"
	"github.com/raggaer/castro/app/database"
	"github.com/raggaer/castro/app/lua"
	"github.com/raggaer/castro/app/util"
	"github.com/raggaer/otmap"
	"log"
	"net/url"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
)

// Start the main execution point for Castro
func Start() {
	// Wait for all goroutines to make their work
	wait := &sync.WaitGroup{}

	// Wait for all tasks
	wait.Add(7)

	// Load application logger
	loadAppLogger()

	// Load application config
	loadAppConfig()

	// Run logger renew service
	go util.RenewLogger()

	// Execute our tasks
	go func(wait *sync.WaitGroup) {

		loadLUAConfig()
		connectDatabase()
		loadMap()
		go loadHouses(wait)
		go loadVocations(wait)
	}(wait)

	// Create application cache
	createCache()

	go loadWidgetList(wait)
	go appTemplates(wait)
	go widgetTemplates(wait)
	go loadSubtopics(wait)
	go loadWidgets(wait)

	// Wait for the tasks
	wait.Wait()
}

func loadWidgets(wg *sync.WaitGroup) {
	// Load subtopic list
	if err := lua.WidgetList.Load("widgets"); err != nil {
		util.Logger.Fatalf("Cannot load application widget list: %v", err)
	}

	// Tell the wait group we are done
	wg.Done()
}

func loadSubtopics(wg *sync.WaitGroup) {
	// Load subtopic list
	if err := lua.PageList.Load("pages"); err != nil {
		util.Logger.Fatalf("Cannot load application subtopic list: %v", err)
	}

	// Tell the wait group we are done
	wg.Done()
}

func loadAppLogger() {
	// Create logger file
	f, day, err := util.CreateLogFile()
<commit_msg>Add init lua file execution<commit_after>package app

import (
	"fmt"
	glua "github.com/yuin/gopher-lua"
	"html/template"
	"time"

	"github.com/patrickmn/go-cache"
	"github.com/raggaer/castro/app/database"
	"github.com/raggaer/castro/app/lua"
	"github.com/raggaer/castro/app/util"
	"github.com/raggaer/otmap"
	"log"
	"net/url"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
)

// Start the main execution point for Castro
func Start() {
	// Wait for all goroutines to make their work
	wait := &sync.WaitGroup{}

	// Wait for all tasks
	wait.Add(7)

	// Load application logger
	loadAppLogger()

	// Load application config
	loadAppConfig()

	// Run logger renew service
	go util.RenewLogger()

	// Execute our tasks
	go func(wait *sync.WaitGroup) {

		loadLUAConfig()
		connectDatabase()
		loadMap()
		go loadHouses(wait)
		go loadVocations(wait)
	}(wait)

	// Create application cache
	createCache()

	go loadWidgetList(wait)
	go appTemplates(wait)
	go widgetTemplates(wait)
	go loadSubtopics(wait)
	go loadWidgets(wait)

	// Wait for the tasks
	wait.Wait()

	// Execute the init lua file
	executeInitFile()
}

func executeInitFile() {
	// Get lua state
	luaState := glua.NewState()

	// Create storage metatable
	lua.SetStorageMetaTable(luaState)

	// Create time metatable
	lua.SetTimeMetaTable(luaState)

	// Create url metatable
	lua.SetURLMetaTable(luaState)

	// Create debug metatable
	lua.SetDebugMetaTable(luaState)

	// Create XML metatable
	lua.SetXMLMetaTable(luaState)

	// Create captcha metatable
	lua.SetCaptchaMetaTable(luaState)

	// Create crypto metatable
	lua.SetCryptoMetaTable(luaState)

	// Create validator metatable
	lua.SetValidatorMetaTable(luaState)

	// Create database metatable
	lua.SetDatabaseMetaTable(luaState)

	// Create config metatable
	lua.SetConfigMetaTable(luaState)

	// Create map metatable
	lua.SetMapMetaTable(luaState)

	// Create mail metatable
	lua.SetMailMetaTable(luaState)

	// Create cache metatable
	lua.SetCacheMetaTable(luaState)

	// Create reflect metatable
	lua.SetReflectMetaTable(luaState)

	// Create json metatable
	lua.SetJSONMetaTable(luaState)

	// Execute init file
	if err := luaState.DoFile(filepath.Join("engine", "init.lua")); err != nil {
		util.Logger.Fatalf("Cannot execute init lua file: %v", err)
	}
}

func loadWidgets(wg *sync.WaitGroup) {
	// Load subtopic list
	if err := lua.WidgetList.Load("widgets"); err != nil {
		util.Logger.Fatalf("Cannot load application widget list: %v", err)
	}

	// Tell the wait group we are done
	wg.Done()
}

func loadSubtopics(wg *sync.WaitGroup) {
	// Load subtopic list
	if err := lua.PageList.Load("pages"); err != nil {
		util.Logger.Fatalf("Cannot load application subtopic list: %v", err)
	}

	// Tell the wait group we are done
	wg.Done()
}

func loadAppLogger() {
	// Create logger file
	f, day, err := util.CreateLogFile()

{'fraction_non_alphanumeric': 0.12389780691838119, 'fraction_numerical': 0.0004521817770743839, 'mean_word_length': 3.883002207505519, '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': '4454735', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1548, 'n_words': 393}