Insanely verbose logging

This commit is contained in:
Imbus 2024-03-19 00:00:18 +01:00
parent 71caf37642
commit fe08d01e15
3 changed files with 43 additions and 13 deletions

View file

@ -68,6 +68,7 @@ func (gs *GState) GetProject(c *fiber.Ctx) error {
// Extract the project ID from the request parameters or body // Extract the project ID from the request parameters or body
projectID := c.Params("projectID") projectID := c.Params("projectID")
if projectID == "" { if projectID == "" {
log.Info("No project ID provided")
return c.Status(400).SendString("No project ID provided") return c.Status(400).SendString("No project ID provided")
} }
log.Info("Getting project with ID: ", projectID) log.Info("Getting project with ID: ", projectID)
@ -75,12 +76,14 @@ func (gs *GState) GetProject(c *fiber.Ctx) error {
// Parse the project ID into an integer // Parse the project ID into an integer
projectIDInt, err := strconv.Atoi(projectID) projectIDInt, err := strconv.Atoi(projectID)
if err != nil { if err != nil {
log.Info("Invalid project ID")
return c.Status(400).SendString("Invalid project ID") return c.Status(400).SendString("Invalid project ID")
} }
// Get the project from the database by its ID // Get the project from the database by its ID
project, err := gs.Db.GetProject(projectIDInt) project, err := gs.Db.GetProject(projectIDInt)
if err != nil { if err != nil {
log.Info("Error getting project:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
@ -92,13 +95,20 @@ func (gs *GState) GetProject(c *fiber.Ctx) error {
func (gs *GState) ListAllUsersProject(c *fiber.Ctx) error { func (gs *GState) ListAllUsersProject(c *fiber.Ctx) error {
// Extract the project name from the request parameters or body // Extract the project name from the request parameters or body
projectName := c.Params("projectName") projectName := c.Params("projectName")
if projectName == "" {
log.Info("No project name provided")
return c.Status(400).SendString("No project name provided")
}
// Get all users associated with the project from the database // Get all users associated with the project from the database
users, err := gs.Db.GetAllUsersProject(projectName) users, err := gs.Db.GetAllUsersProject(projectName)
if err != nil { if err != nil {
log.Info("Error getting users for project:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
log.Info("Returning users for project: ", projectName)
// Return the list of users as JSON // Return the list of users as JSON
return c.JSON(users) return c.JSON(users)
} }

View file

@ -17,50 +17,62 @@ func (gs *GState) SubmitWeeklyReport(c *fiber.Ctx) error {
report := new(types.NewWeeklyReport) report := new(types.NewWeeklyReport)
if err := c.BodyParser(report); err != nil { if err := c.BodyParser(report); err != nil {
log.Info("Error parsing weekly report")
return c.Status(400).SendString(err.Error()) return c.Status(400).SendString(err.Error())
} }
// Make sure all the fields of the report are valid // Make sure all the fields of the report are valid
if report.Week < 1 || report.Week > 52 { if report.Week < 1 || report.Week > 52 {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number") return c.Status(400).SendString("Invalid week number")
} }
if report.DevelopmentTime < 0 || report.MeetingTime < 0 || report.AdminTime < 0 || report.OwnWorkTime < 0 || report.StudyTime < 0 || report.TestingTime < 0 { if report.DevelopmentTime < 0 || report.MeetingTime < 0 || report.AdminTime < 0 || report.OwnWorkTime < 0 || report.StudyTime < 0 || report.TestingTime < 0 {
log.Info("Invalid time report")
return c.Status(400).SendString("Invalid time report") return c.Status(400).SendString("Invalid time report")
} }
if err := gs.Db.AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil { if err := gs.Db.AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil {
log.Info("Error adding weekly report")
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
log.Info("Weekly report added")
return c.Status(200).SendString("Time report added") return c.Status(200).SendString("Time report added")
} }
// Handler for retrieving weekly report // Handler for retrieving weekly report
func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error { func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the request // Extract the necessary parameters from the request
log.Info("GetWeeklyReport")
user := c.Locals("user").(*jwt.Token) user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims) claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string) username := claims["name"].(string)
log.Info("Getting weekly report for: ", username)
// Extract project name and week from query parameters // Extract project name and week from query parameters
projectName := c.Query("projectName") projectName := c.Query("projectName")
log.Info(projectName)
week := c.Query("week") week := c.Query("week")
log.Info(week)
if projectName == "" || week == "" {
log.Info("Missing project name or week number")
return c.Status(400).SendString("Missing project name or week number")
}
// Convert week to integer // Convert week to integer
weekInt, err := strconv.Atoi(week) weekInt, err := strconv.Atoi(week)
if err != nil { if err != nil {
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number") return c.Status(400).SendString("Invalid week number")
} }
// Call the database function to get the weekly report // Call the database function to get the weekly report
report, err := gs.Db.GetWeeklyReport(username, projectName, weekInt) report, err := gs.Db.GetWeeklyReport(username, projectName, weekInt)
if err != nil { if err != nil {
log.Info("Error getting weekly report from db:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
log.Info("Returning weekly report")
// Return the retrieved weekly report // Return the retrieved weekly report
return c.JSON(report) return c.JSON(report)
} }
@ -70,12 +82,13 @@ type ReportId struct {
} }
func (gs *GState) SignReport(c *fiber.Ctx) error { func (gs *GState) SignReport(c *fiber.Ctx) error {
log.Info("Signing report...")
// Extract the necessary parameters from the token // Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token) user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims) claims := user.Claims.(jwt.MapClaims)
projectManagerUsername := claims["name"].(string) projectManagerUsername := claims["name"].(string)
log.Info("Signing report for: ", projectManagerUsername)
// Extract report ID from the request query parameters // Extract report ID from the request query parameters
// reportID := c.Query("reportId") // reportID := c.Query("reportId")
rid := new(ReportId) rid := new(ReportId)
@ -83,22 +96,19 @@ func (gs *GState) SignReport(c *fiber.Ctx) error {
return err return err
} }
log.Info("Signing report for: ", rid.ReportId) log.Info("Signing report for: ", rid.ReportId)
// reportIDInt, err := strconv.Atoi(rid.ReportId)
// log.Info("Signing report for: ", rid.ReportId)
// if err != nil {
// return c.Status(400).SendString("Invalid report ID")
// }
// Get the project manager's ID // Get the project manager's ID
projectManagerID, err := gs.Db.GetUserId(projectManagerUsername) projectManagerID, err := gs.Db.GetUserId(projectManagerUsername)
if err != nil { if err != nil {
log.Info("Failed to get project manager ID")
return c.Status(500).SendString("Failed to get project manager ID") return c.Status(500).SendString("Failed to get project manager ID")
} }
log.Info("blabla", projectManagerID) log.Info("Project manager ID: ", projectManagerID)
// Call the database function to sign the weekly report // Call the database function to sign the weekly report
err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID) err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID)
if err != nil { if err != nil {
log.Info("Error signing weekly report:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }

View file

@ -28,9 +28,9 @@ func (gs *GState) Register(c *fiber.Ctx) error {
return c.Status(400).SendString(err.Error()) return c.Status(400).SendString(err.Error())
} }
log.Info("Adding user:", u.Username)
log.Info("Adding user:", u.Username) log.Info("Adding user:", u.Username)
if err := gs.Db.AddUser(u.Username, u.Password); err != nil { if err := gs.Db.AddUser(u.Username, u.Password); err != nil {
log.Warn("Error adding user:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
@ -48,13 +48,16 @@ func (gs *GState) UserDelete(c *fiber.Ctx) error {
auth_username := c.Locals("user").(*jwt.Token).Claims.(jwt.MapClaims)["name"].(string) auth_username := c.Locals("user").(*jwt.Token).Claims.(jwt.MapClaims)["name"].(string)
if username != auth_username { if username != auth_username {
log.Info("User tried to delete another user")
return c.Status(403).SendString("You can only delete yourself") return c.Status(403).SendString("You can only delete yourself")
} }
if err := gs.Db.RemoveUser(username); err != nil { if err := gs.Db.RemoveUser(username); err != nil {
log.Warn("Error deleting user:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
log.Info("User deleted:", username)
return c.Status(200).SendString("User deleted") return c.Status(200).SendString("User deleted")
} }
@ -67,7 +70,7 @@ func (gs *GState) Login(c *fiber.Ctx) error {
return c.Status(400).SendString(err.Error()) return c.Status(400).SendString(err.Error())
} }
log.Info("Username:", u.Username) log.Info("Username logging in:", u.Username)
if !gs.Db.CheckUser(u.Username, u.Password) { if !gs.Db.CheckUser(u.Username, u.Password) {
log.Info("User not found") log.Info("User not found")
return c.SendStatus(fiber.StatusUnauthorized) return c.SendStatus(fiber.StatusUnauthorized)
@ -97,8 +100,10 @@ func (gs *GState) Login(c *fiber.Ctx) error {
// LoginRenew is a simple handler that renews the token // LoginRenew is a simple handler that renews the token
func (gs *GState) LoginRenew(c *fiber.Ctx) error { func (gs *GState) LoginRenew(c *fiber.Ctx) error {
// For testing: curl localhost:3000/restricted -H "Authorization: Bearer <token>"
user := c.Locals("user").(*jwt.Token) user := c.Locals("user").(*jwt.Token)
log.Info("Renewing token for user:", user.Claims.(jwt.MapClaims)["name"])
claims := user.Claims.(jwt.MapClaims) claims := user.Claims.(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * 72).Unix() claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
renewed := jwt.MapClaims{ renewed := jwt.MapClaims{
@ -109,8 +114,11 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, renewed) token := jwt.NewWithClaims(jwt.SigningMethodHS256, renewed)
t, err := token.SignedString([]byte("secret")) t, err := token.SignedString([]byte("secret"))
if err != nil { if err != nil {
log.Warn("Error signing token")
return c.SendStatus(fiber.StatusInternalServerError) return c.SendStatus(fiber.StatusInternalServerError)
} }
log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"])
return c.JSON(fiber.Map{"token": t}) return c.JSON(fiber.Map{"token": t})
} }
@ -119,9 +127,11 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error {
// Get all users from the database // Get all users from the database
users, err := gs.Db.GetAllUsersApplication() users, err := gs.Db.GetAllUsersApplication()
if err != nil { if err != nil {
log.Info("Error getting users from db:", err) // Debug print
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
log.Info("Returning all users")
// Return the list of users as JSON // Return the list of users as JSON
return c.JSON(users) return c.JSON(users)
} }