|
|
|
@ -2,33 +2,39 @@ const bodyParser = require('body-parser'); |
|
|
|
|
import * as api from '../client/src/api'; |
|
|
|
|
import Knex from 'knex'; |
|
|
|
|
|
|
|
|
|
import { CreateSongEndpointHandler } from './endpoints/CreateSongEndpointHandler'; |
|
|
|
|
import { CreateArtistEndpointHandler } from './endpoints/CreateArtistEndpointHandler'; |
|
|
|
|
import { QueryEndpointHandler } from './endpoints/QueryEndpointHandler'; |
|
|
|
|
import { ArtistDetailsEndpointHandler } from './endpoints/ArtistDetailsEndpointHandler' |
|
|
|
|
import { SongDetailsEndpointHandler } from './endpoints/SongDetailsEndpointHandler'; |
|
|
|
|
import { ModifyArtistEndpointHandler } from './endpoints/ModifyArtistEndpointHandler'; |
|
|
|
|
import { ModifySongEndpointHandler } from './endpoints/ModifySongEndpointHandler'; |
|
|
|
|
import { CreateTagEndpointHandler } from './endpoints/CreateTagEndpointHandler'; |
|
|
|
|
import { ModifyTagEndpointHandler } from './endpoints/ModifyTagEndpointHandler'; |
|
|
|
|
import { TagDetailsEndpointHandler } from './endpoints/TagDetailsEndpointHandler'; |
|
|
|
|
import { CreateAlbumEndpointHandler } from './endpoints/CreateAlbumEndpointHandler'; |
|
|
|
|
import { ModifyAlbumEndpointHandler } from './endpoints/ModifyAlbumEndpointHandler'; |
|
|
|
|
import { AlbumDetailsEndpointHandler } from './endpoints/AlbumDetailsEndpointHandler'; |
|
|
|
|
import { DeleteTagEndpointHandler } from './endpoints/DeleteTagEndpointHandler'; |
|
|
|
|
import { MergeTagEndpointHandler } from './endpoints/MergeTagEndpointHandler'; |
|
|
|
|
import { CreateSongEndpointHandler } from './endpoints/CreateSong'; |
|
|
|
|
import { CreateArtistEndpointHandler } from './endpoints/CreateArtist'; |
|
|
|
|
import { QueryEndpointHandler } from './endpoints/Query'; |
|
|
|
|
import { ArtistDetailsEndpointHandler } from './endpoints/ArtistDetails' |
|
|
|
|
import { SongDetailsEndpointHandler } from './endpoints/SongDetails'; |
|
|
|
|
import { ModifyArtistEndpointHandler } from './endpoints/ModifyArtist'; |
|
|
|
|
import { ModifySongEndpointHandler } from './endpoints/ModifySong'; |
|
|
|
|
import { CreateTagEndpointHandler } from './endpoints/CreateTag'; |
|
|
|
|
import { ModifyTagEndpointHandler } from './endpoints/ModifyTag'; |
|
|
|
|
import { TagDetailsEndpointHandler } from './endpoints/TagDetails'; |
|
|
|
|
import { CreateAlbumEndpointHandler } from './endpoints/CreateAlbum'; |
|
|
|
|
import { ModifyAlbumEndpointHandler } from './endpoints/ModifyAlbum'; |
|
|
|
|
import { AlbumDetailsEndpointHandler } from './endpoints/AlbumDetails'; |
|
|
|
|
import { DeleteTagEndpointHandler } from './endpoints/DeleteTag'; |
|
|
|
|
import { MergeTagEndpointHandler } from './endpoints/MergeTag'; |
|
|
|
|
import { RegisterUserEndpointHandler } from './endpoints/RegisterUser'; |
|
|
|
|
import * as endpointTypes from './endpoints/types'; |
|
|
|
|
import { sha512 } from 'js-sha512'; |
|
|
|
|
|
|
|
|
|
const invokeHandler = (handler:endpointTypes.EndpointHandler, knex: Knex) => { |
|
|
|
|
// For authentication
|
|
|
|
|
var passport = require('passport'); |
|
|
|
|
var Strategy = require('passport-local').Strategy; |
|
|
|
|
|
|
|
|
|
const invokeHandler = (handler: endpointTypes.EndpointHandler, knex: Knex) => { |
|
|
|
|
return async (req: any, res: any) => { |
|
|
|
|
console.log("Incoming", req.method, " @ ", req.url); |
|
|
|
|
await handler(req, res, knex) |
|
|
|
|
.catch(endpointTypes.catchUnhandledErrors) |
|
|
|
|
.catch((_e:endpointTypes.EndpointError) => { |
|
|
|
|
let e:endpointTypes.EndpointError = _e; |
|
|
|
|
console.log("Error handling request: ", e.internalMessage); |
|
|
|
|
res.sendStatus(e.httpStatus); |
|
|
|
|
}) |
|
|
|
|
.catch(endpointTypes.catchUnhandledErrors) |
|
|
|
|
.catch((_e: endpointTypes.EndpointError) => { |
|
|
|
|
let e: endpointTypes.EndpointError = _e; |
|
|
|
|
console.log("Error handling request: ", e.internalMessage); |
|
|
|
|
res.sendStatus(e.httpStatus); |
|
|
|
|
}) |
|
|
|
|
console.log("Finished handling", req.method, "@", req.url); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
@ -37,26 +43,63 @@ const SetupApp = (app: any, knex: Knex, apiBaseUrl: string) => { |
|
|
|
|
app.use(bodyParser.json()); |
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true })); |
|
|
|
|
|
|
|
|
|
const invokeWithKnex = (handler: endpointTypes.EndpointHandler) => { |
|
|
|
|
// Set up auth. See: https://github.com/passport/express-4.x-local-example.git
|
|
|
|
|
passport.use(new Strategy( |
|
|
|
|
function (email: string, password: string, cb: any) { |
|
|
|
|
(async () => { |
|
|
|
|
try { |
|
|
|
|
const user = await knex.select(['email', 'passwordHash', 'id']) |
|
|
|
|
.from('users') |
|
|
|
|
.where({ 'email': email }) |
|
|
|
|
.then((users: any) => users[0]); |
|
|
|
|
if (!user) { cb(null, false); } |
|
|
|
|
if (sha512(password) != user.passwordHash) { |
|
|
|
|
return cb(null, false); |
|
|
|
|
} |
|
|
|
|
return cb(null, user); |
|
|
|
|
} catch (error) { cb(error); } |
|
|
|
|
})(); |
|
|
|
|
})); |
|
|
|
|
// passport.serializeUser(function (user: any, cb: any) {
|
|
|
|
|
// cb(null, user.id);
|
|
|
|
|
// });
|
|
|
|
|
// passport.deserializeUser(function (id: number, cb: any) {
|
|
|
|
|
// (async () => {
|
|
|
|
|
// try {
|
|
|
|
|
// const user = await knex.select(['email', 'passwordHash', 'id'])
|
|
|
|
|
// .from('users')
|
|
|
|
|
// .where({ 'id': id })
|
|
|
|
|
// .then((users: any) => users[0]);
|
|
|
|
|
// if (!user) { cb(null, false); }
|
|
|
|
|
// return cb(null, user);
|
|
|
|
|
// } catch (error) { cb(error); }
|
|
|
|
|
// })();
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
app.use(passport.initialize()); |
|
|
|
|
//app.use(passport.session());
|
|
|
|
|
|
|
|
|
|
const _invoke = (handler: endpointTypes.EndpointHandler) => { |
|
|
|
|
return invokeHandler(handler, knex); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Set up REST API endpoints
|
|
|
|
|
app.post(apiBaseUrl + api.CreateSongEndpoint, invokeWithKnex(CreateSongEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.QueryEndpoint, invokeWithKnex(QueryEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateArtistEndpoint, invokeWithKnex(CreateArtistEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifyArtistEndpoint, invokeWithKnex(ModifyArtistEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifySongEndpoint, invokeWithKnex(ModifySongEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.SongDetailsEndpoint, invokeWithKnex(SongDetailsEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.ArtistDetailsEndpoint, invokeWithKnex(ArtistDetailsEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateTagEndpoint, invokeWithKnex(CreateTagEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifyTagEndpoint, invokeWithKnex(ModifyTagEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.TagDetailsEndpoint, invokeWithKnex(TagDetailsEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateAlbumEndpoint, invokeWithKnex(CreateAlbumEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifyAlbumEndpoint, invokeWithKnex(ModifyAlbumEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.AlbumDetailsEndpoint, invokeWithKnex(AlbumDetailsEndpointHandler)); |
|
|
|
|
app.delete(apiBaseUrl + api.DeleteTagEndpoint, invokeWithKnex(DeleteTagEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.MergeTagEndpoint, invokeWithKnex(MergeTagEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateSongEndpoint, _invoke(CreateSongEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.QueryEndpoint, _invoke(QueryEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateArtistEndpoint, _invoke(CreateArtistEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifyArtistEndpoint, _invoke(ModifyArtistEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifySongEndpoint, _invoke(ModifySongEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.SongDetailsEndpoint, passport.authenticate('local', { session: false }), _invoke(SongDetailsEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.ArtistDetailsEndpoint, _invoke(ArtistDetailsEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateTagEndpoint, _invoke(CreateTagEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifyTagEndpoint, _invoke(ModifyTagEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.TagDetailsEndpoint, _invoke(TagDetailsEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.CreateAlbumEndpoint, _invoke(CreateAlbumEndpointHandler)); |
|
|
|
|
app.put(apiBaseUrl + api.ModifyAlbumEndpoint, _invoke(ModifyAlbumEndpointHandler)); |
|
|
|
|
app.get(apiBaseUrl + api.AlbumDetailsEndpoint, _invoke(AlbumDetailsEndpointHandler)); |
|
|
|
|
app.delete(apiBaseUrl + api.DeleteTagEndpoint, _invoke(DeleteTagEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.MergeTagEndpoint, _invoke(MergeTagEndpointHandler)); |
|
|
|
|
app.post(apiBaseUrl + api.RegisterUserEndpoint, _invoke(RegisterUserEndpointHandler)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export { SetupApp } |