web

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2023 License: MPL-2.0 Imports: 31 Imported by: 2

README

web

项目简介

针对framework web 框架统一从配置中心加载配置文件,并开启防跨站、跨域请求以及自适应限流中间件。

配置说明
type Config struct {
    CsrfDomain   []string         `json:"csrf"`       // 用于防跨站请求
    AllowPattern []string         `json:"csrf_allow"` // 用于防跨站白名单
    Cors         []string         `json:"cors"`       // 用于跨域请求支持域名集
    ServerConfig mvc.ServerConfig `json:"server"`     // 服务器配置
    LimitConfig  ratelimit.Config `json:"limit"`      // 自适应限流
}
配置中心设置
create /system/base/server/9999 {"server":{"network":"tcp","address":":8080","timeout":"2s","read_timeout":1,"write_timeout":0},"limit":{"Enabled":false,"Window":0,"WinBucket":0,"Rule":"","Debug":false,"CPUThreshold":0}}

提醒9999是指具体应用的systemId

Documentation

Index

Constants

View Source
const DefaultTplSetName = "DEFAULT"

Variables

View Source
var (
	// DefaultLoggerConfig is the default Logger middleware config.
	DefaultLoggerConfig = LoggerConfig{
		Skipper: middleware.DefaultSkipper,
		Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}",` +
			`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` +
			`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
			`,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n",
		CustomTimeFormat: "2006-01-02 15:04:05.00000",
		// contains filtered or unexported fields
	}
)
View Source
var SwagHandler echo.HandlerFunc
View Source
var (
	TemplateEnv = "development"
)

Functions

func App

func App(wa WebApp, systemId string, conf configuration.Configuration)

func LoggerWithConfig

func LoggerWithConfig(systemId string, enableLog bool, config LoggerConfig) echo.MiddlewareFunc

LoggerWithConfig returns a Logger middleware with config. See: `Logger()`.

func OptApp

func OptApp(wa WebApp, systemId string, conf configuration.Configuration) *web

func PrepareCharset

func PrepareCharset(charset string) string

func Trace

func Trace() echo.MiddlewareFunc

Trace 是跟踪中间件

Types

type Config

type Config struct {
	Addr      string      `json:"addr"`
	Gzip      int         `json:"gzip"`      // gzip压缩等级
	EnableLog bool        `json:"enableLog"` // 是否打开日记
	Tag       []trace.Tag `json:"tag"`
}

type Delims

type Delims struct {
	Left  string // 左定界符,默认为{{
	Right string // 右定界符,默认为}}
}

代表用于HTML模板渲染的一组左右定界符

type HTMLOptions

type HTMLOptions struct {
	Layout string // 布局模板名称.覆盖Options.Layout.
}

HTMLOptions是用于覆盖特定HTML调用的某些呈现选项的结构

type LoggerConfig

type LoggerConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// Tags to construct the logger format.
	//
	// - time_unix
	// - time_unix_nano
	// - time_rfc3339
	// - time_rfc3339_nano
	// - time_custom
	// - id (Request ID)
	// - remote_ip
	// - uri
	// - host
	// - method
	// - path
	// - protocol
	// - referer
	// - user_agent
	// - status
	// - error
	// - latency (In nanoseconds)
	// - latency_human (Human readable)
	// - bytes_in (Bytes received)
	// - bytes_out (Bytes sent)
	// - header:<NAME>
	// - query:<NAME>
	// - form:<NAME>
	//
	// Example "${remote_ip} ${status}"
	//
	// Optional. Default value DefaultLoggerConfig.Format.
	Format string `yaml:"format"`

	// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
	CustomTimeFormat string `yaml:"custom_time_format"`

	// Output is a writer where logs in JSON format are written.
	// Optional. Default value os.Stdout.
	Output io.Writer
	// contains filtered or unexported fields
}

LoggerConfig defines the config for Logger middleware.

type RenderOptions

type RenderOptions struct {
	Directory          string             // 加载模板目标.默认为"templates".
	AppendDirectories  []string           // 附加目录会覆盖默认模板.
	Layout             string             // 布局模板名称. 如果为""代表不会渲染布局.默认是"".
	Extensions         []string           // 用于从中解析模板文件的扩展. 默认值为[".tmpl", ".html"].
	Funcs              []template.FuncMap // Funcs是FuncMap的一部分,可在编译时应用于模板. 这对于助手功能很有用. 默认是[].
	Delims             Delims             // 将定界符设置为Delims结构中的指定字符串.
	Charset            string             // 将给定的字符集附加到Content-Type标头.默认是"UTF-8".
	HTMLContentType    string             // 允许将输出更改为XHTML而不是HTML.默认是"text/html".
	TemplateFileSystem                    // TemplateFileSystem是用于支持任何模板文件系统实现的接口.
}

RenderOptions 表示用于指定Render中间件的配置选项的结构。.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 0 if the response has
	// not been written
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(ResponseWriter))
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a response writer if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type TemplateFile

type TemplateFile interface {
	Name() string
	Data() []byte
	Ext() string
}

表示具有名称且可以读取的模板文件的接口.

type TemplateFileSystem

type TemplateFileSystem interface {
	ListFiles() []TemplateFile
	Get(string) (io.Reader, error)
}

表示能够列出所有文件的模板文件系统的接口.

type TemplateSet

type TemplateSet struct {
	// contains filtered or unexported fields
}

表示类型为*template.Template的模板集。

func NewTemplateSet

func NewTemplateSet() *TemplateSet

初始化一个新的空模板集.

func (*TemplateSet) Get

func (ts *TemplateSet) Get(name string) *template.Template

func (*TemplateSet) GetDir

func (ts *TemplateSet) GetDir(name string) string

func (*TemplateSet) Set

func (ts *TemplateSet) Set(name string, opt *RenderOptions) *template.Template

type TplFile

type TplFile struct {
	// contains filtered or unexported fields
}

实现TemplateFile接口.

func NewTplFile

func NewTplFile(name string, data []byte, ext string) *TplFile

创建具有给定名称和数据的新模板文件.

func (*TplFile) Data

func (f *TplFile) Data() []byte

func (*TplFile) Ext

func (f *TplFile) Ext() string

func (*TplFile) Name

func (f *TplFile) Name() string

type TplFileSystem

type TplFileSystem struct {
	// contains filtered or unexported fields
}

实现TemplateFileSystem接口.

func NewTemplateFileSystem

func NewTemplateFileSystem(opt RenderOptions, omitData bool) TplFileSystem

使用给定的选项创建新的模板文件系统.

func (TplFileSystem) Get

func (fs TplFileSystem) Get(name string) (io.Reader, error)

func (TplFileSystem) ListFiles

func (fs TplFileSystem) ListFiles() []TemplateFile

type TplRender

type TplRender struct {
	http.ResponseWriter
	*TemplateSet
	Opt     *RenderOptions
	Charset string
	// contains filtered or unexported fields
}

func (*TplRender) HTML

func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions)

func (*TplRender) HTMLSet

func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions)

func (*TplRender) HTMLSetBytes

func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)

type WebApp

type WebApp func(eng *echo.Echo)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL