-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
131 lines (107 loc) · 4.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const path = require('path');
const fs = require('fs')
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const express = require('express');
const app = express();
const paths = ['login','signup','passwordreset','settings','activity','posts','explore','new','confirm'];
const { MetadataGenerator } = require('metatags-generator');
const indexPath = path.resolve(__dirname, 'build', 'index.html');
const backendURL = process.env.REACT_APP_BACKEND_URL || `https://backpanel.dogegram.xyz`;
const settings = {
androidChromeIcons: true,
msTags: true,
safariTags: true,
appleTags: true,
openGraphTags: true,
twitterTags: true,
facebookTags: true
};
var indexfile;
const load = () => {
console.log('load')
fs.readFile(indexPath, 'utf8', (err, data)=>{
indexfile = data
// console.log(data)
});
}
load()
// static resources should just be served as they are
app.use(express.static(
path.resolve(__dirname, 'build'),
{ maxAge: '30d' },
));
app.get('/post/:postId', async (req, res, next) => {
try{
const postId = req.params.postId;
let htmlData = indexfile;
const requsermeta = await fetch(`${backendURL}/api/post/internal/meta/${postId}`);
const usermeta = await requsermeta.json();
if(usermeta.error){
//cache the index file for 2 mins but revalidate it
res.set('Cache-Control', 'public, max-age=120, s-maxage=120, stale-while-revalidate=60');
return res.send(indexfile)
}
const generator = new MetadataGenerator();
const meta = generator
.configure(settings)
.setPageMeta({
title: `@${usermeta.userName}'s Dogegram post`,
description: `${usermeta.caption}`,
url: `https://app.dogegram.xyz/post/${postId}`,
image: `https://bom1-storage.dogegram.xyz/${usermeta.image.split('/').pop()}`,
keywords: `site, website, profile`,
locale: 'en_US'
})
.build();
//kinda hacky but it works
let metahtml = htmlData.replace('<meta name="description" content="The cool new social media platform!"/>', meta.head)
metahtml = metahtml.replace(/(\r\n|\n|\r)/gm, "")
return res.send(metahtml);
} catch(err){
return res.send(indexfile)
}
})
app.get('/:userId', async (req, res, next) => {
const userId = req.params.userId;
if(!paths.includes(userId)){
let htmlData = indexfile;
const requsermeta = await fetch(`${backendURL}/api/user/internal/meta/${userId}`);
const usermeta = await requsermeta.json();
if(usermeta.error === 'Could not find a user with that username.'){
//cache the index file for 2 mins but revalidate it
res.set('Cache-Control', 'public, max-age=120, s-maxage=120, stale-while-revalidate=60');
return res.send(indexfile)
}
const generator = new MetadataGenerator();
const meta = generator
.configure(settings)
.setPageMeta({
title: `${usermeta.name}'s dogegram profile`,
description: `${usermeta.bio}`,
url: `https://app.dogegram.xyz/${userId}`,
image: `https://bom1-storage.dogegram.xyz/${usermeta.avatar.split('/').pop()}`,
keywords: `site, website, profile, ${usermeta.name}`,
locale: 'en_US'
})
.build();
let metahtml = htmlData.replace('<meta name="description" content="The cool new social media platform!"/>', meta.head)
metahtml = metahtml.replace(/(\r\n|\n|\r)/gm, "")
res.setHeader('Cache-Control', 'public, max-age=300');
return res.send(metahtml);
} else {
return res.send(indexfile)
}
});
app.use(function (req, res, next) {
res.status(200).send(indexfile)
})
app.listen(process.env.PORT || 3000, async (error) => {
if (error) {
return console.log('Error during app startup', error);
}
var gitdatareq = await fetch('https://api.github.com/repos/Dogegram/Frontend/commits/master')
var gitdata = await gitdatareq.json()
console.log(`listening on ${process.env.PORT || 3000}...`);
console.log(`running git commit hash: ${gitdata.sha.slice(0, 7)}...`);
console.log(`git commit message: ${gitdata.commit.message}...`);
});