mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-24 20:34:55 +00:00
Test out API requests from frontend to backend
This commit is contained in:
parent
53cbe6e26c
commit
8416549858
5 changed files with 87 additions and 6 deletions
|
@ -1,6 +1,8 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/Fluffy-Bean/TastyBites/front"
|
"github.com/Fluffy-Bean/TastyBites/front"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
@ -16,11 +18,17 @@ func Serve(c Config) {
|
||||||
|
|
||||||
if c.Logging {
|
if c.Logging {
|
||||||
r.Use(middleware.Logger())
|
r.Use(middleware.Logger())
|
||||||
|
r.Use(middleware.CORS())
|
||||||
}
|
}
|
||||||
r.Use(middleware.Recover())
|
r.Use(middleware.Recover())
|
||||||
|
|
||||||
r.StaticFS("/", front.DistDir)
|
r.StaticFS("/", front.DistDir)
|
||||||
|
|
||||||
|
api := r.Group("/api")
|
||||||
|
api.GET("/items", func(e echo.Context) error {
|
||||||
|
return e.JSON(http.StatusOK, sampleData)
|
||||||
|
})
|
||||||
|
|
||||||
r.HideBanner = true
|
r.HideBanner = true
|
||||||
r.Logger.Fatal(r.Start(c.Host))
|
r.Logger.Fatal(r.Start(c.Host))
|
||||||
}
|
}
|
||||||
|
|
59
api/testData.go
Normal file
59
api/testData.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
type menuItem struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Price float32 `json:"price"`
|
||||||
|
Labels []string `json:"labels"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var sampleData = []menuItem{
|
||||||
|
{
|
||||||
|
Name: "Bar of Soap",
|
||||||
|
Price: 69.99,
|
||||||
|
Labels: []string{"vegan", "spicy"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Sock",
|
||||||
|
Price: 21,
|
||||||
|
Labels: []string{"vegan", "fish", "nut", "spicy"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Brick",
|
||||||
|
Price: 0,
|
||||||
|
Labels: []string{"spicy"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Toast",
|
||||||
|
Price: 4382749832743,
|
||||||
|
Labels: []string{"gluten"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "water",
|
||||||
|
Price: 1,
|
||||||
|
Labels: []string{"fish"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "half eaten mouldy bread",
|
||||||
|
Price: -9999,
|
||||||
|
Labels: []string{"nut"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "GwaGwa",
|
||||||
|
Price: 69,
|
||||||
|
Labels: []string{"nut"},
|
||||||
|
Image: "/dab.jpg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Hogermellon",
|
||||||
|
Price: 1111,
|
||||||
|
Labels: []string{"fish"},
|
||||||
|
Image: "/wathog.jpg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Blue HOGGGGG",
|
||||||
|
Price: 0,
|
||||||
|
Labels: []string{"nut", "gluten", "spicy"},
|
||||||
|
Image: "/sonichog.jpg",
|
||||||
|
},
|
||||||
|
}
|
|
@ -1,7 +1,14 @@
|
||||||
import Items from '%/lib/testData.js';
|
import Items from '%/lib/testData.js';
|
||||||
|
|
||||||
export function getPopularToday() {
|
export async function getPopularToday() {
|
||||||
return Items;
|
const res = await fetch("/api/items")
|
||||||
|
const data = res.json()
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
return data
|
||||||
|
} else {
|
||||||
|
throw new Error("Failed to fetch popular today")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMenuItems() {
|
export function getMenuItems() {
|
|
@ -3,7 +3,7 @@
|
||||||
import { link } from 'svelte-spa-router';
|
import { link } from 'svelte-spa-router';
|
||||||
import { ArrowUpRight } from "phosphor-svelte";
|
import { ArrowUpRight } from "phosphor-svelte";
|
||||||
import { map, tileLayer, marker } from 'leaflet';
|
import { map, tileLayer, marker } from 'leaflet';
|
||||||
import { getPopularToday } from "%/lib/APIDEV.js";
|
import { getPopularToday } from "%/lib/api.js";
|
||||||
import AnnouncementBanner from "%/pages/elements/AnnouncementBanner.svelte";
|
import AnnouncementBanner from "%/pages/elements/AnnouncementBanner.svelte";
|
||||||
import MenuList from "%/pages/elements/MenuList.svelte";
|
import MenuList from "%/pages/elements/MenuList.svelte";
|
||||||
|
|
||||||
|
@ -18,7 +18,8 @@
|
||||||
{maxZoom: 19, attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'}
|
{maxZoom: 19, attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'}
|
||||||
).addTo(Map);
|
).addTo(Map);
|
||||||
marker([50.82304922105467, -0.432780150496344]).addTo(Map);
|
marker([50.82304922105467, -0.432780150496344]).addTo(Map);
|
||||||
})
|
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
|
@ -86,7 +87,13 @@
|
||||||
<div class="spacer" />
|
<div class="spacer" />
|
||||||
|
|
||||||
<h2>Popular Today</h2>
|
<h2>Popular Today</h2>
|
||||||
<MenuList {items} />
|
{#await items}
|
||||||
|
<p>Loading...</p>
|
||||||
|
{:then items}
|
||||||
|
<MenuList {items} />
|
||||||
|
{:catch error}
|
||||||
|
<p>Failed to get todays specials!</p>
|
||||||
|
{/await}
|
||||||
<a href="/menu" use:link>See All <ArrowUpRight /></a>
|
<a href="/menu" use:link>See All <ArrowUpRight /></a>
|
||||||
<div class="spacer" />
|
<div class="spacer" />
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
import { ArrowClockwise } from "phosphor-svelte";
|
import { ArrowClockwise } from "phosphor-svelte";
|
||||||
import MenuList from "%/pages/elements/MenuList.svelte";
|
import MenuList from "%/pages/elements/MenuList.svelte";
|
||||||
import DropDown from "%/components/DropDown.svelte";
|
import DropDown from "%/components/DropDown.svelte";
|
||||||
import { getMenuItems } from "%/lib/APIDEV.js";
|
import { getMenuItems } from "%/lib/api.js";
|
||||||
|
|
||||||
let items = getMenuItems();
|
let items = getMenuItems();
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue