Skip to content

Commit

Permalink
move nuxt stuff to app, fix ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratismal committed Jul 12, 2018
1 parent 492e046 commit adcb1d9
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 44 deletions.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<nuxt-link to='/' class='child title primary-text'>blargbot</nuxt-link>
<div class='child container'>
<div class='flexbox row'>
<div class='child'>One</div>
<div class='child'><nuxt-link to='login'>Login</nuxt-link></div>
<div class='child'>Two</div>
<div class='child'>Three</div>
</div>
Expand Down
4 changes: 1 addition & 3 deletions src/frontend/layouts/default.vue → app/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import Header from "~/components/Header.vue";
import Footer from "~/components/Footer.vue";
export default {
components: { Header, Footer },
mounted() {},
watch: {}
components: { Header, Footer }
};
</script>

Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions app/pages/login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<section>
<h1>Logging you in...</h1>
</section>
</template>

<script>
import vue from "vue";
/* global window */
export default {
mounted() {
console.log("i want to die");
console.log(vue.ws);
window.location.href =
"https://discordapp.com/oauth2/authorize" +
"?client_id=453716817639243779" +
"&scope=identify" +
"&response_type=code" +
"&redirect_uri=" +
encodeURIComponent("kys");
}
};
</script>
12 changes: 12 additions & 0 deletions app/plugins/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default async ({ req, store, app }) => {
// let token = localStorage.getItem('token');
// if (token !== null) {
// const user = await app.$axios.$get("/users/@me", {
// headers: {
// Authorization: token
// }
// });
// store.commit("setUser", user);
// app.$axios.setToken(token);
// }
};
23 changes: 23 additions & 0 deletions app/plugins/websocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This code will be injected before initializing the root App
import Vue from 'vue';
/* global window, EventEmitter */

class VueWebsocket extends EventEmitter {
constructor() {
super();
this.url = process.env.isbeta ? 'ws://localhost:8085' : 'wss://blargbot.xyz';
this.ws = new window.WebSocket(this.url);
}

install(Vue, options) {
Vue.ws = this;
}
}

const options = {};

// Activate plugin
Vue.use(new VueWebsocket(), options);

export default async ({ app }) => {
};
43 changes: 43 additions & 0 deletions app/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// const cp = require('cookieparser');

// export const state = () => ({
// user: null,
// token: null,
// privacyVersion: 0
// });

// export const mutations = {
// setUser(state, user) {
// state.user = user;
// },
// setUserPrivacy(state, accept) {
// state.user.privacyAccept = accept;
// },
// setToken(state, token) {
// state.token = token;
// },
// setPrivacyVersion(state, ver) {
// state.privacyVersion = ver;
// }
// };

// export const actions = {
// async nuxtServerInit({ commit }, { app, req, store }) {
// if (req.headers.cookie) {
// try {
// let p = cp(req.headers.cookie);
// if (p.token) {
// let user = await app.$axios.$get('/users/@me', {
// headers: {
// Authorization: p.token
// }
// });
// store.commit("setUser", user);
// app.$axios.setToken(p.token);
// }
// } catch (err) {

// }
// }
// }
// };
58 changes: 31 additions & 27 deletions nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const config = require('./config.json');

module.exports = {
srcDir: 'src/frontend',
srcDir: 'app',
/*
** Headers of the page
*/
Expand All @@ -19,6 +19,10 @@ module.exports = {
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js', body: true }
]
},
env: {
beta: config.general.isbeta
},

