Skip to content

Commit

Permalink
Access calypso on backend through separate url
Browse files Browse the repository at this point in the history
  • Loading branch information
foodelevator committed Aug 4, 2024
1 parent 942d2c2 commit 0c15cf9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ NOTE: when building with podman, you may need to specify `--ulimit nofile=65535:
|--------------------------------------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------|
| TAITAN_URL | https://taitan.datasektionen.se | URL to taitan from the backend. |
| RAZZLE_TAITAN_URL | https://taitan.datasektionen.se | URL to taitan from the frontend. **Set during both build and run**. |
| RAZZLE_CALYPSO_URL | https://calypso.datasektionen.se/api | URL to get news from calypso on. **Set during both build and run**. |
| CALYPSO_URL | https://calypso.datasektionen.se/api | URL to calypso from the backend. |
| RAZZLE_CALYPSO_URL | https://calypso.datasektionen.se/api | URL to calypso from the frontend. **Set during both build and run**. |
| TAITAN_CACHE_TTL | 3600 | Time to keep content from taitan cached in seconds. Tip: Set to 0 if using local taitan & bawang-content. |
| CALYPSO_CACHE_TTL | 30 | Time to keep news from calypso cached in seconds. Tip: Set to 0 if using local calypso. |
| SLUTA_FIPPLA_MED_MINA_ENV_VARIABLER_RAZZLE__PORT | 3000 | Port to listen on |
Expand Down
5 changes: 5 additions & 0 deletions job.nomad.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ job "bawang" {
template {
data = <<ENV
TAITAN_URL=http://taitan.nomad.dsekt.internal
RAZZLE_TAITAN_URL=https://taitan.datasektionen.se
CALYPSO_URL=http://calypso.nomad.dsekt.internal
RAZZLE_CALYPSO_URL=https://calypso.datasektionen.se/api
SLUTA_FIPPLA_MED_MINA_ENV_VARIABLER_RAZZLE__PORT={{ env "NOMAD_PORT_http" }}
ENV
destination = "local/.env"
Expand Down
18 changes: 12 additions & 6 deletions src/components/Calypso.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import React, { useState } from 'react';

import { DataLoader } from './DataLoader';
import { CALYPSO_URL, FRONTEND_CALYPSO_URL } from '../utility/env';

const RAZZLE_CALYPSO_URL = process.env.RAZZLE_CALYPSO_URL || 'https://calypso.datasektionen.se/api'
const CALYPSO_CACHE_TTL = process.env.CALYPSO_CACHE_TTL ? parseInt(process.env.CALYPSO_CACHE_TTL, 10) : 30
const CALYPSO_CACHE_TTL = process.env.CALYPSO_CACHE_TTL ? parseInt(process.env.CALYPSO_CACHE_TTL, 10) : 30;

const calypsoFetcher = url =>
fetch(url)
function calypsoFetcher(url) {
// We're using the frontend url as the cache key when running on the backend as well, even though
// we're not fetching using it, since the cache created by the backend is sent to the frontend.
const url = cacheKey.startsWith(FRONTEND_CALYPSO_URL)
? cacheKey.replace(FRONTEND_CALYPSO_URL, CALYPSO_URL)
: cacheKey;
return fetch(url)
.then(res => res.json())
.catch(err => {
console.error('Calypso error', err)
});
}

function iso(date) {
let str = date.toISOString();
Expand All @@ -31,10 +37,10 @@ function iso(date) {
*/
function getCalypsoUrl(type, search, timeSpan) {
if (!timeSpan || type !== 'event') {
return `${RAZZLE_CALYPSO_URL}/${type || 'list'}${search || ''}`;
return `${FRONTEND_CALYPSO_URL}/${type || 'list'}${search || ''}`;
}
const [startDate, endDate] = timeSpan;
const url = new URL(`${RAZZLE_CALYPSO_URL}/event/span`);
const url = new URL(`${FRONTEND_CALYPSO_URL}/event/span`);
url.searchParams.set('startDate', iso(startDate));
url.searchParams.set('endDate', iso(endDate));
return url.href;
Expand Down
3 changes: 3 additions & 0 deletions src/utility/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// In the server, both of these may be set, but in the client, only RAZZLE_* environment variables can be seen.
export const TAITAN_URL = process.env.TAITAN_URL || process.env.RAZZLE_TAITAN_URL || "https://taitan.datasektionen.se";
export const FRONTEND_TAITAN_URL = process.env.RAZZLE_TAITAN_URL || "https://taitan.datasektionen.se"

export const CALYPSO_URL = process.env.CALYPSO_URL || process.env.RAZZLE_CALYPSO_URL || "https://calypso.datasektionen.se/api";
export const FRONTEND_CALYPSO_URL = process.env.RAZZLE_CALYPSO_URL || "https://calypso.datasektionen.se/api";

0 comments on commit 0c15cf9

Please sign in to comment.