Skip to content

Commit 84fa47d

Browse files
authored
Merge pull request #32 from encap/dev
serve entry point from cdn, backend from subdomain
2 parents 63518cf + e6e9a42 commit 84fa47d

15 files changed

+45
-68
lines changed

.env.development

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VUE_APP_API_URL=http://localhost:3000

.env.production

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VUE_APP_ASSETS_PATH=https://coderushcdn.ddns.net
1+
VUE_APP_API_URL=https://api.coderush.xyz

.github/workflows/push-controller.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
- name: Vue build
7070
if: env.all || env.frontend
7171
run: |
72-
npm run build:prod
72+
npm run build
7373
- name: Copy submitted code
7474
if: env.code_examples && !env.frontend
7575
run: |

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"serve": "vue-cli-service serve --port 80",
77
"dev": "concurrently -r \"npm:serve\" \"npm:server\"",
88
"vue-ui": "vue ui",
9-
"build:prod": "vue-cli-service build",
10-
"build": "VUE_APP_ASSETS_PATH=\"\" vue-cli-service build",
9+
"build": "vue-cli-service build",
10+
"build:dev": "VUE_APP_API_URL=http://localhost:3000 vue-cli-service build",
1111
"heroku-postbuild": "echo SKIP",
1212
"lint": "vue-cli-service lint",
1313
"server": "kill $(lsof -ti :3000) & DEBUG=socket.io:client* nodemon -V -w server/ -e js --inspect ./server/server.js",
14-
"report": "VUE_APP_ASSETS_PATH=\"\" vue-cli-service build --report"
14+
"report": "vue-cli-service build --report"
1515
},
1616
"dependencies": {
1717
"@fortawesome/fontawesome-free": "^5.14.0",

server/server.js

+20-44
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,9 @@ if (process.env.AUTO_PROMOTE) {
7878
const q = faunadb.query;
7979
const client = new faunadb.Client({ secret: PROD ? process.env.FAUNA_KEY : process.env.FAUNA_DEV_KEY });
8080

81-
app.enable('trust proxy'); // trust heroku and cloudflare
82-
app.use(bodyParser.urlencoded({ extended: false }));
83-
app.use(bodyParser.json());
84-
85-
86-
let cachedIndexHtml = '';
87-
88-
const updateIndexHtmlCache = async () => {
89-
try {
90-
const res = await axios.get(process.env.INDEX_HTML_URL);
91-
cachedIndexHtml = res.data;
92-
} catch (err) {
93-
console.log('Failed to get index.html from cdn.');
94-
console.error(err);
95-
process.exit(1);
96-
}
97-
};
98-
99-
updateIndexHtmlCache();
100-
10181
let database = null;
10282
let stringifiedDB = '';
10383

104-
10584
const updateDatabaseCache = () => {
10685
stringifiedDB = JSON.stringify(database);
10786
};
@@ -171,29 +150,37 @@ if (process.env.AUTO_PROMOTE) {
171150
// });
172151

173152
// heroku free tier goes to sleep after 30 minutes of network inactivity
174-
app.get('/api/ping', (req, res) => {
153+
app.get('/ping', (req, res) => {
175154
res.sendStatus(200);
176155
});
177156

178157
setInterval(() => {
179-
axios.get('https://coderush.xyz/api/ping').catch((err) => console.error(`Ping Error: ${err}`));
158+
axios.get('https://api.coderush.xyz/ping').catch((err) => console.error(`Ping Error: ${err}`));
180159
}, 1000 * 60 * 10);
181160
} else {
161+
// DEV
162+
163+
app.use((req, res, next) => {
164+
res.header('Access-Control-Allow-Origin', '*');
165+
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
166+
res.header('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
167+
168+
next();
169+
});
170+
182171
app.use((req, res, next) => {
183172
if (!req.path.includes('code/') && (req.path.slice(-2) === 'js' || req.path.slice(-3) === 'css')) {
184173
res.header('content-encoding', 'gzip');
174+
console.log('gzip');
185175
}
176+
console.log('not gzip');
186177
next();
187178
});
188179
}
189180

190-
app.post(process.env.INDEX_HTML_UPDATE_URL, (req, res) => {
191-
if (updateIndexHtmlCache()) {
192-
res.sendStatus(201);
193-
} else {
194-
res.sendStatus(400);
195-
}
196-
});
181+
app.enable('trust proxy'); // trust heroku and cloudflare
182+
app.use(bodyParser.urlencoded({ extended: false }));
183+
app.use(bodyParser.json());
197184

198185
app.post(process.env.DATABASE_UPDATE_URL, (req, res) => {
199186
if (fetchDatabase()) {
@@ -204,24 +191,13 @@ if (process.env.AUTO_PROMOTE) {
204191
}
205192
});
206193

207-
// send cached index.html when possible
208-
app.use((req, res, next) => {
209-
const match = req.originalUrl.match(/\.\w+$/);
210-
const ext = match ? match[0][0] : '';
211-
if ((req.method === 'GET' || req.method === 'HEAD') && (ext === '' || ext === 'html')) {
212-
res.send(cachedIndexHtml);
213-
} else {
214-
next();
215-
}
216-
});
217-
218-
app.get('/database.json', (req, res) => {
194+
app.get('/data', (req, res) => {
219195
res.setHeader('Content-Type', 'application/json');
220196
res.send(stringifiedDB);
221197
});
222198

223199

224-
app.post('/api/upload', (req, res) => {
200+
app.post('/upload', (req, res) => {
225201
if (typeof req.body.code === 'string' && req.body.code.length > 20) {
226202
axios({
227203
url: 'https://api.github.com/repos/encap/coderush/dispatches',
@@ -250,7 +226,7 @@ if (process.env.AUTO_PROMOTE) {
250226
}
251227
});
252228

253-
app.post('/api/stats', async (req, res) => {
229+
app.post('/stats', async (req, res) => {
254230
const { main, misc } = req.body;
255231

256232
try {

src/cmLoader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ CodeMirror.requireMode = (mode, cont, reject) => {
5858
const script = document.createElement('script');
5959
script.onerror = () => reject(Error('No internet'));
6060
script.async = true;
61-
script.src = `${process.env.VUE_APP_ASSETS_PATH || ''}/cm/mode/${mode}/${mode}.js`;
61+
script.src = `/cm/mode/${mode}/${mode}.js`;
6262
const others = document.getElementsByTagName('script')[0];
6363
loading[mode] = [cont];
6464

@@ -108,7 +108,7 @@ const loadTheme = (name = 'material-darker') => {
108108

109109
if (!existing) {
110110
const link = document.createElement('link');
111-
link.href = `${process.env.VUE_APP_ASSETS_PATH || ''}/cm/theme/${name}.css`;
111+
link.href = `/cm/theme/${name}.css`;
112112
link.rel = 'stylesheet';
113113
link.id = name;
114114
document.head.appendChild(link);

src/components/CodeEditor.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ export default {
514514
if (!this.codeInfo.name) {
515515
return this.customCode.text;
516516
}
517-
const url = `${process.env.VUE_APP_ASSETS_PATH || ''}/code/${this.language.name.replace('#', '_sharp')}/${this.codeInfo.name}.${this.language.ext}`;
517+
const url = `/code/${this.language.name.replace('#', '_sharp')}/${this.codeInfo.name}.${this.language.ext}`;
518518
try {
519519
const resp = await axios.get(url);
520520
return resp.data;

src/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ library.add(faUsers, faPlay, faInfo, faFileCode, faShareAlt, faTimes, faLink, fa
2222
Vue.component('fa', FontAwesomeIcon);
2323
Vue.component('faStack', FontAwesomeLayers);
2424

25-
const socket = io('/', {
25+
const socket = io(process.env.VUE_APP_API_URL, {
2626
autoConnect: false,
2727
reconnectionAttempts: 3,
2828
timeout: 10000,

src/store/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import Vue from 'vue';
22
import Vuex from 'vuex';
33
import options from './modules/options';
44
import room from './modules/room';
5-
import other from './modules/other';
5+
import misc from './modules/misc';
66

77
Vue.use(Vuex);
88

99
export default new Vuex.Store({
1010
modules: {
1111
options,
1212
room,
13-
other,
13+
misc,
1414
},
1515

1616
});

src/store/modules/other.js src/store/modules/misc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const actions = {
2929
loadLanguagesList: async (context) => {
3030
if (!context.state.languagesList.length) {
3131
try {
32-
const response = await axios.get('/database.json');
32+
const response = await axios.get(`${process.env.VUE_APP_API_URL}/data`);
3333
const languagesList = response.data.languages;
3434
context.commit('SET_LANGUAGES_LIST', languagesList);
3535
context.commit('SET_DATABASE_STATS', response.data.stats);

src/views/About.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default {
106106
methods: {
107107
async loadExampleResults() {
108108
if (!this.exampleStats) {
109-
const response = await axios.get(`${process.env.VUE_APP_ASSETS_PATH || ''}/exampleResults.json`);
109+
const response = await axios.get('/exampleResults.json');
110110
this.exampleStats = response.data;
111111
}
112112

src/views/Contribute.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default {
133133
ext: this.language.ext,
134134
},
135135
};
136-
const url = `${window.location.origin}/api/upload`;
136+
const url = `${process.env.VUE_APP_API_URL}/upload`;
137137
axios.post(url, data)
138138
.then(() => {
139139
this.sent = true;

src/views/Results.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export default {
221221
deletingTime: this.format(deletingTime, 0),
222222
},
223223
};
224-
const url = `${window.location.origin}/api/stats`;
224+
const url = `${process.env.VUE_APP_API_URL}/stats`;
225225
axios.post(url, data)
226226
.then(() => {
227227
// console.log('Stats sent');

sync_dev_db.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ read_var() {
99
FAUNA_KEY=$(read_var FAUNA_KEY .env.local)
1010
FAUNA_DEV_KEY=$(read_var FAUNA_DEV_KEY .env.local)
1111

12-
cd /home/fdm && exec ./fdm --verbose -source key=$FAUNA_KEY -dest key=$FAUNA_DEV_KEY
12+
cd /home/fdm && exec ./fdm --verbose -source key=$FAUNA_KEY -dest key=$FAUNA_DEV_KEY index=ignore

vue.config.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ const zopfli = require('@gfx/zopfli');
22
// const fs = require('fs');
33
// const StatsPlugin = require('stats-webpack-plugin');
44

5-
const assetsPath = process.env.VUE_APP_ASSETS_PATH || '';
6-
75
module.exports = {
86
productionSourceMap: false,
9-
publicPath: assetsPath || '/',
107
css: {
118
loaderOptions: {
129
sass: {
@@ -69,7 +66,12 @@ module.exports = {
6966
// config.plugins.delete('prefetch');
7067
},
7168
devServer: {
72-
proxy: 'http://localhost:3000',
69+
progress: true,
70+
clientLogLevel: 'info',
71+
72+
headers: {
73+
'Access-Control-Allow-Origin': '*',
74+
},
7375
before(app) {
7476
app.use((req, res, next) => {
7577
if (req.path.slice(0, 4) === '/cm/') {
@@ -79,13 +81,11 @@ module.exports = {
7981
});
8082
},
8183

82-
progress: true,
83-
compress: false,
84-
// https: assetsPath ? false : {
84+
// compress: false,
85+
// https: {
8586
// key: fs.readFileSync(`${process.env.HOME}/localhost.key`),
8687
// cert: fs.readFileSync(`${process.env.HOME}/localhost.crt`),
8788
// ca: fs.readFileSync(`${process.env.HOME}/rootCA.crt`),
8889
// },
8990
},
90-
9191
};

0 commit comments

Comments
 (0)