/*
** Global CSS
*/
Expand All @@ -28,34 +32,34 @@ module.exports = {
*/
loading: { color: '#3B8070' },

modules: [
["@nuxtjs/axios", {
prefix: "/api",
proxy: true,
port: 8102
}]
],
plugins: [],
proxy: {
"/api/": config.origin || "https://giveaways.stupidcat.me"
},
// modules: [
// ["@nuxtjs/axios", {
// prefix: "/api",
// proxy: true,
// port: 8085
// }]
// ],
// plugins: [{ src: '@/plugins/websocket.js', ssr: false }],
// proxy: {
// "/api/": config.origin || "https://blargbot.xyz"
// },
css: [{ src: '@/assets/scss/base.scss', type: 'scss' }],
/*
** Build configuration
*/
build: {
/*
** Run ESLINT on save
*/
extend(config, ctx) {
if (ctx.isClient) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
});
}
}
}
// build: {
// /*
// ** Run ESLINT on save
// */
// extend(config, ctx) {
// if (ctx.isClient) {
// config.module.rules.push({
// enforce: 'pre',
// test: /\.(js|vue)$/,
// loader: 'eslint-loader',
// exclude: /(node_modules)/
// });
// }
// }
// }
};
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"cat-loggr": "^1.0.2",
"cli-table": "^0.3.1",
"color-thief-jimp": "^2.0.2",
"cookieparser": "^0.1.0",
"eris": "^0.8.6",
"eslint-plugin-vue": "^4.5.0",
"eventemitter3": "^2.0.3",
Expand All @@ -26,8 +27,12 @@
"heapdump": "^0.3.9",
"irc": "^0.5.0",
"jimp": "^0.2.28",
"jsonwebtoken": "^8.3.0",
"koa": "^2.5.1",
"koa-bodyparser": "^4.2.1",
"koa-mount": "^3.0.0",
"koa-router": "^7.4.0",
"koa-static": "^5.0.0",
"limax": "^1.6.0",
"mkdirp": "^0.5.1",
"moment-timezone": "^0.5.21",
Expand Down Expand Up @@ -62,13 +67,14 @@
"sass-loader": "^7.0.3"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "nuxt build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/blargbot/blargbot.git"
},
"author": "",
"author": "stupid cat (ratismal)",
"license": "ISC",
"bugs": {
"url": "https://github.com/blargbot/blargbot/issues"
Expand Down
9 changes: 5 additions & 4 deletions src/core/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: stupid cat
* @Date: 2017-05-07 19:31:12
* @Last Modified by: stupid cat
* @Last Modified time: 2018-06-28 15:32:47
* @Last Modified time: 2018-07-12 10:11:43
*
* This project uses the AGPLv3 license. Please read the license file before using/adapting any of the code.
*/
Expand Down Expand Up @@ -106,9 +106,10 @@ class DiscordClient extends Client {

console.addPostHook(({ text, level, timestamp }) => {
try {
this.sender.send('log', {
text, level, timestamp
});
if (this.process.connected)
this.sender.send('log', {
text, level, timestamp
}).catch(() => { });
} catch (err) { }
})
}
Expand Down
14 changes: 8 additions & 6 deletions src/frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Koa = require('koa');
const Router = require('koa-router');
const { Nuxt, Builder } = require('nuxt');
const config = require('../../config.json');
const bodyParser = require('koa-bodyparser');

module.exports = class Frontend {
constructor(client) {
Expand All @@ -13,24 +14,25 @@ module.exports = class Frontend {
let conf = require('../../nuxt.config.js');
conf.dev = config.general.isbeta === true;
this.nuxt = new Nuxt(conf);
if (conf.dev) {
const builder = new Builder(this.nuxt);
builder.build();
if (this.nuxt.options.dev) {
new Builder(this.nuxt).build();
}
this.app.use(bodyParser());

const ApiRoute = require('./routes/api');
new ApiRoute(this);

this.app.use((ctx, next) => {
this.app.use(async (ctx, next) => {
ctx.status = 200;
if (!ctx.path.startsWith('/api'))
return new Promise((resolve, reject) => {
return await (new Promise((resolve, reject) => {
ctx.res.on('close', resolve);
ctx.res.on('finish', resolve);

this.nuxt.render(ctx.req, ctx.res, promise => {
promise.then(resolve).catch(reject)
});
});
}))
});

this.app.listen(8085);
Expand Down
13 changes: 12 additions & 1 deletion src/frontend/routes/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Router = require('koa-router');
const Security = require('../security');

module.exports = class ApiRoute {
constructor(frontend) {
Expand All @@ -8,7 +9,17 @@ module.exports = class ApiRoute {

router.get('/', async (ctx, next) => {
ctx.body = 'Hello, world!';
})
});

router.get('/authenticate', async (ctx, next) => {
console.log(ctx.req.user);
ctx.body = 'ok';
});

router.get('/users/@me', async (ctx, next) => {
let id = Security.validateToken(ctx.req.headers.authorization);

});

frontend.app.use(this.router.routes())
.use(this.router.allowedMethods());
Expand Down
26 changes: 26 additions & 0 deletions src/frontend/security.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const jwt = require('jsonwebtoken');
const config = require('../../config.json');
const secret = config.auth.secret;
let expiry = (60 * 60 * 24 * 2); // 48 hours

class Security {
static generatedToken(id) {
const token = jwt.sign({ id, exp: Math.floor(Date.now() / 1000) + expiry }, secret);
return token;
}

static validateToken(token) {
try {
const id = jwt.verify(token, secret);
return id;
} catch (err) {
return null;
}
}

static set expiry(value) {
expiry = value;
}
}

module.exports = Security;
1 change: 1 addition & 0 deletions src/structures/Sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Sender extends EventEmitter {
});
if (!didSend) {
console.error('Shard failed to send message. Connected: ' + this.process.connected);
if (!this.process.connected) process.exit();
reject(Error('Shard failed to send message'));
}
});
Expand Down

0 comments on commit adcb1d9

Please sign in to comment.