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

Updating for security reason #419

Merged
merged 11 commits into from
Jul 29, 2024
37 changes: 37 additions & 0 deletions backend/config/functions/cronTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// backend/config/functions/cronTask.js
/*
* Function to fetch data from Space-Track such as Eccentricy, SMA, Inclination every month
* and update the database with the new data
*/
'use strict';

const { fetchOrbitalData } = require('./satelliteUtils');

module.exports = {
updateAllSatellitesData: {
task: async ({ strapi }) => {
try {
// Fetching all satellites
const satellites = await strapi.entityService.findMany('api::satellite.satellite');

// Waiting for all promises to be resolved
await Promise.all(
satellites.map(async satellite => {
try {
setTimeout(async () => {
await fetchOrbitalData(strapi, satellite.id);
}, 10000);
} catch (error) {
console.error(error);
}
})
);
} catch (error) {
console.error(error);
}
},
options: {
rule: "0 0 0 3 * *", // Every month on the 3rd at midnight
},
},
};
54 changes: 54 additions & 0 deletions backend/config/functions/satelliteUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// backend/utils/satelliteUtils.js
const axios = require('axios');

async function fetchOrbitalData(strapi, contextId) {
try {
// Fetching the satellite
const satellite = await strapi.entityService.findOne('api::satellite.satellite', contextId);
const noradId = satellite.catalogNumberNORAD;

// Authentication to Space-Track
const authResponse = await axios.post('https://www.space-track.org/ajaxauth/login', {
identity: 'floridg@stud.ntnu.no',
password: 'Vm5JxTtD3-hYBdq'
});

if (authResponse.status === 200) {
// Fetching data from Space-Track
const satelliteResponse = await axios.get(`https://www.space-track.org/basicspacedata/query/class/gp_history/NORAD_CAT_ID/${noradId}/orderby/TLE_LINE1%20ASC/EPOCH/1950-07-02--2024-07-02/format/json`, {
headers: {
Cookie: authResponse.headers['set-cookie']
}
});

if (satelliteResponse.status === 200) {
// Collecting data
const satelliteData = satelliteResponse.data;
const historicalOrbitalData = satelliteData.map(data => ({
epoch: data.EPOCH,
inclination: data.INCLINATION,
eccentricity: data.ECCENTRICITY,
semiMajorAxis: data.SEMIMAJOR_AXIS
}));

// Updating the satellite with the new data
const updatedSatellite = await strapi.entityService.update('api::satellite.satellite', contextId, {
data: {
historicalOrbitalData: historicalOrbitalData,
},
});
return updatedSatellite;
} else {
throw new Error('Error while fetching data from Space-Track');
}
} else {
throw new Error('Authentication failed');
}
} catch (error) {
console.error('Error while fetching data to Space-Track: ', error);
}
}

module.exports = {
fetchOrbitalData,
};
12 changes: 11 additions & 1 deletion backend/config/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ module.exports = [
'strapi::cors',
'strapi::poweredBy',
'strapi::query',
'strapi::body',
{
name: "strapi::body",
config: {
formLimit: "256mb", // modify form body
jsonLimit: "256mb", // modify JSON body
textLimit: "256mb", // modify text body
formidable: {
maxFileSize: 200 * 1024 * 1024, // multipart data, modify here limit of uploaded file size
},
},
},
'strapi::session',
'strapi::favicon',
'strapi::public',
Expand Down
7 changes: 7 additions & 0 deletions backend/config/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const cronTask = require('./functions/cronTask');

module.exports = ({ env }) => ({
host: env("HOST", "127.0.0.1"),
port: env.int("PORT", 1337),
Expand All @@ -7,4 +9,9 @@ module.exports = ({ env }) => ({
webhooks: {
populateRelations: env.bool("WEBHOOKS_POPULATE_RELATIONS", false),
},
cron: {
// Enable or disable the cron tasks
enabled: env.bool("CRON_ENABLED", true),
tasks: cronTask,
},
});
3 changes: 3 additions & 0 deletions backend/src/api/satellite/content-types/satellite/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
},
"massKg": {
"type": "float"
},
"historicalOrbitalData": {
"type": "json"
}
}
}
2 changes: 1 addition & 1 deletion backend/src/api/satellite/controllers/satellite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::satellite.satellite');
module.exports = createCoreController('api::satellite.satellite');
5 changes: 4 additions & 1 deletion backend/src/api/satellite/services/satellite.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* satellite service
*/


const { createCoreService } = require('@strapi/strapi').factories;

module.exports = createCoreService('api::satellite.satellite');
module.exports = {
...createCoreService('api::satellite.satellite')
};
1 change: 1 addition & 0 deletions backend/types/generated/contentTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ export interface ApiSatelliteSatellite extends Schema.CollectionType {
slug: Attribute.UID<'api::satellite.satellite', 'name'> &
Attribute.Required;
massKg: Attribute.Float;
historicalOrbitalData: Attribute.JSON;
createdAt: Attribute.DateTime;
updatedAt: Attribute.DateTime;
publishedAt: Attribute.DateTime;
Expand Down
Loading
Loading