adding cors for to all endpoints

This commit is contained in:
Roland Osborne 2022-06-09 21:39:28 -07:00
parent 1ea1b73fc0
commit a33fd3a3f9
8 changed files with 26 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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