Skip to content
Open
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
56 changes: 30 additions & 26 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,48 @@ let getPublicProjects = memoize(() => {
return axios({
httpsAgent: agent,
method: 'GET',
url: SERVER_ADDRESS +'/api/Projects/PROJECTS'
url: SERVER_ADDRESS + '/api/Projects/PROJECTS',
});
}, {promise: true, maxAge: 86400 });
}, { promise: true, maxAge: 86400 });

let getExamples = memoize(() => {
log.debug('Calling server for example projects');
return axios({
httpsAgent: agent,
method: 'GET',
url: SERVER_ADDRESS + '/api/Examples/EXAMPLES?metadata=true',
method: 'get'
});
}, {promise: true, maxAge: 86400 });

app.get('/', (req, res) => {
}, { promise: true, maxAge: 86400 });

// set caching headers
res.set({
app.get('/', async (req, res) => {
res.set({ // set caching headers
'Cache-Control': 'private, max-age=3600',
});

// get the examples and public projects data
// get examples data
let examplesPromise = getExamples();
// get projects data
let publicProjectsPromise = getPublicProjects();
// end of calls to get the data

axios.all([examplesPromise,publicProjectsPromise]).then(axios.spread((examples,projects)=>{
log.debug('Data received from server',projects.data.length);
// this is cached by default by express if node env is set to production
examples.data = examples.data.filter(eg => !['Weather','Star Map','Battleship','Earthquakes'].includes(eg.projectName));
res.render('index.pug', {examples: examples.data, projects: projects.data });
})).catch((err)=>{
//handle errors
log.debug('Failed to get projects data from netsblox server.',err);
res.status(500).send();
});

const [examples, projects] = await Promise.all([
(async () => {
try {
const ex = await getExamples();
// this is cached by default by express if node env is set to production
return ex.data.filter(eg => !['Weather', 'Star Map', 'Battleship', 'Earthquakes'].includes(eg.projectName));
} catch (e) {
log.debug('Failed to get example projects from netsblox server', e);
return [];
}
})(),
(async () => {
try {
const proj = await getPublicProjects();
return proj.data;
} catch (e) {
log.debug('Failed to get public projects from netsblox server', e);
return [];
}
})(),
]);

log.debug(`Data received from server: ${examples.length} examples and ${projects.length} public projects`);
res.render('index.pug', { examples, projects });
});

function renderView(res, path) {
Expand Down
3 changes: 2 additions & 1 deletion src/server/views/includes/pubProjects.pug
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
ul#projects-slider.cS-hidden
each eg in projects
if eg.thumbnail
- var img = Array.isArray(eg.thumbnail) ? eg.thumbnail[0] : eg.thumbnail
.element-item.col-xs-12.col-sm-4.col-md-3.col-lg-2
a(class="wrapper-link", href= env.EDITOR_ADDRESS + '/#present:Username='+eg.owner+'&ProjectName='+eg.projectName + '&editMode=true', target='_blank')
.h-thumbnail
.img-container
img.img-responsive.lazy.center-block.img-thumbnail(src='images/placeholder.jpg', data-src= eg.thumbnail, style='width: 100%;')
img.img-responsive.lazy.center-block.img-thumbnail(src=img, data-src=img, style='width: 100%;')
.overlay
.caption.text-center= eg.notes || 'No description provided.'
ul.list-group
Expand Down