From ba032828ada06c5b73e26c27e329da07868d414f Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Mon, 20 Jul 2020 13:56:13 +0200 Subject: [PATCH] enable CORS --- frontend/src/CoffeeList.jsx | 60 ++++++++++++++++--------------------- go.mod | 1 + go.sum | 2 ++ main.go | 5 +++- server/server.go | 8 +++-- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/frontend/src/CoffeeList.jsx b/frontend/src/CoffeeList.jsx index 76a6054..41e4b5d 100644 --- a/frontend/src/CoffeeList.jsx +++ b/frontend/src/CoffeeList.jsx @@ -1,44 +1,32 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; import Table from "react-bootstrap/Table"; import axios from "axios"; -import { useState } from "react"; -import { useEffect } from "react"; + +const Products = ({ products }) => + products.map((product) => ( + + {product.name} + {product.price} + {product.sku} + + )); const CoffeeList = () => { - const [state, setState] = useState({ products: [] }); + const [products, setProducts] = useState([]); useEffect(() => { + const readData = () => { + axios + .get(window.global.api_location + "/products") + .then((response) => { + setProducts(response.data); + }) + .catch((error) => { + console.error(error); + }); + }; readData(); - }); - - const readData = () => { - axios - .get(window.global.api_location + "/products") - .then((response) => { - console.log(response.data); - - setState({ products: response.data }); - }) - .catch((error) => { - console.log(error); - }); - }; - - const getProducts = () => { - let table = []; - - for (let i = 0; i < state.products.length; i++) { - table.push( - - {state.products[i].name} - {state.products[i].price} - {state.products[i].sku} - - ); - } - - return table; - }; + }, []); return (
@@ -51,7 +39,9 @@ const CoffeeList = () => { SKU - {getProducts()} + + +
); diff --git a/go.mod b/go.mod index cd721bb..984c9e6 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/go-openapi/validate v0.19.10 github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/validator v9.31.0+incompatible + github.com/gorilla/handlers v1.4.2 github.com/gorilla/mux v1.7.4 github.com/leodido/go-urn v1.2.0 // indirect go.mongodb.org/mongo-driver v1.3.5 // indirect diff --git a/go.sum b/go.sum index fd016c7..0c3525e 100644 --- a/go.sum +++ b/go.sum @@ -121,6 +121,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg= +github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= diff --git a/main.go b/main.go index 926b15b..5dbc72e 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,9 @@ import ( const port = ":5000" const productPath = "/products" +// allowed consumers. Use "*" for public API only. +var origins = []string{"http://localhost:3000"} + func main() { // create a logger to control application wide logging logger := log.New(os.Stdout, "Product API: ", log.LstdFlags|log.Lshortfile) @@ -30,7 +33,7 @@ func main() { productsHandler.RegisterRoutes(router, productPath) // creates a production-ready server using the handler - srv := server.New(router, port) + srv := server.New(router, port, origins) // start a non blocking application server go func() { diff --git a/server/server.go b/server/server.go index e73187c..50ba82e 100644 --- a/server/server.go +++ b/server/server.go @@ -3,13 +3,17 @@ package server import ( "net/http" "time" + + gohandlers "github.com/gorilla/handlers" ) // New creates a server using given mux and port -func New(mux http.Handler, port string) *http.Server { +func New(mux http.Handler, port string, origins []string) *http.Server { + // CORS + corsHandler := gohandlers.CORS(gohandlers.AllowedOrigins(origins)) return &http.Server{ Addr: port, - Handler: mux, + Handler: corsHandler(mux), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, // keep connection opened to prevent Ddos attacks