diff --git a/client/lib/main.dart b/client/lib/main.dart index 6b63d4b..87bc79a 100644 --- a/client/lib/main.dart +++ b/client/lib/main.dart @@ -89,7 +89,10 @@ class _MyHomePageState extends State { List rows = List.empty(growable: true); for (Recipe recipe in snapshot.data!) { - rows.add(TableRow(children: [Text(recipe.name)])); + var image = Image.network(fit: BoxFit.fitHeight, + height: 32.0, + "http://localhost:8080/api/image/${recipe.icon}"); + rows.add(TableRow(children: [TableCell(child: image), TableCell(child: Text(recipe.name))])); } return Table(children: rows); diff --git a/server/.env b/server/.env index 090904b..af02248 100644 --- a/server/.env +++ b/server/.env @@ -1,2 +1,3 @@ GREPFOOD_SYNCSERVER_PORT=:8080 GREPFOOD_DATABASE_CONNECTION=postgresql://user:password@localhost:5432/grepfood?sslmode=disable +GREPFOOD_IMAGE_LOCAL_STORAGE_PATH=./mockup/images diff --git a/server/main.go b/server/main.go index 9085939..893c1ef 100644 --- a/server/main.go +++ b/server/main.go @@ -7,6 +7,8 @@ import ( "log" "net/http" "os" + "path/filepath" + _ "github.com/lib/pq" ) @@ -228,6 +230,25 @@ func HandleApiIngedientAdd(w http.ResponseWriter, r *http.Request, db *sql.DB) { } +func HandleGetImage(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet && r.Method != "" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + log.Print("Mismatched method: ", r.Method) + return + } + + var imageSearchPath = os.Getenv("GREPFOOD_IMAGE_LOCAL_STORAGE_PATH"); + + bytes, err := os.ReadFile(filepath.Join(imageSearchPath, r.PathValue("name"))) + if err != nil { + log.Print(err) + http.Error(w, "File not found", http.StatusNotFound) + } + + w.Header().Set("Content-Type", "image/png") + w.Write(bytes) +} + func main() { dbconn := os.Getenv("GREPFOOD_DATABASE_CONNECTION") @@ -250,6 +271,9 @@ func main() { http.HandleFunc("/api/recipe/{name}/ingredients", func(w http.ResponseWriter, r *http.Request) { HandleApiRecipeIngredients(w, r, db) }) + http.HandleFunc("/api/image/{name}", func(w http.ResponseWriter, r *http.Request) { + HandleGetImage(w, r) + }) log.Fatal(http.ListenAndServe(os.Getenv("GREPFOOD_SYNCSERVER_PORT"), nil)) } diff --git a/server/mockup/images/bread.png b/server/mockup/images/bread.png new file mode 100644 index 0000000..789c61c Binary files /dev/null and b/server/mockup/images/bread.png differ diff --git a/server/mockup/images/cookie.png b/server/mockup/images/cookie.png new file mode 100644 index 0000000..4e79a51 Binary files /dev/null and b/server/mockup/images/cookie.png differ diff --git a/server/mockup/images/pasta.png b/server/mockup/images/pasta.png new file mode 100644 index 0000000..1e77373 Binary files /dev/null and b/server/mockup/images/pasta.png differ diff --git a/server/mockup/images/pizza.png b/server/mockup/images/pizza.png new file mode 100644 index 0000000..446cdd2 Binary files /dev/null and b/server/mockup/images/pizza.png differ