package main import ( "encoding/json" "fmt" "log" "net/http" "time" "github.com/gin-gonic/gin" cfg "github.com/jesusperez/cfgsrv" utils "github.com/jesusperez/datautils" ) func get_tracking_list_handle(c *gin.Context, rtenv *RouteEnv) { role := rtenv.Cfg.AdminRole user,res := user_has_role(nil,c,rtenv,role) if !res { c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"}) return } var trackingData []string result := false path := "" var err error switch (rtenv.Cfg.TrackingStore) { case cfg.DsRedis: path = "redis" // TODO send to redis client case cfg.DsFile: path = rtenv.Cfg.TrackingOut trackingData = getFileMatch(path,".json",false,false) result = true } if result { fmt.Printf("Tracking actions from (%s): %s\n",user.UserName, path) logRoute(c,rtenv,"get_tracking",fmt.Sprintf("get %s: %s",user.UserName,path), fmt.Sprintf("get %s: %s",user.UserName,path)) // c.JSON(http.StatusOK, gin.H{"data": trackingData}) c.JSON(http.StatusOK, trackingData) } else { logRoute(c,rtenv,"get_tracking",fmt.Sprintf("get %s: read error %v ",user.UserName,err), fmt.Sprintf("get %s: %s",user.UserName,path)) fmt.Printf("Error get tracking from (%s) %s: %v\n", user.UserName, path, err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read tracking list"}) } } func get_tracking_handle(c *gin.Context, rtenv *RouteEnv) { role := rtenv.Cfg.AdminRole user,res := user_has_role(nil,c,rtenv,role) if !res { c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"}) return } target := c.Params.ByName(rtenv.Cfg.Routes["tracking"].Param) var trackingData []TrackAction result := false path := "" var err error switch (rtenv.Cfg.TrackingStore) { case cfg.DsRedis: path = "redis" // TODO send to redis client case cfg.DsFile: path = fmt.Sprintf("%s/%s.json",rtenv.Cfg.TrackingOut,target) var data []byte data, err = loadPath(path) if err == nil { err = json.Unmarshal([]byte(fmt.Sprintf("[%s{}]",string(data))), &trackingData) if err == nil { result = true } } } if result { fmt.Printf("Tracking actions from (%s): %s\n",user.UserName, path) logRoute(c,rtenv,"get_tracking",fmt.Sprintf("get %s: %s",user.UserName,path), fmt.Sprintf("get %s: %s",user.UserName,path)) // c.JSON(http.StatusOK, gin.H{"data": trackingData}) c.JSON(http.StatusOK, trackingData) } else { logRoute(c,rtenv,"get_tracking",fmt.Sprintf("get %s: read error %v ",user.UserName,err), fmt.Sprintf("get %s: %s",user.UserName,path)) fmt.Printf("Error get tracking from (%s) %s: %v\n", user.UserName, path, err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read tracking"}) } } func post_tracking_handle(c *gin.Context, rtenv *RouteEnv) { var trackpost PostTrackAction err := c.BindJSON(&trackpost) if err != nil { fmt.Printf("err: %+v\n", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save info"}) return } if len(trackpost.Data) == 0 { c.JSON(http.StatusNotAcceptable, gin.H{"track": "error data"}) return } userid := trackpost.Data _, okmdl := rtenv.MdlsUsrs[userid] auth := "" if !okmdl { _, okusr := rtenv.Users.Accounts[userid] // if !okusr { // c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"}) // return // } if okusr && rtenv.Cfg.UseJWT { tk, errtk := rtenv.AuthMiddleware.ParseToken(c) if errtk == nil { auth = fmt.Sprintf("%v",tk.Raw) } } } ip, _ := getIP(c.Request,true) trackaction := &TrackAction{ When: trackpost.When, Where: trackpost.Where, What: trackpost.What, Context: trackpost.Context, Data: trackpost.Data, Auth: auth, Ip: ip, // Info: requestInfo(c,rtenv,"post_tracking",fmt.Sprintf("post %s",trackpost.Data), fmt.Sprintf("post %s",userid)), } data,err := json.Marshal(trackaction) if err != nil { logRoute(c,rtenv,"post_tracking",fmt.Sprintf("post %s: error %v ",trackpost.Data,err), fmt.Sprintf("post %s",userid)) fmt.Printf("Error Parse tracking %s: %v\n", trackpost.Data, err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save tracking"}) return } result := false path := "" switch (rtenv.Cfg.TrackingStore) { case cfg.DsRedis: path = "redis" // TODO send to redis client case cfg.DsFile: path = fmt.Sprintf("%s/%s.json",rtenv.Cfg.TrackingOut,time.Now().Format("2006_01_02")) err := utils.CheckDirPath(fmt.Sprintf("%s",rtenv.Cfg.TrackingOut)) if err != nil { log.Fatalf("setReqLog path %s: %v",rtenv.Cfg.TrackingOut,err) } err = utils.WriteAppendData(fmt.Sprintf("%s,",string(data)),path) if err == nil { result = true } } if result { fmt.Printf("Track action from %s writed to: %s\n", userid, path) logRoute(c,rtenv,"post_tracking",fmt.Sprintf("post %s: %s",userid,path), fmt.Sprintf("post %s: %s",userid,path)) c.JSON(http.StatusOK, gin.H{"status": "ok"}) } else { logRoute(c,rtenv,"post_tracking",fmt.Sprintf("post %s: write error %v ",userid,err), fmt.Sprintf("post %s: %s",userid,path)) fmt.Printf("Error write tracking from %s to %s: %v\n", userid, path, err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save tracking"}) } }