echocore

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 32 Imported by: 0

README

EchoCore

My current skeleton to simplify running web services based on labstack/echo. Also included are features based on:

Documentation

Index

Constants

View Source
const (
	CtxCore    = "core"
	CtxSession = "session"
)

Variables

This section is empty.

Functions

func CSRFMiddleware

func CSRFMiddleware(cfg *Config) echo.MiddlewareFunc

func ContextMiddleware

func ContextMiddleware(k string, i interface{}) echo.MiddlewareFunc

func GzipMiddleware

func GzipMiddleware(level int) echo.MiddlewareFunc

func Handle

func Handle(h Handler) error

func LastModifiedMiddleware

func LastModifiedMiddleware(t time.Time) echo.MiddlewareFunc

func LoggerMiddleware

func LoggerMiddleware(tformat, lformat string, skip []string) echo.MiddlewareFunc

func NewEcho

func NewEcho(core *Core, pre ...echo.MiddlewareFunc) *echo.Echo

func Run

func Run(core *Core, e *echo.Echo)

func ServerHeaderMiddleware

func ServerHeaderMiddleware(name string) echo.MiddlewareFunc

func SessionMiddleware

func SessionMiddleware(cfg *Config) echo.MiddlewareFunc

func SkipperRouteName

func SkipperRouteName(routeNames []string) func(c echo.Context) bool

func StaticMiddleware

func StaticMiddleware(docroot string, fs *embed.FS) echo.MiddlewareFunc

Types

type Config

type Config struct {
	App struct {
		Bind         string `json:"bind"          env:"APP_BIND"          envDefault:":8082"        validate:"required"`
		EchoTimeout  int    `json:"echo_timeout"  env:"APP_ECHO_TIMEOUT"  envDefault:"10"           validate:"required,gte=0"`
		GzipCompr    int    `json:"gzip_compr"    env:"APP_GZIP_COMPR"    envDefault:"-1"           validate:"gzip_compr"`
		ServerHeader string `json:"server_header" env:"APP_SERVER_HEADER" envDefault:"echocore/1.0"`
	} `json:"app"`
	Log struct {
		Level      string   `json:"level"       env:"LOG_LEVEL"       envDefault:"info" validate:"log_level"`
		TimeFormat string   `json:"time_format" env:"LOG_TIME_FORMAT" envDefault:"2006-01-02T15:04:05Z07:00" validate:"required"`
		LineFormat string   `` /* 145-byte string literal not displayed */
		SkipRoutes []string `json:"skip_routes" env:"LOG_SKIP_ROUTES"`
	} `json:"log"`
	DB struct {
		Addr      string `json:"addr"       env:"DB_ADDR"            envDefault:"localhost:3306" validate:"hostname_port"`
		User      string `json:"user"       env:"DB_USER"            envDefault:""`
		Pass      string `json:"pass"       env:"DB_PASS"            envDefault:""`
		Name      string `json:"name"       env:"DB_NAME"            envDefault:""`
		Timezone  string `json:"timezone"   env:"DB_TIMEZONE"        envDefault:"Europe/Berlin"  validate:"timezone"`
		Collation string `json:"collation"  env:"DB_COLLATION"       envDefault:"utf8mb4_unicode_ci"`
		Charset   string `json:"charset"    env:"DB_CHARSET"         envDefault:"utf8mb4"`
		ParseTime bool   `json:"parse_time" env:"DB_PARSE_TIME"      envDefault:"true"`
		Multi     bool   `json:"multi"      env:"DB_MULTI_STATEMENT" envDefault:"true"`
		MaxIdle   int    `json:"max_idle"   env:"DB_MAX_IDLE"        envDefault:"10"`
		MaxOpen   int    `json:"max_open"   env:"DB_MAX_OPEN"        envDefault:"50"`
		MaxLife   int    `json:"max_life"   env:"DB_MAX_LIFE"        envDefault:"60"`
		TLS       struct {
			Crt        string             `json:"crt"         env:"DB_TLS_CRT"         envDefault:""    validate:"omitempty,file"`
			Key        string             `json:"key"         env:"DB_TLS_KEY"         envDefault:""    validate:"omitempty,file"`
			ClientCAs  []string           `json:"client_cas"  env:"DB_TLS_CLIENT_CAS"`
			RootCAs    []string           `json:"root_cas"    env:"DB_TLS_ROOT_CAS"`
			SkipVerify bool               `json:"skip_verify" env:"DB_TLS_SKIP_VERIFY"`
			ClientAuth tls.ClientAuthType `json:"client_auth" env:"DB_TLS_CLIENT_AUTH" envDefault:"0"   validate:"client_auth"`
			MinVersion uint16             `json:"min_version" env:"DB_TLS_MIN_VERSION" envDefault:"771" validate:"tls_ver"`
		} `json:"tls"`
	} `json:"db"`
	Redis struct {
		Addr string `json:"addr" env:"REDIS_ADDR" envDefault:"localhost:6379" validate:"hostname_port"`
		User string `json:"user" env:"REDIS_USER" envDefault:""`
		Pass string `json:"pass" env:"REDIS_PASS" envDefault:""`
		TLS  struct {
			Crt        string             `json:"crt"         env:"REDIS_TLS_CRT"         envDefault:""    validate:"omitempty,file"`
			Key        string             `json:"key"         env:"REDIS_TLS_KEY"         envDefault:""    validate:"omitempty,file"`
			ClientCAs  []string           `json:"client_cas"  env:"REDIS_TLS_CLIENT_CAS"`
			RootCAs    []string           `json:"root_cas"    env:"REDIS_TLS_ROOT_CAS"`
			SkipVerify bool               `json:"skip_verify" env:"REDIS_TLS_SKIP_VERIFY"`
			ClientAuth tls.ClientAuthType `json:"client_auth" env:"REDIS_TLS_CLIENT_AUTH" envDefault:"0"   validate:"client_auth"`
			MinVersion uint16             `json:"min_version" env:"REDIS_TLS_MIN_VERSION" envDefault:"771" validate:"tls_ver"`
		} `json:"tls"`
	} `json:"redis"`
	Session struct {
		Path     string        `json:"path"      env:"SESS_PATH"         envDefault:"/"         validate:"required,gte=1"`
		Domain   string        `json:"domain"    env:"SESS_DOMAIN"       envDefault:"localhost" validate:"required"`
		MaxAge   int           `json:"max_age"   env:"SESS_MAX_AGE"      envDefault:"0"`
		Secure   bool          `json:"secure"    env:"SESS_SECURE"       envDefault:"false"`
		HTTPOnly bool          `json:"http_only" env:"SESS_HTTP_ONLY"    envDefault:"true"`
		SameSite http.SameSite `json:"same_site" env:"SESS_SAME_SITE"    envDefault:"1"         validate:"required,gte=1,lte=4"`
		SessID   string        `json:"sess_id"   env:"SESS_SESS_ID"      envDefault:"id"        validate:"required,gte=1,lte=64"`
		Seconds  int           `json:"seconds"   env:"SESS_SESS_SECONDS" envDefault:"600"       validate:"required,gte=1"`
	} `json:"session"`
	CSRF struct {
		TokenLength uint8  `json:"token_length" env:"CSRF_TOKEN_LENGTH" envDefault:"32"        validate:"gte=12"`
		TokenLookup string `json:"token_lookup" env:"CSRF_TOKEN_LOOKUP" envDefault:"form:csrf" validate:"required"`
		ContextKey  string `json:"context_key"  env:"CSRF_CONTEXT_KEY"  envDefault:"csrf"      validate:"required"`
		CookieName  string `json:"cookie_name"  env:"CSRF_COOKIE_NAME"  envDefault:"idc"       validate:"required"`
	} `json:"csrf"`
}

