feat: added image loading and storage to server and client
Gitea Action for testing api server / run-ci-server (push) Failing after 2m20s Details

This commit is contained in:
Sven Vogel 2024-10-28 16:29:59 +01:00
parent 002506789f
commit 2e26078425
7 changed files with 29 additions and 1 deletions

View File

@ -89,7 +89,10 @@ class _MyHomePageState extends State<MyHomePage> {
List<TableRow> 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);

View File

@ -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

View File

@ -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))
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 MiB