Example #1
0
// Init must be called
func (h *Handler) Init(env zerver.Environment) error {
	if h.SaveImage == nil {
		panic("the function to save image should not be nil")
	}

	defval.Int64(&h.MaxMemory, 1<<19) // 512K

	defval.String(&h.FileKey, "image")
	if h.ErrNoFile == nil {
		h.ErrNoFile = httperrs.BadRequest.NewS("upload file not exists")
	}

	if h.Suffixes == nil {
		h.AddSuffixes("png", "jpg")
	}

	if h.ErrNonImage == nil {
		h.ErrNonImage = httperrs.BadRequest.NewS("the upload file is not an image")
	}

	defval.Int64(&h.MaxSize, 1<<18) // 256K
	if h.ErrTooLarge == nil {
		h.ErrTooLarge = httperrs.BadRequest.NewS("the upload file size is too large")
	}

	defval.String(&h.PathKey, "path")
	h.logger = env.Logger().Prefix("[zerver/image]")

	return nil
}
Example #2
0
func (ri *RequestId) Init(env zerver.Env) error {
	defval.Nil(&ri.Store, new(MemIDStore))
	ri.Store.Init(env)
	defval.String(&ri.HeaderName, "X-Request-Id")
	defval.String(&ri.Error, "header value X-Request-Id can't be empty")
	defval.String(&ri.ErrorOverlap, "request already accepted before, please wait")
	ri.log = log.Derive("Component", "RequestId")
	return nil
}
Example #3
0
func (ri *RequestId) Init(env zerver.Environment) error {
	defval.Nil(&ri.Store, new(MemIDStore))
	ri.Store.Init(env)
	defval.String(&ri.HeaderName, "X-Request-Id")
	defval.String(&ri.Error, "header value X-Request-Id can't be empty")
	defval.String(&ri.ErrorOverlap, "request already accepted before, please wait")
	ri.logger = env.Logger().Prefix("[RequestID]")

	return nil
}
Example #4
0
// Init must be called
func (h *Handler) Init(env zerver.Env) error {
	if h.SaveImage == nil {
		panic("the function to save image should not be nil")
	}

	defval.Int64(&h.MaxMemory, 1<<19) // 512K

	defval.String(&h.FileKey, "image")
	if h.ErrNoFile == nil {
		h.ErrNoFile = httperrs.BadRequest.NewS("upload file not exists")
	}

	if h.Suffixes == nil {
		h.AddSuffixes("png", "jpg")
	}

	if h.ErrNonImage == nil {
		h.ErrNonImage = httperrs.BadRequest.NewS("the upload file is not an image")
	}

	defval.Int64(&h.MaxSize, 1<<18) // 256K
	if h.ErrTooLarge == nil {
		h.ErrTooLarge = httperrs.BadRequest.NewS("the upload file size is too large")
	}

	h.log = log.Derive("Component", "ImageHandler")
	return nil
}
Example #5
0
func (o *ServerOption) init() {
	defval.String(&o.ListenAddr, ":4000")
	defval.Int(&o.PathVarCount, 3)
	defval.Int(&o.FilterCount, 5)
	if o.KeepAlivePeriod == 0 {
		o.KeepAlivePeriod = 3 * time.Minute // same as net/http/server.go:tcpKeepAliveListener
	}
	if o.Logger == nil {
		o.Logger = log2.Default()
	}
}
Example #6
0
func (r *RedisIDStore) Init(env zerver.Environment) error {
	rd, err := env.Component(component.REDIS)
	if err != nil {
		return err
	}

	r.store = rd.(kv.Store)
	defval.String(&r.Key, "RequestID")

	return nil
}
Example #7
0
func (j *JWTAuth) Init(s *zerver.Server) error {
	if j.JWT == nil {
		return ErrNilJWT
	}
	if j.JWT.Keyfunc == nil {
		return ErrNilKeyFunc
	}
	defval.String(&j.AuthTokenAttrName, "AuthToken")
	defval.Nil(&j.JWT.SigningMethod, jwt.SigningMethodHS256)

	return nil
}
Example #8
0
func (o *ServerOption) init() {
	if o.Logger == nil {
		o.Logger = log.Derive("Framework", "Server")
	}

	defval.String(&o.ListenAddr, ":4000")
	if o.KeepAlivePeriod == 0 {
		o.KeepAlivePeriod = 3 * time.Minute // same as net/http/server.go:tcpKeepAliveListener
	}
	if o.Codec == nil {
		o.Codec = encoding.JSON
	}
}
Example #9
0
func (o *RedisOption) init() {
	if o.MaxIdle == 0 {
		o.MaxIdle = 8
	}

	if o.Dial == nil {
		defval.String(&o.Addr, ":6379")

		o.Dial = func() (redis.Conn, error) {
			return redis.Dial("tcp", o.Addr)
		}
	}
}
Example #10
0
func (x *Xsrf) Init(env zerver.Env) error {
	if x.Secret == "" {
		return errors.Err("xsrf secret can't be empty")
	}

	defval.Int64(&x.Timeout, _DEF_XSRF_TIMEOUT)
	defval.Nil(&x.HashMethod, sha256.New)
	defval.String(&x.Error, "xsrf token is invalid or not found")

	if x.UsePool {
		if x.Pool == nil {
			x.Pool = bytes2.NewSyncPool(0, true)
		}
	} else {
		x.Pool = bytes2.FakePool{}
	}
	defval.Nil(&x.TokenInfo, jsonToken{})
	x.log = log.Derive("Component", "Xsrf")
	return nil
}
Example #11
0
func (l *Log) Init(env zerver.Environment) error {
	defval.String(&l.Prefix, "[Access]")
	l.logger = env.Logger().Prefix(l.Prefix)
	return nil
}