func (*Config) GommonLevel

func (cfg *Config) GommonLevel() log.Lvl

func (*Config) IsDebug

func (cfg *Config) IsDebug() bool

func (*Config) IsTLSConfiguredDB

func (cfg *Config) IsTLSConfiguredDB() bool

func (*Config) IsTLSConfiguredRedis

func (cfg *Config) IsTLSConfiguredRedis() bool

func (*Config) LogrusLevel

func (cfg *Config) LogrusLevel() logrus.Level

func (*Config) TLSConfigDB

func (cfg *Config) TLSConfigDB() (*tls.Config, error)

func (*Config) TLSConfigRedis

func (cfg *Config) TLSConfigRedis() (*tls.Config, error)

type Core

type Core struct {
	Config    *Config
	Gorm      *gorm.DB
	Redis     *redis.Client
	SessStore *redstore.RedisStore
	TmpDir    string
}

func NewCore

func NewCore() (*Core, error)

func (*Core) Init

func (c *Core) Init(inits []InitHandler) error

func (*Core) InitCopyFs added in v0.1.2

func (c *Core) InitCopyFs(dir string, fsys fs.FS) InitHandler

func (*Core) InitGorm

func (c *Core) InitGorm() InitHandler

func (*Core) InitRedis

func (c *Core) InitRedis() InitHandler

func (*Core) InitSessStore

func (c *Core) InitSessStore() InitHandler

func (*Core) InitTmpDir added in v0.1.2

func (c *Core) InitTmpDir() InitHandler

func (*Core) ListenSig

func (c *Core) ListenSig(ch chan os.Signal, e *echo.Echo, wg *sync.WaitGroup)

func (*Core) Shutdown

func (c *Core) Shutdown()

type CustomValidator

type CustomValidator struct {
	Validator *impl.Validate
}

func NewValidator

func NewValidator() *CustomValidator

func (*CustomValidator) Validate

func (v *CustomValidator) Validate(i interface{}) error

type Handler

type Handler interface {
	Init() error
	Exec() error
	Error(err error) error
}

type InitHandler

type InitHandler func() error

type Route

type Route struct {
	Ctx echo.Context
}

func NewRoute

func NewRoute(ctx echo.Context) Route

func (*Route) BadRequest

func (r *Route) BadRequest(err error) error

func (*Route) Bind

func (r *Route) Bind(i interface{}) error

func (*Route) BindVal

func (r *Route) BindVal(i interface{}) error

func (*Route) Config

func (r *Route) Config() *Config

func (*Route) Error

func (r *Route) Error(err error) error

func (*Route) Gorm

func (r *Route) Gorm() *gorm.DB

func (*Route) Redis

func (r *Route) Redis() *redis.Client

func (*Route) SessStore

func (r *Route) SessStore() *redstore.RedisStore

type ServiceMessage

type ServiceMessage struct {
	Message string `json:"message"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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