-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (165 loc) · 5.07 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* eslint-disable max-statements */
/* eslint-disable max-lines-per-function */
//Routes File
'use strict'
/* MODULE IMPORTS */
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const staticDir = require('koa-static')
const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')({ multipart: true, uploadDir: '.' })
const session = require('koa-session')
/* IMPORT ROUTE MODULES */
const getHome = require('./routes/home.get')
const postRegister = require('./routes/register.post')
const getLogin = require('./routes/login.get')
const postLogin = require('./routes/login.post')
const getSongs = require('./routes/songs.get')
const getSongDetails = require('./routes/songDetails.get')
const getPlaylists = require('./routes/playlists.get')
const postPlaylists = require('./routes/playlists.post')
const postPlaylistAdd = require('./routes/playlistAdd.post')
const getLibrary = require('./routes/library.get')
const getLibraryDetails = require('./routes/libraryDetails.get')
const getBrowse = require('./routes/browse.get')
const getUpload = require('./routes/upload.get')
const postUpload = require('./routes/upload.post')
const postComment = require('./routes/comment.post')
const getDeleteSong = require('./routes/deleteSong.get')
const getDeleteComment = require('./routes/deleteComment.get')
const getLogout = require('./routes/logout.get')
const app = new Koa()
const router = new Router()
/* CONFIGURING THE MIDDLEWARE */
app.keys = ['darkSecret']
app.use(staticDir('public'))
app.use(bodyParser())
app.use(session(app))
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, { map: { handlebars: 'handlebars' } }))
const defaultPort = 8080
const port = process.env.PORT || defaultPort
const dbName = 'website.db'
/**
* The secure home page.
* @name Home Page
* @route {GET} /
*/
router.get('/', async ctx => await getHome(ctx, dbName))
/**
* The user registration page.
* @name Register Page
* @route {GET} /register
*/
router.get('/register', async ctx => await ctx.render('register'))
/**
* The script to process new user registrations.
* @name Register Script
* @route {POST} /register
*/
router.post('/register', koaBody, async ctx => await postRegister(ctx, dbName))
/**
* The secure login page.
* @name Login Page
* @route {GET} /
* @authentication This route requires cookie-based authentication.
*/
router.get('/login', async ctx => await getLogin(ctx))
/**
* The script to process user logging in.
* @name Login script
* @route {POST} /login
*/
router.post('/login', async ctx => await postLogin(ctx, dbName))
/**
* The songs page.
* @name Songs page
* @route {GET} /songs
*/
router.get('/songs', async ctx => await getSongs(ctx, dbName))
/**
* The individual song page.
*
* @name Songs/id Page
* @route {Get} /songs
*/
router.get('/songs/:id', async ctx => await getSongDetails(ctx, dbName))
/**
* The playlist creation page.
*
* @name Playlists Page
* @route {Get} /playlists
*/
// eslint-disable-next-line complexity
router.get('/playlists', async ctx => await getPlaylists(ctx, dbName))
/**
* The script to process new playlist creations.
* @name Playlist script
* @route {POST} /playlists
*/
router.post('/playlists', koaBody, async ctx => await postPlaylists(ctx, dbName))
/**
* The script to process adding a song to a playlist.
* @name PlaylistAdd script
* @route {POST} /playlistAdd
*/
router.post('/playlistAdd', koaBody, async ctx => await postPlaylistAdd(ctx, dbName))
/**
* The user playlists page AKA Library.
*
* @name Library Page
* @route {Get} /library
*/
router.get('/library', async ctx => await getLibrary(ctx, dbName))
/**
* The individual playlist page.
* @name Library/id Page
* @route {Get} /library
*/
router.get('/library/:id', async ctx => await getLibraryDetails(ctx, dbName))
/**
* The page that displays all the playlists in the database.
*
* @name Browse Page
* @route {Get} /browse
*/
router.get('/browse', async ctx => await getBrowse(ctx, dbName))
/**
* The page responsible for uploading songs.
*
* @name Upload Page
* @route {GET} /upload
*/
router.get('/upload', async ctx => await getUpload(ctx, dbName))
/**
* The script that handles uploading music.
* @name Upload script
* @route {POST} /upload
*/
router.post('/upload', koaBody, async ctx => await postUpload(ctx, dbName, __dirname))
/**
* The script responsible for adding playlist comments.
* @name Comment script
* @route {POST} /comment
*/
router.post('/comment', koaBody, async ctx => await postComment(ctx, dbName))
/**
* The song delete script.
* @name Delete song page
* @route {GET} /delete-song/:id
*/
router.get('/delete-song/:id', async ctx => await getDeleteSong(ctx, dbName))
/**
* The comment delete script.
* @name Delete comment
* @route {GET} /delete-com/:id
*/
router.get('/delete-com/:id', async ctx => await getDeleteComment(ctx, dbName))
/**
* The upload page.
* @name Logout page
* @route {GET} /logout
*/
router.get('/logout', async ctx => await getLogout(ctx))
app.use(router.routes())
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`))