From f5a60a5138135a3412dfbcfd6e564f7aa47a55c2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 6 Nov 2015 17:34:15 +0100 Subject: [PATCH] Add API versionning --- public/javascripts/index.js | 10 +++++----- routes/api/{ => v1}/index.js | 0 routes/api/{ => v1}/pods.js | 4 ++-- routes/api/{ => v1}/remoteVideos.js | 4 ++-- routes/api/{ => v1}/videos.js | 4 ++-- server.js | 12 ++++++++---- src/pods.js | 4 ++-- src/videos.js | 4 ++-- test/api/friends.js | 4 ++-- test/api/multiplePods.js | 4 ++-- test/api/singlePod.js | 11 +++++++++-- 11 files changed, 36 insertions(+), 25 deletions(-) rename routes/api/{ => v1}/index.js (100%) rename routes/api/{ => v1}/pods.js (89%) rename routes/api/{ => v1}/remoteVideos.js (87%) rename routes/api/{ => v1}/videos.js (93%) diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 95caf7eb4..c67e22c7b 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -38,7 +38,7 @@ if (e.keyCode === 13) { $.ajax({ - url: '/api/videos/search/' + search, + url: '/api/v1/videos/search/' + search, type: 'GET', dataType: 'json', success: function (videos) { @@ -51,7 +51,7 @@ // Join a new network function makeFriends () { $.ajax({ - url: '/api/pods/makefriends', + url: '/api/v1/pods/makefriends', type: 'GET', dataType: 'json', success: function () { @@ -140,7 +140,7 @@ $form_video.fileupload({ singleFileUploads: true, multipart: true, - url: '/api/videos', + url: '/api/v1/videos', autoupload: false, add: function (e, data) { var $text = $('').addClass('name_file').text(data['files'][0]['name']) @@ -170,7 +170,7 @@ // Print the list of all the videos function getVideos () { $.ajax({ - url: '/api/videos/', + url: '/api/v1/videos/', dataType: 'json', type: 'GET', success: function (videos) { @@ -181,7 +181,7 @@ function removeVideo (video) { $.ajax({ - url: '/api/videos/' + video._id, + url: '/api/v1/videos/' + video._id, type: 'DELETE', success: function (response, status) { getVideos() diff --git a/routes/api/index.js b/routes/api/v1/index.js similarity index 100% rename from routes/api/index.js rename to routes/api/v1/index.js diff --git a/routes/api/pods.js b/routes/api/v1/pods.js similarity index 89% rename from routes/api/pods.js rename to routes/api/v1/pods.js index 8fa29b4f7..961388fcb 100644 --- a/routes/api/pods.js +++ b/routes/api/v1/pods.js @@ -3,8 +3,8 @@ var express = require('express') var router = express.Router() - var middleware = require('../../middlewares') - var pods = require('../../src/pods') + var middleware = require('../../../middlewares') + var pods = require('../../../src/pods') function listPods (req, res, next) { pods.list(function (err, pods_list) { diff --git a/routes/api/remoteVideos.js b/routes/api/v1/remoteVideos.js similarity index 87% rename from routes/api/remoteVideos.js rename to routes/api/v1/remoteVideos.js index 2ae3ce5bc..88b8e879b 100644 --- a/routes/api/remoteVideos.js +++ b/routes/api/v1/remoteVideos.js @@ -3,8 +3,8 @@ var express = require('express') var router = express.Router() - var middleware = require('../../middlewares') - var videos = require('../../src/videos') + var middleware = require('../../../middlewares') + var videos = require('../../../src/videos') function addRemoteVideos (req, res, next) { videos.addRemote(req.body.data, function (err, video) { diff --git a/routes/api/videos.js b/routes/api/v1/videos.js similarity index 93% rename from routes/api/videos.js rename to routes/api/v1/videos.js index 087fc96bc..246620ac6 100644 --- a/routes/api/videos.js +++ b/routes/api/v1/videos.js @@ -3,8 +3,8 @@ var express = require('express') var router = express.Router() - var middleware = require('../../middlewares') - var videos = require('../../src/videos') + var middleware = require('../../../middlewares') + var videos = require('../../../src/videos') function listVideos (req, res, next) { videos.list(function (err, videos_list) { diff --git a/server.js b/server.js index 96c493f29..d3718f8de 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,9 @@ ;(function () { 'use strict' + // ----------- Constantes ----------- + global.API_VERSION = 'v1' + // ----------- Node modules ----------- var express = require('express') var path = require('path') @@ -28,7 +31,7 @@ var config = require('config') var logger = require('./src/logger') var routes = require('./routes') - var api = require('./routes/api') + var api = require('./routes/api/' + global.API_VERSION) var utils = require('./src/utils') var videos = require('./src/videos') var webtorrent = require('./src/webTorrentNode') @@ -88,9 +91,10 @@ } // ----------- Routes ----------- - app.use('/api/videos', api.videos) - app.use('/api/remotevideos', api.remoteVideos) - app.use('/api/pods', api.pods) + var api_route = '/api/' + global.API_VERSION + app.use(api_route + '/videos', api.videos) + app.use(api_route + '/remotevideos', api.remoteVideos) + app.use(api_route + '/pods', api.pods) // ----------- Tracker ----------- diff --git a/src/pods.js b/src/pods.js index 30d465ee5..db159a466 100644 --- a/src/pods.js +++ b/src/pods.js @@ -18,7 +18,7 @@ // ----------- Private functions ----------- function getForeignPodsList (url, callback) { - var path = '/api/pods' + var path = '/api/' + global.API_VERSION + '/pods' request.get(url + path, function (err, response, body) { if (err) throw err @@ -143,7 +143,7 @@ logger.debug('Make requests...') utils.makeMultipleRetryRequest( - { method: 'POST', path: '/api/pods/', data: data }, + { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data }, pods_list, diff --git a/src/videos.js b/src/videos.js index 48ec19d4d..f787ae49c 100644 --- a/src/videos.js +++ b/src/videos.js @@ -73,7 +73,7 @@ logger.debug('Sending this video Uri to friends...') var data = { - path: '/api/remotevideos/add', + path: '/api/' + global.API_VERSION + '/remotevideos/add', method: 'POST', data: params } @@ -130,7 +130,7 @@ } var data = { - path: '/api/remotevideos/remove', + path: '/api/' + global.API_VERSION + '/remotevideos/remove', method: 'POST', data: { magnetUri: video.magnetUri diff --git a/test/api/friends.js b/test/api/friends.js index 033d3799a..845ccd1a8 100644 --- a/test/api/friends.js +++ b/test/api/friends.js @@ -9,7 +9,7 @@ var utils = require('../utils') function getFriendsList (url, end) { - var path = '/api/pods/' + var path = '/api/v1/pods/' request(url) .get(path) @@ -75,7 +75,7 @@ }) } - var path = '/api/pods/makefriends' + var path = '/api/v1/pods/makefriends' // The second pod make friend with the third request(urls[1]) diff --git a/test/api/multiplePods.js b/test/api/multiplePods.js index 3b69738f4..1edfc1ce3 100644 --- a/test/api/multiplePods.js +++ b/test/api/multiplePods.js @@ -11,7 +11,7 @@ webtorrent.silent = true describe('Test multiple pods', function () { - var path = '/api/videos' + var path = '/api/v1/videos' var apps = [] var urls = [] var video_id = -1 @@ -38,7 +38,7 @@ before(function (done) { this.timeout(30000) - var path_friends = '/api/pods/makefriends' + var path_friends = '/api/v1/pods/makefriends' utils.runMultipleServers(3, function (apps_run, urls_run) { apps = apps_run diff --git a/test/api/singlePod.js b/test/api/singlePod.js index 5c4c892bb..bc9243c1c 100644 --- a/test/api/singlePod.js +++ b/test/api/singlePod.js @@ -3,6 +3,7 @@ var request = require('supertest') var chai = require('chai') + var fs = require('fs') var expect = chai.expect var webtorrent = require(__dirname + '/../../src/webTorrentNode') webtorrent.silent = true @@ -10,7 +11,7 @@ var utils = require('../utils') describe('Test a single pod', function () { - var path = '/api/videos' + var path = '/api/v1/videos' var app = null var url = '' var video_id = -1 @@ -98,7 +99,13 @@ .expect(204) .end(function (err, res) { if (err) throw err - done() + + fs.readdir(__dirname + '/../../test1/uploads/', function (err, files) { + if (err) throw err + + expect(files.length).to.equal(0) + done() + }) }) }) -- 2.41.0