package main import ( "embed" "flag" "html/template" "net/http" "strconv" "github.com/charmbracelet/log" "github.com/gin-gonic/gin" todo "github.com/1set/todotxt" ) //go:embed static/* templates/* var fs embed.FS func main() { configPath := flag.String("config", "config.yaml", "Path to config file") flag.Parse() config := getConfig(*configPath) router := gin.Default() templ := template.Must(template.New("").ParseFS(fs, "templates/*.html")) router.SetHTMLTemplate(templ) router.StaticFS("/static", http.FS(fs)) 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) { taskList := getTaskList("todo.txt") 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("todo.txt") } else if content != "" { newTask, _ := todo.ParseTask(content) taskList.AddTask(newTask) taskList.WriteToPath("todo.txt") } 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 } }