Reduce handler request boilerplate
- Add session.Attach to reuse per-request sessions across middleware and ghome handlers - Make Session finalize/abort paths idempotent to avoid duplicate commit or rollback work - Move request_data parsing helper to internal/utils - Expand ParseRequestData usage across api, download, unit, live, multiunit, and subscenario handlers - Extract download package metadata into internal/handler/download/types.go - Return explicit errors from selected session lookup helpers and update callers - Make api action handlers return errors directly instead of calling ss.CheckErr in nested layers Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
@@ -6,26 +6,30 @@ import (
|
||||
)
|
||||
|
||||
func (ss *Session) GetDeviceID() string {
|
||||
return ss.Ctx.Request.Header.Get("X-DEVICEID")
|
||||
return ss.deviceID
|
||||
}
|
||||
|
||||
func (ss *Session) GetRandKey() []byte {
|
||||
func (ss *Session) GetRandKey() ([]byte, error) {
|
||||
deviceKey := ghomemodel.DeviceKey{}
|
||||
has, err := ss.UserEng.Table(new(ghomemodel.DeviceKey)).
|
||||
Where("device_id = ?", ss.GetDeviceID()).Get(&deviceKey)
|
||||
if ss.CheckErr(err) {
|
||||
return nil
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !has {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return []byte(deviceKey.RandKey)
|
||||
return []byte(deviceKey.RandKey), nil
|
||||
}
|
||||
func (ss *Session) SetRandKey(key string) {
|
||||
var err error
|
||||
if ss.GetRandKey() == nil {
|
||||
randKey, err := ss.GetRandKey()
|
||||
if ss.CheckErr(err) {
|
||||
return
|
||||
}
|
||||
if randKey == nil {
|
||||
_, err = ss.UserEng.Insert(&ghomemodel.DeviceKey{
|
||||
DeviceID: ss.GetDeviceID(),
|
||||
RandKey: key,
|
||||
@@ -40,7 +44,10 @@ func (ss *Session) SetRandKey(key string) {
|
||||
}
|
||||
|
||||
func (ss *Session) Get3DESRandKey() ([]byte, error) {
|
||||
randKey := ss.GetRandKey()
|
||||
randKey, err := ss.GetRandKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(randKey) < 24 {
|
||||
return nil, errors.New("invalid rand key")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user