Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Gatsby functions in preview deploys #4919

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ ETHERSCAN_API_KEY=insertValue
GATSBY_ALGOLIA_APP_ID=insertValue
GATSBY_ALGOLIA_SEARCH_KEY=insertValue
GATSBY_GITHUB_TOKEN_READ_ONLY=insertValue
GATSBY_FUNCTIONS_PATH=insertValue

# Build pages only for the specified langs. Leave it empty to build all the langs
# e.g. `en,fr` will only build english and french pages
# Note: always include `en` as it is the default lang of the site
GATSBY_BUILD_LANGS=

# Folders or files to ignore from the `src/content` folder
IGNORE_CONTENT=**/docs,**/tutorials
IGNORE_CONTENT=**/docs,**/tutorials
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dotenv": "^8.2.0",
"ethereum-blockies-base64": "^1.0.2",
"framer-motion": "^4.1.3",
"gatsby": "^4.0.0",
"gatsby": "^4.6.0",
"gatsby-plugin-gatsby-cloud": "^4.3.0",
"gatsby-plugin-image": "^2.0.0",
"gatsby-plugin-intl": "^0.3.3",
Expand Down
8 changes: 8 additions & 0 deletions src/api/coinmetrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { lambda } from "../lambda/coinmetrics"

async function handler(__req, res) {
const { statusCode, body } = await lambda()
res.status(statusCode).send(body)
}

export default handler
10 changes: 10 additions & 0 deletions src/api/defipulse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lambda } from "../lambda/defipulse"

async function handler(__req, res) {
// passing env vars as arguments due to a bug on GC functions where env vars
// can not be accessed by imported functions
const { statusCode, body } = await lambda(process.env.DEFI_PULSE_API_KEY)
res.status(statusCode).send(body)
}

export default handler
10 changes: 10 additions & 0 deletions src/api/etherscan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lambda } from "../lambda/etherscan"

async function handler(__req, res) {
// passing env vars as arguments due to a bug on GC functions where env vars
// can not be accessed by imported functions
const { statusCode, body } = await lambda(process.env.ETHERSCAN_API_KEY)
res.status(statusCode).send(body)
}

export default handler
10 changes: 10 additions & 0 deletions src/api/etherscanBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lambda } from "../lambda/etherscanBlock"

async function handler(__req, res) {
// passing env vars as arguments due to a bug on GC functions where env vars
// can not be accessed by imported functions
const { statusCode, body } = await lambda(process.env.ETHERSCAN_API_KEY)
res.status(statusCode).send(body)
}

export default handler
10 changes: 10 additions & 0 deletions src/api/roadmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lambda } from "../lambda/roadmap"

async function handler(__req, res) {
// passing env vars as arguments due to a bug on GC functions where env vars
// can not be accessed by imported functions
const { statusCode, body } = await lambda(process.env.GITHUB_TOKEN)
res.status(statusCode).send(body)
}

export default handler
10 changes: 10 additions & 0 deletions src/api/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lambda } from "../lambda/translations"

async function handler(__req, res) {
// passing env vars as arguments due to a bug on GC functions where env vars
// can not be accessed by imported functions
const { statusCode, body } = await lambda(process.env.CROWDIN_API_KEY)
res.status(statusCode).send(body)
}

export default handler
10 changes: 10 additions & 0 deletions src/api/txs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lambda } from "../lambda/txs"

async function handler(__req, res) {
// passing env vars as arguments due to a bug on GC functions where env vars
// can not be accessed by imported functions
const { statusCode, body } = await lambda(process.env.ETHERSCAN_API_KEY)
res.status(statusCode).send(body)
}

