Skip to content

Commit

Permalink
feat(gitpod env): open strapi port publicly, set STRAPI_URL env param…
Browse files Browse the repository at this point in the history
…, add .env.example to scripts folder
  • Loading branch information
jtfairbank committed Jul 25, 2024
1 parent d19697b commit 1234f24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

ports:
- name: Strapi
port: 1337
onOpen: open-browser
visibility: public

tasks:
- name: dev server
init: yarn install && yarn run build
command: yarn run develop

- name: bash
init: bash
command: export STRAPI_URL=$(gp url 1337)
2 changes: 2 additions & 0 deletions src/scripts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# strapi
STRAPI_API_KEY= # set in admin panel: /admin/settings/api-tokens/
20 changes: 7 additions & 13 deletions src/scripts/uploadData.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import 'dotenv/config'
import { config } from 'dotenv';
config({ debug: true });
import fetch from 'node-fetch'
import { readFileSync } from 'fs'

const strapiURL = 'https://1337-distributea-aggregatedp-hvms4i4hj5d.ws-us115.gitpod.io/api/categories';
const strapiURL = `${process.env.STRAPI_URL}/api/categories`;
const apiKey = process.env.STRAPI_API_KEY;

// console.log(process.env.STRAPI_API_KEY);

const jsonData = readFileSync("./needs-data.json", 'utf8');
const data = JSON.parse(jsonData);

Expand Down Expand Up @@ -52,18 +48,16 @@ const productsWithCategories = data.map((need) => {
const categories = productsWithCategories.map(product => product.category)
const uniqueCategories = Array.from(new Set(categories))

// console.log(uniqueCategories);

async function sendDataToStrapi(uniqueCategories) {
for (let categoryName of uniqueCategories) {
try {
// Check if category exists
let response = await fetch(`${strapiURL}?name=${encodeURIComponent(categoryName)}`, {
method: 'Get',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
method: "GET",
headers: {
"Content-Type": "application/json",
'Authorization': `Bearer ${apiKey}`
},
});

if (!response.ok) {
Expand Down Expand Up @@ -95,7 +89,7 @@ async function sendDataToStrapi(uniqueCategories) {
console.log(`Category "${categoryName}" already exists, skipping creation.`);
}
} catch (error) {
console.log(`Failed to process category"${categoryName}" : ${error.message}`);
console.log(`Failed to process category "${categoryName}" : ${error.message}`);
}
}
}
Expand Down

0 comments on commit 1234f24

Please sign in to comment.