Beispiel #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
}
Beispiel #2
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
}
Beispiel #3
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
}
Beispiel #4
0
func (r *Redis) Init(env zerver.Environment) error {
	compEnv := env.(zerver.ComponentEnvironment)
	var o *RedisOption
	if op := compEnv.GetSetAttr(REDIS, nil); op == nil {
		o = &RedisOption{}
	} else {
		o = op.(*RedisOption)
	}
	o.init()

	r.MaxIdle = o.MaxIdle
	r.MaxActive = o.MaxActive
	r.IdleTimeout = time.Duration(o.IdleTimeout) * time.Second
	r.Dial = o.Dial
	r.logger = env.Logger().Prefix("[Redis]")

	_, err := r.Get().Do("PING")
	return err
}
Beispiel #5
0
func (m *Queue) Init(env zerver.Environment) error {
	if m.Processor == nil {
		return errors.Err("message processor shouldn't be nil")
	}
	if m.TaskBufsize == 0 {
		m.TaskBufsize = 256
	}
	if m.ErrorLogger == nil {
		m.ErrorLogger = env.Logger()
	}
	m.ErrorLogger = m.ErrorLogger.Prefix("zerver/msq")
	if m.BytesPool == nil {
		m.BytesPool = bytes2.NewFakePool()
	}

	m.queue = make(chan zerver.Task, m.TaskBufsize)

	go m.start()
	return nil
}
Beispiel #6
0
func (x *Xsrf) Init(env zerver.Environment) 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.logger = env.Logger().Prefix("[XSRF]")
	return nil
}
Beispiel #7
0
func (l *Log) Init(env zerver.Environment) error {
	defval.String(&l.Prefix, "[Access]")
	l.logger = env.Logger().Prefix(l.Prefix)
	return nil
}
Beispiel #8
0
func (r *Recovery) Init(env zerver.Environment) error {
	r.logger = env.Logger().Prefix("[Recovery]")
	defval.Int(&r.Bufsize, 1024*4)
	return nil
}