export default handler
2 changes: 1 addition & 1 deletion src/components/ReleaseBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ReleaseBanner = ({ storageKey }) => {
try {
const data = await getFreshData(
process.env.NODE_ENV === "production"
? "/.netlify/functions/etherscanBlock"
? `${process.env.GATSBY_FUNCTIONS_PATH}/etherscanBlock`
: "http://localhost:9000/etherscanBlock"
)
setTimeLeft(data)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Roadmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const Roadmap = () => {
axios
.get(
process.env.NODE_ENV === "production"
? "/.netlify/functions/roadmap"
? `${process.env.GATSBY_FUNCTIONS_PATH}/roadmap`
: "http://localhost:9000/roadmap"
)
.then((response) => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/StatsBoxGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ const StatsBoxGrid = () => {
try {
const { result } = await getData(
process.env.NODE_ENV === "production"
? "/.netlify/functions/etherscan"
? `${process.env.GATSBY_FUNCTIONS_PATH}/etherscan`
: "http://localhost:9000/etherscan"
)
const data = result
Expand Down Expand Up @@ -368,7 +368,7 @@ const StatsBoxGrid = () => {
try {
const response = await getData(
process.env.NODE_ENV === "production"
? "/.netlify/functions/defipulse"
? `${process.env.GATSBY_FUNCTIONS_PATH}/defipulse`
: "http://localhost:9000/defipulse"
)
const data = response
Expand Down Expand Up @@ -397,7 +397,7 @@ const StatsBoxGrid = () => {
try {
const response = await getData(
process.env.NODE_ENV === "production"
? "/.netlify/functions/txs"
? `${process.env.GATSBY_FUNCTIONS_PATH}/txs`
: "http://localhost:9000/txs"
)
const data = response.result
Expand Down
2 changes: 1 addition & 1 deletion src/components/TranslationsInProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TranslationsInProgress = () => {
axios
.get(
process.env.NODE_ENV === "production"
? "/.netlify/functions/translations"
? `${process.env.GATSBY_FUNCTIONS_PATH}/translations`
: "http://localhost:9000/translations"
)
.then((response) => {
Expand Down
8 changes: 6 additions & 2 deletions src/lambda/coinmetrics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const axios = require("axios")

const handler = async () => {
const lambda = async () => {
try {
const response = await axios.get(
"https://community-api.coinmetrics.io/v2/assets/eth/metricdata/?metrics=TxCnt"
Expand All @@ -16,4 +16,8 @@ const handler = async () => {
}
}

module.exports = { handler }
const handler = () => {
return lambda()
}

module.exports = { handler, lambda }
10 changes: 7 additions & 3 deletions src/lambda/defipulse.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const axios = require("axios")

const handler = async () => {
const lambda = async (apiKey) => {
try {
const response = await axios.get(
`https://data-api.defipulse.com/api/v1/defipulse/api/GetHistory?api-key=${process.env.DEFI_PULSE_API_KEY}&period=3m&length=1`
`https://data-api.defipulse.com/api/v1/defipulse/api/GetHistory?api-key=${apiKey}&period=3m&length=1`
)
if (response.status < 200 || response.status >= 300) {
return {
Expand All @@ -19,4 +19,8 @@ const handler = async () => {
}
}

module.exports = { handler }
const handler = () => {
return lambda(process.env.DEFI_PULSE_API_KEY)
}

module.exports = { handler, lambda }
10 changes: 7 additions & 3 deletions src/lambda/etherscan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const axios = require("axios")

const handler = async () => {
const lambda = async (apiKey) => {
const daysToFetch = 90
const now = new Date()
const endDate = now.toISOString().split("T")[0] // YYYY-MM-DD
Expand All @@ -9,7 +9,7 @@ const handler = async () => {
.split("T")[0] // {daysToFetch} days ago
try {
const response = await axios.get(
`https://api.etherscan.io/api?module=stats&action=nodecounthistory&startdate=${startDate}&enddate=${endDate}&sort=desc&apikey=${process.env.ETHERSCAN_API_KEY}`
`https://api.etherscan.io/api?module=stats&action=nodecounthistory&startdate=${startDate}&enddate=${endDate}&sort=desc&apikey=${apiKey}`
)
if (response.status < 200 || response.status >= 300) {
return { statusCode: response.status, body: response.statusText }
Expand All @@ -22,4 +22,8 @@ const handler = async () => {
}
}

module.exports = { handler }
const handler = () => {
return lambda(process.env.ETHERSCAN_API_KEY)
}

module.exports = { handler, lambda }
10 changes: 7 additions & 3 deletions src/lambda/etherscanBlock.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const axios = require("axios")

const handler = async () => {
const lambda = async (apiKey) => {
try {
const response = await axios.get(
`https://api.etherscan.io/api?module=block&action=getblockcountdown&blockno=12965000&apikey=${process.env.ETHERSCAN_API_KEY}`
`https://api.etherscan.io/api?module=block&action=getblockcountdown&blockno=12965000&apikey=${apiKey}`
)
if (response.status < 200 || response.status >= 300) {
return { statusCode: response.status, body: response.statusText }
Expand All @@ -20,4 +20,8 @@ const handler = async () => {
}
}

module.exports = { handler }
const handler = () => {
return lambda(process.env.ETHERSCAN_API_KEY)
}

module.exports = { handler, lambda }
11 changes: 8 additions & 3 deletions src/lambda/roadmap.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const axios = require("axios")

exports.handler = async function (event, context) {
const lambda = async function (githubToken) {
try {
const baseURL =
"https://api.github.com/repos/ethereum/ethereum-org-website/issues?per_page=100&state=all"
const { GITHUB_TOKEN } = process.env

const resp = await axios.get(`${baseURL}`, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
Authorization: `token ${githubToken}`,
},
})

Expand All @@ -29,3 +28,9 @@ exports.handler = async function (event, context) {
}
}
}

const handler = () => {
return lambda(process.env.GITHUB_TOKEN)
}

module.exports = { handler, lambda }
11 changes: 8 additions & 3 deletions src/lambda/translations.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const axios = require("axios")

exports.handler = async function (event, context) {
const lambda = async function (apiKey) {
try {
const baseURL = "https://api.crowdin.com/api/project/ethereum-org/status"
const { CROWDIN_API_KEY } = process.env

const resp = await axios.get(`${baseURL}?key=${CROWDIN_API_KEY}&json`)
const resp = await axios.get(`${baseURL}?key=${apiKey}&json`)

if (resp.status < 200 || resp.status >= 300) {
return { statusCode: resp.status, body: resp.statusText }
Expand All @@ -24,3 +23,9 @@ exports.handler = async function (event, context) {
}
}
}

const handler = () => {
return lambda(process.env.CROWDIN_API_KEY)
}

module.exports = { handler, lambda }
10 changes: 7 additions & 3 deletions src/lambda/txs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const axios = require("axios")

const handler = async () => {
const lambda = async (apiKey) => {
try {
const daysToFetch = 90
const now = new Date()
Expand All @@ -9,7 +9,7 @@ const handler = async () => {
.toISOString()
.split("T")[0] // {daysToFetch} days ago
const response = await axios.get(
`https://api.etherscan.io/api?module=stats&action=dailytx&startdate=${startDate}&enddate=${endDate}&sort=asc&apikey=${process.env.ETHERSCAN_API_KEY}`
`https://api.etherscan.io/api?module=stats&action=dailytx&startdate=${startDate}&enddate=${endDate}&sort=asc&apikey=${apiKey}`
)
if (response.status < 200 || response.status >= 300) {
return { statusCode: response.status, body: response.statusText }
Expand All @@ -25,4 +25,8 @@ const handler = async () => {
}
}

module.exports = { handler }
const handler = () => {
return lambda(process.env.ETHERSCAN_API_KEY)
}

module.exports = { handler, lambda }
Loading