diff options
author | xengineering <me@xengineering.eu> | 2023-04-13 19:35:29 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-04-13 20:21:58 +0200 |
commit | 937d0e07d9ad4ed98b4349de568de325c5cc7e70 (patch) | |
tree | c5effe3af464c4065d9c6aa86e76e038c60c6a29 | |
parent | ee9fd81f40c1e470f83be06717affd34779dd29f (diff) | |
download | ceres-937d0e07d9ad4ed98b4349de568de325c5cc7e70.tar ceres-937d0e07d9ad4ed98b4349de568de325c5cc7e70.tar.zst ceres-937d0e07d9ad4ed98b4349de568de325c5cc7e70.zip |
Allow empty recipe list
This is useful for the case that no recipes exist or the recipe data
folder does not exist.
-rw-r--r-- | handler.go | 56 |
1 files changed, 24 insertions, 32 deletions
@@ -23,34 +23,31 @@ type Recipe struct { func indexGet(w http.ResponseWriter, r *http.Request) { - entries, err := os.ReadDir(filepath.Join(config.Data, "recipes")) - if err != nil { - http.Error(w, "Could not list recipes!", 500) - return - } - recipes := make([]Recipe, 0) - for _, v := range entries { - if v.IsDir() == false { - continue - } - - _, err = strconv.Atoi(v.Name()) - if err != nil { - continue + entries, err := os.ReadDir(filepath.Join(config.Data, "recipes")) + if err == nil { + for _, v := range entries { + if v.IsDir() == false { + continue + } + + _, err = strconv.Atoi(v.Name()) + if err != nil { + continue + } + + textpath := filepath.Join(config.Data, "recipes", v.Name(), "text") + data, _ := ioutil.ReadFile(textpath) + markup := Markup(data) + + recipes = append(recipes, Recipe{ + v.Name(), + markup.title(), + string(data), + "", + }) } - - textpath := filepath.Join(config.Data, "recipes", v.Name(), "text") - data, _ := ioutil.ReadFile(textpath) - markup := Markup(data) - - recipes = append(recipes, Recipe{ - v.Name(), - markup.title(), - string(data), - "", - }) } ServeTemplate(w, "index.html", recipes) @@ -164,14 +161,9 @@ func recipeConfirmDeletionPost(w http.ResponseWriter, r *http.Request) { func addRecipesGet(w http.ResponseWriter, r *http.Request) { - entries, err := os.ReadDir(filepath.Join(config.Data, "recipes")) - if err != nil { - http.Error(w, "Could not get list of existing recipes!", 500) - return - } - var biggest int = -1 + entries, _ := os.ReadDir(filepath.Join(config.Data, "recipes")) for _, v := range entries { if v.IsDir() == false { continue @@ -191,7 +183,7 @@ func addRecipesGet(w http.ResponseWriter, r *http.Request) { newIdStr := strconv.Itoa(newId) recipedir := filepath.Join(config.Data, "recipes", newIdStr) - err = os.Mkdir(recipedir, 0755) + err := os.Mkdir(recipedir, 0755) if err != nil { http.Error(w, "Could not create new recipe!", 500) return |