From a33fd3a3f9ed02a2cbc273108ebee4cacd91e950 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Thu, 9 Jun 2022 21:39:28 -0700 Subject: [PATCH] adding cors for to all endpoints --- net/server/go.mod | 2 ++ net/server/go.sum | 4 ++++ net/server/internal/api_getAccountListing.go | 2 -- net/server/main.go | 6 +++++- net/web/src/api/getContactChannelDetail.js | 4 ++-- net/web/src/api/getContactChannelSummary.js | 4 ++-- net/web/src/api/getContactChannels.js | 4 ++-- net/web/src/context/useCardContext.hook.js | 18 +++++++++--------- 8 files changed, 26 insertions(+), 18 deletions(-) diff --git a/net/server/go.mod b/net/server/go.mod index ad847365..2422496a 100644 --- a/net/server/go.mod +++ b/net/server/go.mod @@ -4,7 +4,9 @@ go 1.17 require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/felixge/httpsnoop v1.0.1 // indirect github.com/google/uuid v1.3.0 // indirect + github.com/gorilla/handlers v1.5.1 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect diff --git a/net/server/go.sum b/net/server/go.sum index 68c1409e..c4207304 100644 --- a/net/server/go.sum +++ b/net/server/go.sum @@ -2,8 +2,12 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= +github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= +github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= diff --git a/net/server/internal/api_getAccountListing.go b/net/server/internal/api_getAccountListing.go index 7659cc77..f54eaf8a 100644 --- a/net/server/internal/api_getAccountListing.go +++ b/net/server/internal/api_getAccountListing.go @@ -7,8 +7,6 @@ import ( func GetAccountListing(w http.ResponseWriter, r *http.Request) { - EnableCors(&w); - var accounts []store.Account if err := store.DB.Preload("AccountDetail").Where("searchable = ? AND disabled = ?", true, false).Find(&accounts).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) diff --git a/net/server/main.go b/net/server/main.go index d1dda053..3c504b93 100644 --- a/net/server/main.go +++ b/net/server/main.go @@ -5,6 +5,7 @@ import ( "net/http" app "databag/internal" "databag/internal/store" + "github.com/gorilla/handlers" ) func main() { @@ -15,5 +16,8 @@ func main() { router := app.NewRouter() - log.Fatal(http.ListenAndServe(":7000", router)) + origins := handlers.AllowedOrigins([]string{"*"}) + methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}) + + log.Fatal(http.ListenAndServe(":7000", handlers.CORS(origins, methods)(router))) } diff --git a/net/web/src/api/getContactChannelDetail.js b/net/web/src/api/getContactChannelDetail.js index 69f3eae5..4d2e1a17 100644 --- a/net/web/src/api/getContactChannelDetail.js +++ b/net/web/src/api/getContactChannelDetail.js @@ -1,7 +1,7 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; -export async function getContactChannelDetail(token, channelId) { - let detail = await fetchWithTimeout(`/content/channels/${channelId}/detail?contact=${token}`, { method: 'GET' }); +export async function getContactChannelDetail(server, token, channelId) { + let detail = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/detail?contact=${token}`, { method: 'GET' }); checkResponse(detail) return await detail.json() } diff --git a/net/web/src/api/getContactChannelSummary.js b/net/web/src/api/getContactChannelSummary.js index ccc73e74..84221417 100644 --- a/net/web/src/api/getContactChannelSummary.js +++ b/net/web/src/api/getContactChannelSummary.js @@ -1,7 +1,7 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; -export async function getContactChannelSummary(token, channelId) { - let summary = await fetchWithTimeout(`/content/channels/${channelId}/summary?contact=${token}`, { method: 'GET' }); +export async function getContactChannelSummary(server, token, channelId) { + let summary = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/summary?contact=${token}`, { method: 'GET' }); checkResponse(summary) return await summary.json() } diff --git a/net/web/src/api/getContactChannels.js b/net/web/src/api/getContactChannels.js index d5c91450..3edd9b1c 100644 --- a/net/web/src/api/getContactChannels.js +++ b/net/web/src/api/getContactChannels.js @@ -1,6 +1,6 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; -export async function getContactChannels(token, viewRevision, channelRevision) { +export async function getContactChannels(server, token, viewRevision, channelRevision) { let param = "?contact=" + token if (viewRevision != null) { param += '&viewRevision=' + viewRevision @@ -8,7 +8,7 @@ export async function getContactChannels(token, viewRevision, channelRevision) { if (channelRevision != null) { param += '&channelRevision=' + channelRevision } - let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' }); + let channels = await fetchWithTimeout(`https://${server}/content/channels${param}`, { method: 'GET' }); checkResponse(channels) return await channels.json() } diff --git a/net/web/src/context/useCardContext.hook.js b/net/web/src/context/useCardContext.hook.js index c196f07c..8fc9f97d 100644 --- a/net/web/src/context/useCardContext.hook.js +++ b/net/web/src/context/useCardContext.hook.js @@ -77,8 +77,8 @@ export function useCardContext() { cur.data.articles = new Map(); cur.channels = new Map(); - await updateContactChannels(card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedChannel, cur.channels); - await updateContactArticles(card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedArticle, cur.data.articles); + await updateContactChannels(cur.data.cardProfile.node, card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedChannel, cur.channels); + await updateContactArticles(cur.data.cardProfile.node, card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedArticle, cur.data.articles); // update view cur.data.notifiedArticle = card.data.notifiedArticle; @@ -87,12 +87,12 @@ export function useCardContext() { } if (cur.data.notifiedArticle != card.data.notifiedArticle) { // update remote articles - await updateContactArticles(card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedArticle, cur.data.articles); + await updateContactArticles(cur.data.cardProfile.node, card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedArticle, cur.data.articles); cur.data.notifiedArticle = card.data.notifiedArticle; } if (cur.data.notifiedChannel != card.data.notifiedChannel) { // update remote channels - await updateContactChannels(card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedChannel, cur.channels); + await updateContactChannels(cur.data.cardProfile.node, card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedChannel, cur.channels); cur.data.notifiedChannel = card.data.notifiedChannel; } } @@ -109,8 +109,8 @@ export function useCardContext() { } } - const updateContactChannels = async (cardId, guid, token, viewRevision, channelRevision, channelMap) => { - let delta = await getContactChannels(guid + "." + token, viewRevision, channelRevision); + const updateContactChannels = async (node, cardId, guid, token, viewRevision, channelRevision, channelMap) => { + let delta = await getContactChannels(node, guid + "." + token, viewRevision, channelRevision); for (let channel of delta) { if (channel.data) { let cur = channelMap.get(channel.id); @@ -122,7 +122,7 @@ export function useCardContext() { cur.data.channelDetail = channel.data.channelDetail; } else { - let detail = await getContactChannelDetail(guid + "." + token, channel.id); + let detail = await getContactChannelDetail(node, guid + "." + token, channel.id); cur.data.channelDetail = detail; } cur.data.detailRevision = channel.data.detailRevision; @@ -132,7 +132,7 @@ export function useCardContext() { cur.data.channelSummary = channel.data.channelSummary; } else { - let summary = await getContactChannelSummary(guid + "." + token, channel.id); + let summary = await getContactChannelSummary(node, guid + "." + token, channel.id); cur.data.channelSummary = summary; } cur.data.topicRevision = channel.data.topicRevision; @@ -146,7 +146,7 @@ export function useCardContext() { } } - const updateContactArticles = async (cardId, guid, token, viewRevision, articleRevision, articleMap) => { + const updateContactArticles = async (node, cardId, guid, token, viewRevision, articleRevision, articleMap) => { console.log("update contact articles"); }