github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/common/common.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "reflect" 8 9 "github.com/gin-gonic/gin" 10 "github.com/mdaxf/iac/framework/auth" 11 "github.com/mdaxf/iac/logger" 12 ) 13 14 func GetRequestUser(ctx *gin.Context) (string, string, string, error) { 15 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "GetRequestUser"} 16 /* startTime := time.Now() 17 18 defer func() { 19 elapsed := time.Since(startTime) 20 iLog.PerformanceWithDuration("controllers.common.GetRequestUser", elapsed) 21 }() 22 defer func() { 23 err := recover() 24 if err != nil { 25 iLog.Error(fmt.Sprintf("controllers.common.GetRequestUser error: %s", err)) 26 } 27 }() 28 */ 29 // iLog.Debug("GetRequestUser") 30 31 userid, user, clientid, err := auth.GetUserInformation(ctx) 32 33 if err != nil { 34 iLog.Error(fmt.Sprintf("GetRequestUser error: %s", err.Error())) 35 return "", "", "", err 36 } 37 iLog.Debug(fmt.Sprintf("GetRequestUser userid: %s user: %s clientid: %s", userid, user, clientid)) 38 39 return userid, user, clientid, nil 40 } 41 42 func GetRequestBody(ctx *gin.Context) ([]byte, error) { 43 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "GetRequestBody"} 44 /* startTime := time.Now() 45 46 defer func() { 47 elapsed := time.Since(startTime) 48 iLog.PerformanceWithDuration("controllers.common.GetRequestBody", elapsed) 49 }() 50 51 defer func() { 52 err := recover() 53 if err != nil { 54 iLog.Error(fmt.Sprintf("controllers.common.GetRequestBody error: %s", err)) 55 } 56 }() 57 */ 58 _, user, clientid, _ := GetRequestUser(ctx) 59 60 iLog.ClientID = clientid 61 iLog.User = user 62 63 iLog.Debug(fmt.Sprintf("GetRequestBody")) 64 65 body, err := ioutil.ReadAll(ctx.Request.Body) 66 67 if err != nil { 68 iLog.Error(fmt.Sprintf("GetRequestBody error: %s", err.Error())) 69 return nil, err 70 } 71 // iLog.Debug(fmt.Sprintf("GetRequestBody body: %s", body)) 72 return body, nil 73 } 74 75 func GetRequestBodyandUser(ctx *gin.Context) ([]byte, string, string, error) { 76 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "GetRequestBody"} 77 /* startTime := time.Now() 78 79 defer func() { 80 elapsed := time.Since(startTime) 81 iLog.PerformanceWithDuration("controllers.common.GetRequestBody", elapsed) 82 }() 83 84 defer func() { 85 err := recover() 86 if err != nil { 87 iLog.Error(fmt.Sprintf("controllers.common.GetRequestBody error: %s", err)) 88 } 89 }() 90 */ 91 _, user, clientid, _ := GetRequestUser(ctx) 92 93 iLog.ClientID = clientid 94 iLog.User = user 95 96 body, err := ioutil.ReadAll(ctx.Request.Body) 97 98 if err != nil { 99 iLog.Error(fmt.Sprintf("GetRequestBody error: %s", err.Error())) 100 return nil, clientid, user, err 101 } 102 iLog.Debug(fmt.Sprintf("GetRequestBody body: %s", body)) 103 return body, clientid, user, nil 104 } 105 106 func GetRequestBodyandUserbyJson(ctx *gin.Context) (map[string]interface{}, string, string, error) { 107 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "GetRequestBodyandUserbyJson"} 108 /* startTime := time.Now() 109 110 defer func() { 111 elapsed := time.Since(startTime) 112 iLog.PerformanceWithDuration("controllers.common.GetRequestBodyandUserbyJson", elapsed) 113 }() 114 defer func() { 115 err := recover() 116 if err != nil { 117 iLog.Error(fmt.Sprintf("controllers.common.GetRequestBodyandUserbyJson error: %s", err)) 118 } 119 }() 120 */ 121 _, user, clientid, _ := GetRequestUser(ctx) 122 123 iLog.ClientID = clientid 124 iLog.User = user 125 iLog.Debug(fmt.Sprintf("GetRequestBodyandUserbyJson")) 126 127 var request map[string]interface{} 128 err := json.NewDecoder(ctx.Request.Body).Decode(&request) 129 if err != nil { 130 iLog.Error(fmt.Sprintf("Failed to decode request body: %v", err)) 131 return nil, clientid, user, err 132 } 133 iLog.Debug(fmt.Sprintf("GetRequestBodybyJson request: %s", request)) 134 return request, clientid, user, nil 135 } 136 137 func GetRequestBodybyJson(ctx *gin.Context) (map[string]interface{}, error) { 138 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "GetRequestBodybyJson"} 139 /* startTime := time.Now() 140 141 defer func() { 142 elapsed := time.Since(startTime) 143 iLog.PerformanceWithDuration("controllers.common.GetRequestBodybyJson", elapsed) 144 }() 145 defer func() { 146 err := recover() 147 if err != nil { 148 iLog.Error(fmt.Sprintf("controllers.common.GetRequestBodybyJson error: %s", err)) 149 } 150 }() 151 */ 152 _, user, clientid, err := GetRequestUser(ctx) 153 154 iLog.ClientID = clientid 155 iLog.User = user 156 iLog.Debug(fmt.Sprintf("GetRequestBodybyJson")) 157 158 var request map[string]interface{} 159 err = json.NewDecoder(ctx.Request.Body).Decode(&request) 160 if err != nil { 161 iLog.Error(fmt.Sprintf("Failed to decode request body: %v", err)) 162 return nil, err 163 } 164 iLog.Debug(fmt.Sprintf("GetRequestBodybyJson request: %s", request)) 165 return request, nil 166 } 167 168 func mapToStruct(data map[string]interface{}, outStruct interface{}) error { 169 // Get the type of the struct 170 structType := reflect.TypeOf(outStruct) 171 172 // Make sure outStruct is a pointer to a struct 173 if structType.Kind() != reflect.Ptr || structType.Elem().Kind() != reflect.Struct { 174 return fmt.Errorf("outStruct must be a pointer to a struct") 175 } 176 177 // Get the value of the struct 178 structValue := reflect.ValueOf(outStruct).Elem() 179 180 // Iterate through the map and set struct fields 181 for key, value := range data { 182 structField := structValue.FieldByName(key) 183 184 if !structField.IsValid() { 185 continue 186 //return fmt.Errorf("field %s does not exist in the struct", key) 187 } 188 189 if !structField.CanSet() { 190 continue 191 //return fmt.Errorf("field %s cannot be set", key) 192 } 193 194 fieldValue := reflect.ValueOf(value) 195 if !fieldValue.Type().AssignableTo(structField.Type()) { 196 //return fmt.Errorf("field %s type mismatch", key) 197 continue 198 } 199 200 structField.Set(fieldValue) 201 } 202 203 return nil 204 }