<commit_before>// Copyright 2016 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package thinkgo

import (
	"fmt"
	"net/http"
	"os"
	"strings"

	"github.com/henrylee2cn/apiware"
	"github.com/henrylee2cn/thinkgo/acceptencoder"
	"github.com/henrylee2cn/thinkgo/logging"
	"github.com/henrylee2cn/thinkgo/utils"
)

// GlobalSetting defines the global configuration and functions...
type GlobalSetting struct {
	config GlobalConfig
	// Error replies to the request with the specified error message and HTTP code.
	// It does not otherwise end the request; the caller should ensure no further
	// writes are done to response.
	// The error message should be plain text.
	errorFunc ErrorFunc
	// The following is only for the APIHandler
	bindErrorFunc BindErrorFunc
	// When the APIHander's parameter name (struct tag) is unsetted,
	// it is mapped from the structure field name by default.
	// If `paramMapping` is nil, use snake style.
	// If the APIHander's parameter binding fails, the default handler is invoked
	paramMapping apiware.ParamNameFunc
	// global file cache system manager
	fsManager    *FileServerManager
	pongo2Render *Pongo2Render
	// The path for the upload files
	uploadDir string
	// The path for the static files
	staticDir string
	// The path for the log files
	logDir string

	syslog *logging.Logger
	bizlog *logging.Logger
}

// global configuration and functions...
var Global = func() *GlobalSetting {
	global := &GlobalSetting{
		config:        globalConfig,
		errorFunc:     defaultErrorFunc,
		bindErrorFunc: defaultBindErrorFunc,
		paramMapping:  defaultParamMapping,
		fsManager: newFileServerManager(
			globalConfig.Cache.SizeMB*1024*1024,
			globalConfig.Cache.Expire,
			globalConfig.Cache.Enable,
			globalConfig.Gzip.Enable,
		),
		uploadDir: defaultUploadDir,
		staticDir: defaultStaticDir,
		logDir:    defaultLogDir,
	}
	if globalConfig.Cache.Enable {
		global.pongo2Render = newPongo2Render(global.fsManager.OpenFile)
	} else {
		global.pongo2Render = newPongo2Render(nil)
	}
	global.initLogger()
	return global
}()

var (
	defaultErrorFunc = func(ctx *Context, errStr string, status int) {
		statusText := http.StatusText(status)
		if len(errStr) > 0 {
			errStr = `<br><p><b style="color:red;">[ERROR]</b> <pre>` + errStr + `</pre></p>`
		}<commit_msg>`defaultErrorFunc` add an error message to print<commit_after>// Copyright 2016 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package thinkgo

import (
	"fmt"
	"net/http"
	"os"
	"strings"

	"github.com/henrylee2cn/apiware"
	"github.com/henrylee2cn/thinkgo/acceptencoder"
	"github.com/henrylee2cn/thinkgo/logging"
	"github.com/henrylee2cn/thinkgo/utils"
)

// GlobalSetting defines the global configuration and functions...
type GlobalSetting struct {
	config GlobalConfig
	// Error replies to the request with the specified error message and HTTP code.
	// It does not otherwise end the request; the caller should ensure no further
	// writes are done to response.
	// The error message should be plain text.
	errorFunc ErrorFunc
	// The following is only for the APIHandler
	bindErrorFunc BindErrorFunc
	// When the APIHander's parameter name (struct tag) is unsetted,
	// it is mapped from the structure field name by default.
	// If `paramMapping` is nil, use snake style.
	// If the APIHander's parameter binding fails, the default handler is invoked
	paramMapping apiware.ParamNameFunc
	// global file cache system manager
	fsManager    *FileServerManager
	pongo2Render *Pongo2Render
	// The path for the upload files
	uploadDir string
	// The path for the static files
	staticDir string
	// The path for the log files
	logDir string

	syslog *logging.Logger
	bizlog *logging.Logger
}

// global configuration and functions...
var Global = func() *GlobalSetting {
	global := &GlobalSetting{
		config:        globalConfig,
		errorFunc:     defaultErrorFunc,
		bindErrorFunc: defaultBindErrorFunc,
		paramMapping:  defaultParamMapping,
		fsManager: newFileServerManager(
			globalConfig.Cache.SizeMB*1024*1024,
			globalConfig.Cache.Expire,
			globalConfig.Cache.Enable,
			globalConfig.Gzip.Enable,
		),
		uploadDir: defaultUploadDir,
		staticDir: defaultStaticDir,
		logDir:    defaultLogDir,
	}
	if globalConfig.Cache.Enable {
		global.pongo2Render = newPongo2Render(global.fsManager.OpenFile)
	} else {
		global.pongo2Render = newPongo2Render(nil)
	}
	global.initLogger()
	return global
}()

var (
	defaultErrorFunc = func(ctx *Context, errStr string, status int) {
		ctx.Log().Error(errStr)
		statusText := http.StatusText(status)
		if len(errStr) > 0 {
			errStr = `<br><p><b style="color:red;">[ERROR]</b> <pre>` + errStr + `</pre></p>`
		}
{'fraction_non_alphanumeric': 0.09286088322569384, 'fraction_numerical': 0.009425728748472683, 'mean_word_length': 3.922680412371134, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2576950', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1683, 'n_words': 591}