package main import ( "net/http" "strconv" "github.com/charmbracelet/log" "github.com/gin-gonic/gin" todo "github.com/1set/todotxt" ) var todoFilePath string = "todo.txt" func main() { gin.SetMode(gin.DebugMode) router := gin.Default() router.LoadHTMLGlob("templates/*.html") router.Static("/static", "static/") router.GET("/", loadTasksList) router.POST("/", loadTasksList) router.Run() } func loadTasksList(c *gin.Context) { taskList := getTaskList() id := c.PostForm("taskId") content := c.PostForm("taskContent") if id != "" { taskId, err := strconv.Atoi(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if updatedTask, err := taskList.GetTask(taskId); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "taskId": taskId}) return } else { if c.PostForm("checked") == "on" { updatedTask.Complete() } else { updatedTask.Reopen() } } taskList.WriteToPath(todoFilePath) } else if content != "" { newTask, _ := todo.ParseTask(content) taskList.AddTask(newTask) taskList.WriteToPath(todoFilePath) } c.HTML(http.StatusOK, "tasks-list.html", gin.H{ "tasks": taskList, }) } func getTaskList() todo.TaskList { if tasklist, err := todo.LoadFromPath(todoFilePath); err != nil { log.Fatalf("Can't load file %s", todoFilePath) return nil } else { return tasklist } }