Browse Source

API can now access different actions using a similar system to that of socket emitting. Any actions that require hooks don't work with the API yet.

theflametrooper 8 years ago
parent
commit
7424308509
4 changed files with 29 additions and 11 deletions
  1. 4 0
      backend/index.js
  2. 2 2
      backend/logic/actions/songs.js
  3. 23 7
      backend/logic/api.js
  4. 0 2
      backend/logic/app.js

+ 4 - 0
backend/index.js

@@ -6,6 +6,7 @@ const async = require('async');
 
 const db = require('./logic/db');
 const app = require('./logic/app');
+const api = require('./logic/api');
 const io = require('./logic/io');
 const stations = require('./logic/stations');
 const songs = require('./logic/songs');
@@ -49,6 +50,9 @@ async.waterfall([
 	// setup the playlists
 	(next) => playlists.init(next),
 
+	// setup the API
+	(next) => api.init(next),
+
 	// setup the frontend for local setups
 	(next) => {
 		if (!config.get("isDocker")) {

+ 2 - 2
backend/logic/actions/songs.js

@@ -56,12 +56,12 @@ cache.sub('song.undislike', (data) => {
 
 module.exports = {
 
-	index: (session, cb) => {
+	index: hooks.adminRequired((session, cb) => {
 		db.models.song.find({}, (err, songs) => {
 			if (err) throw err;
 			cb(songs);
 		});
-	},
+	}),
 
 	update: hooks.adminRequired((session, songId, song, cb) => {
 		db.models.song.update({ _id: songId }, song, { upsert: true }, (err, updatedSong) => {

+ 23 - 7
backend/logic/api.js

@@ -1,11 +1,27 @@
-module.exports = (app) => {
-	'use strict';
+module.exports = {
+	init: (cb) => {
+		const { app } = require('./app.js');
+		const actions = require('./actions');
 
-	app.get('/', (req, res) => {
-		res.json({
-			status: 'success',
-			message: 'Coming Soon'
+		app.get('/', (req, res) => {
+			res.json({
+				status: 'success',
+				message: 'Coming Soon'
+			});
 		});
-	});
 
+		Object.keys(actions).forEach((namespace) => {
+			Object.keys(actions[namespace]).forEach((action) => {
+				let name = `/${namespace}/${action}`;
+
+				app.get(name, (req, res) => {
+					actions[namespace][action](null, (result) => {
+						if (typeof cb === 'function') return res.json(result);
+					});
+				});
+			})
+		});
+
+		cb();
+	}
 }

+ 0 - 2
backend/logic/app.js

@@ -34,8 +34,6 @@ const lib = {
 		app.use(cors(corsOptions));
 		app.options('*', cors(corsOptions));
 
-		api(app);
-
 		let oauth2 = new OAuth2(
 			config.get('apis.github.client'),
 			config.get('apis.github.secret'),