-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (119 loc) · 3.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require('express');
const cors = require('cors');
const { createLogger, transports, format } = require('winston');
const ServiceProvider = require("./lib/musiclib");
let lib = new ServiceProvider();
const app = express();
const port = 3001;
app.use(cors());
app.use(express.json());
const logger = createLogger({
level: 'error',
format: format.combine(
format.timestamp(),
format.json()
),
transports: [
new transports.File({ filename: 'logs/error.log', level: 'error' }),
new transports.Console({ format: format.simple() })
]
});
app.use((err, req, res, next) => {
logger.error(err.stack);
res.status(500).json({ error: 'Internal server error' });
});
function asyncHandler(fn) {
return async (req, res, next) => {
try {
await fn(req, res, next);
} catch (error) {
next(error);
}
};
}
app.get(['/', '/api'], asyncHandler(async (req, res) => {
await res.json({ message: 'Invalid route /api' });
}));
app.get('/api/getArtist', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getArtist(query.id, query.count);
await res.json(data);
}));
app.get('/api/getTopSearches', asyncHandler(async (req, res) => {
const data = await lib.getTopSearches();
await res.json(data);
}));
app.get('/api/getHome', asyncHandler(async (req, res) => {
const data = await lib.getLaunchData();
await res.json(data);
}));
app.get('/api/getAlbums', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getAlbums(query.count, query.page);
await res.json(data);
}));
app.get('/api/getCharts', asyncHandler(async (req, res) => {
const data = await lib.getCharts();
await res.json(data);
}));
app.get('/api/getFeaturedPlaylists', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getFeaturedPlaylists(query.count, query.page);
await res.json(data);
}));
app.get('/api/getTopShows', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getTopShows(query.count, query.page);
await res.json(data);
}));
app.get('/api/getTopArtists', asyncHandler(async (req, res) => {
const data = await lib.getTopArtists();
await res.json(data);
}));
app.get('/api/playById', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.playById(query.id);
await res.json(data);
}));
app.get('/api/getLyricsById', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getLyrics(query.id);
await res.json(data);
}));
app.get('/api/getPlaylistById', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getPlaylistById(query.id);
await res.json(data);
}));
app.get('/api/search', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getSearch(query.query, query.page, query.type);
await res.json(data);
}));
app.get('/api/getTopSearch', asyncHandler(async (req, res) => {
const data = await lib.getTopSearches();
await res.json(data);
}));
app.get('/api/getFeaturedStations', asyncHandler(async (req, res) => {
const data = await lib.getFeaturedStations();
await res.json(data);
}));
app.get('/api/getSongById', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getSongById(query.id);
await res.json(data);
}));
app.get('/api/getAlbumById', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getAlbumById(query.id);
await res.json(data);
}));
app.get('/api/getStationById', asyncHandler(async (req, res) => {
const query = req.query;
const data = await lib.getStationById(name = query.name, count = query.count);
await res.json(data);
}));
// Start the server
app.listen(port, () => {
console.log(`Server is running on : ${port}`);
});