You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.4 KiB
33 lines
1.4 KiB
const bodyParser = require('body-parser'); |
|
const models = require('./models'); |
|
import * as api from '../client/src/api'; |
|
|
|
import { CreateSongEndpointHandler } from './endpoints/CreateSongEndpointHandler'; |
|
import { CreateArtistEndpointHandler } from './endpoints/CreateArtistEndpointHandler'; |
|
import { ListSongsEndpointHandler } from './endpoints/ListSongsEndpointHandler'; |
|
import { ListArtistsEndpointHandler } from './endpoints/ListArtistsEndpointHandler'; |
|
import * as endpointTypes from './endpoints/types'; |
|
|
|
const invokeHandler = (handler:endpointTypes.EndpointHandler) => { |
|
return async (req: any, res: any) => { |
|
await handler(req, res) |
|
.catch((_e:endpointTypes.EndpointError) => { |
|
let e:endpointTypes.EndpointError = _e; |
|
console.log("Error handling request: ", e.internalMessage); |
|
res.sendStatus(e.httpStatus); |
|
}) |
|
}; |
|
} |
|
|
|
const SetupApp = (app: any) => { |
|
app.use(bodyParser.json()); |
|
app.use(bodyParser.urlencoded({ extended: true })); |
|
|
|
// Set up REST API endpoints |
|
app.post(api.CreateSongEndpoint, invokeHandler(CreateSongEndpointHandler)); |
|
app.get(api.ListSongsEndpoint, invokeHandler(ListSongsEndpointHandler)); |
|
app.post(api.CreateArtistEndpoint, invokeHandler(CreateArtistEndpointHandler)); |
|
app.get(api.ListArtistsEndpoint, invokeHandler(ListArtistsEndpointHandler)); |
|
} |
|
|
|
export { SetupApp } |