package main import ( "net/http" "strconv" "github.com/charmbracelet/log" "github.com/gin-gonic/gin" todo "github.com/1set/todotxt" ) func main() { router := gin.Default() config := getConfig() router.LoadHTMLGlob("templates/*.html") router.Static("/static", "static/") router.GET("/", loadTasksList) router.POST("/", loadTasksList) server := http.Server{ Addr: ":4443", Handler: router, TLSConfig: getTLSConfig(config), } defer server.Close() log.Fatal(server.ListenAndServeTLS("", "")) } func loadTasksList(c *gin.Context) { config := getConfig() // TODO Fix: YAML config file is read for each request taskList := getTaskList(config.TodoPath) 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(config.TodoPath) } else if content != "" { newTask, _ := todo.ParseTask(content) taskList.AddTask(newTask) taskList.WriteToPath(config.TodoPath) } c.HTML(http.StatusOK, "tasks-list.html", gin.H{ "tasks": taskList, }) } func getTaskList(todoFilePath string) todo.TaskList { if tasklist, err := todo.LoadFromPath(todoFilePath); err != nil { log.Fatalf("Can't load file %s", todoFilePath) return nil } else { return tasklist } }