Skip to content

Commit

Permalink
Merge pull request #18 from dddwa/fix/cors-for-app-sessionize
Browse files Browse the repository at this point in the history
Add cors and announcement route
  • Loading branch information
JakeGinnivan authored Nov 6, 2024
2 parents 6096ed6 + fe4471e commit c14bbd1
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
# TODO Replace this PAT with a GitHub App
GH_CONTENT_TOKEN: ${{ secrets.GH_CONTENT_TOKEN }}
GOOGLE_FORMS_API_KEY: ${{ secrets.GOOGLE_FORMS_API_KEY }}
GOOGLE_FORMS_FORM_ID: ${{ secrets.GOOGLE_FORMS_FORM_ID }}

steps:
- name: Checkout
Expand Down
20 changes: 20 additions & 0 deletions infra/app/ddd.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ param gitHubOrganization string
param gitHubRepo string
@secure()
param gitHubToken string
@secure()
param googleFormsApiKey string
@secure()
param googleFormsFileId string
param exists bool

resource identity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
Expand Down Expand Up @@ -83,6 +87,14 @@ resource app 'Microsoft.App/containerApps@2024-03-01' = {
name: 'session-secret'
value: sessionSecret
}
{
name: 'google-forms-api-key'
value: googleFormsApiKey
}
{
name: 'google-forms-file-id'
value: googleFormsFileId
}
]
}
template: {
Expand Down Expand Up @@ -115,6 +127,14 @@ resource app 'Microsoft.App/containerApps@2024-03-01' = {
name: 'SESSION_SECRET'
secretRef: 'session-secret'
}
{
name: 'GOOGLE_FORMS_API_KEY'
secretRef: 'google-forms-api-key'
}
{
name: 'GOOGLE_FORMS_FILE_ID'
secretRef: 'google-forms-file-id'
}
]

resources: {
Expand Down
7 changes: 7 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ param gitHubRepo string
@secure()
param gitHubToken string

@secure()
param googleFormsApiKey string
@secure()
param googleFormsFileId string

// Tags that should be applied to all resources.
//
// Note that 'azd-service-name' tags should be applied separately to service host resources.
Expand Down Expand Up @@ -111,6 +116,8 @@ module ddd './app/ddd.bicep' = {
gitHubOrganization: gitHubOrganization
gitHubRepo: gitHubRepo
gitHubToken: gitHubToken
googleFormsApiKey: googleFormsApiKey
googleFormsFileId: googleFormsFileId
}
scope: rg
}
Expand Down
7 changes: 5 additions & 2 deletions infra/main.parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
"gitHubRepo": {
"value": "dddperth"
},
"gitHubToken": {
"value": "${GH_CONTENT_TOKEN}"
"googleFormsApiKey": {
"value": "${GOOGLE_FORMS_API_KEY}"
},
"googleFormsFormId": {
"value": "${GOOGLE_FORMS_FORM_ID}"
}
}
}
5 changes: 5 additions & 0 deletions website/app/lib/http.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const CACHE_CONTROL = {
* back button
*/
DEFAULT: 'max-age=300',

/**
* Add 1 minutes cache control for announcements
*/
announce: 'max-age=60',
/**
* Keep it in the browser (and CDN) for 5 minutes so when they click
* back/forward/etc. it's super fast. SWR for 1 week on CDN so it stays fast,
Expand Down
7 changes: 6 additions & 1 deletion website/app/routes/app-agenda-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export async function loader({ context }: LoaderFunctionArgs) {

const schedule = schedules[0]

return json(schedule, { headers: { 'Cache-Control': CACHE_CONTROL.conf } })
return json(schedule, {
headers: {
'Cache-Control': CACHE_CONTROL.conf,
'Access-Control-Allow-Origin': '*',
},
})
}
export const headers: HeadersFunction = ({ loaderHeaders }) => {
// Inherit the caching headers from the loader so we don't cache 404s
Expand Down
7 changes: 6 additions & 1 deletion website/app/routes/app-agenda-sessions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export async function loader({ context }: LoaderFunctionArgs) {
})
: []

return json(sessions, { headers: { 'Cache-Control': CACHE_CONTROL.conf } })
return json(sessions, {
headers: {
'Cache-Control': CACHE_CONTROL.conf,
'Access-Control-Allow-Origin': '*',
},
})
}

export const headers: HeadersFunction = ({ loaderHeaders }) => {
Expand Down
7 changes: 6 additions & 1 deletion website/app/routes/app-agenda-speakers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export async function loader({ context }: LoaderFunctionArgs) {
})
: []

return json(speakers, { headers: { 'Cache-Control': CACHE_CONTROL.conf } })
return json(speakers, {
headers: {
'Cache-Control': CACHE_CONTROL.conf,
'Access-Control-Allow-Origin': '*',
},
})
}

export const headers: HeadersFunction = ({ loaderHeaders }) => {
Expand Down
42 changes: 42 additions & 0 deletions website/app/routes/app-announcements.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { json } from '@remix-run/server-runtime'
import { CACHE_CONTROL } from '~/lib/http.server'

export type GoogleFormUpdates = {
Timestamp: string
Message: string
}

/** This route is used by the app for on the day announcements */
export async function loader() {
const apiKey = process.env.GOOGLE_FORMS_API_KEY
const fileId = process.env.GOOGLE_FORMS_FORM_ID
if (!apiKey || !fileId) {
return new Response(JSON.stringify({ message: 'No Google Forms API key or form ID' }), { status: 404 })
}

const BASE_URL = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media&key=${apiKey}`

const response = await fetch(BASE_URL, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
})

const responseData = (await response.json()) as GoogleFormUpdates[]

const announcementData = responseData
.map((row) => {
return { createdTime: row.Timestamp, update: row.Message }
})
.sort((a, b) => {
return new Date(b.createdTime).getTime() - new Date(a.createdTime).getTime()
})

return json(announcementData, {
headers: {
'Cache-Control': CACHE_CONTROL.announce,
'Access-Control-Allow-Origin': '*',
},
})
}
2 changes: 2 additions & 0 deletions website/app/routes/app-config.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { json, LoaderFunctionArgs } from '@remix-run/server-runtime'
import { YearSponsors } from '~/lib/config-types'
import { CACHE_CONTROL } from '~/lib/http.server'

/** This route is used by the app or integrations to understand the state of the conference */
export function loader({ context }: LoaderFunctionArgs) {
Expand All @@ -18,6 +19,7 @@ export function loader({ context }: LoaderFunctionArgs) {

return json(data, {
headers: {
'Cache-Control': CACHE_CONTROL.doc,
'Access-Control-Allow-Origin': '*',
},
})
Expand Down
7 changes: 6 additions & 1 deletion website/app/routes/app-content.$.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ export async function loader({ params, request, context }: LoaderFunctionArgs) {
post: post.code,
conferenceState: context.conferenceState,
},
{ headers: { 'Cache-Control': CACHE_CONTROL.doc, 'Access-Control-Allow-Origin': '*' } },
{
headers: {
'Cache-Control': CACHE_CONTROL.doc,
'Access-Control-Allow-Origin': '*',
},
},
)
}

Expand Down

0 comments on commit c14bbd1

Please sign in to comment.