Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

refactor: extract app handler

Christian Fei99cd33d3cb12
diff --git a/lib/router/app.js b/lib/router/app.js
new file mode 100644
index 0000000..57cc0db
--- /dev/null
+++ b/lib/router/app.js
@@ -0,0 +1,44 @@
+import fs from 'fs'
+import { URL } from 'url'
+
+export default function apiHandler(req, res) {
+ const url = new URL(req.url, `http://${req.headers.host}`)
+
+ if (url.pathname === '/main.css')
+ return fileHandler('client/main.css', 'text/css')(req, res)
+ if (url.pathname === '/normalize.css')
+ return fileHandler('client/normalize.css', 'text/css')(req, res)
+ if (url.pathname === '/main.js')
+ return fileHandler('client/main.js', 'application/javascript')(req, res)
+ if (url.pathname === '/lib/store.js')
+ return fileHandler('client/lib/store.js', 'application/javascript')(req, res)
+ if (url.pathname === '/lib/router.js')
+ return fileHandler('client/lib/router.js', 'application/javascript')(req, res)
+ if (url.pathname === '/lib/utils.js')
+ return fileHandler('client/lib/utils.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/video-element.js')
+ return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/add-channel-form.js')
+ return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/manage-channels-form.js')
+ return fileHandler('client/components/manage-channels-form.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/video-quality-form.js')
+ return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/manage-disk-space-form.js')
+ return fileHandler('client/components/manage-disk-space-form.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/search-videos.js')
+ return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/channels-list.js')
+ return fileHandler('client/components/channels-list.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/empty-state.js')
+ return fileHandler('client/components/empty-state.js', 'application/javascript')(req, res)
+
+ return fileHandler('client/index.html', 'text/html')(req, res)
+}
+
+function fileHandler (filePath, contentType) {
+ return (req, res) => {
+ res.writeHead(200, { 'Content-Type': contentType })
+ res.end(fs.readFileSync(filePath, 'utf8'))
+ }
+}
\ No newline at end of file
diff --git a/lib/server.js b/lib/server.js
index 0be2e5b..97c8ffd 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -4,6 +4,7 @@ import fs from 'fs'
import { handleSSE, broadcastSSE } from './sse.js'
import { updateAndPersistVideos } from './update-videos.js'
import apiHandler from './router/api.js'
+import appHandler from './router/app.js'
export function createServer ({repo, state = {}, connections = [], updateVideos = true} = {}) {
if (updateVideos) {
@@ -31,49 +32,9 @@ export function createServer ({repo, state = {}, connections = [], updateVideos
if (url.pathname.startsWith('/api/')) return apiHandler(req, res, repo, connections, state)
- /* client */
- if (url.pathname === '/main.css')
- return fileHandler('client/main.css', 'text/css')(req, res)
- if (url.pathname === '/normalize.css')
- return fileHandler('client/normalize.css', 'text/css')(req, res)
- if (url.pathname === '/main.js')
- return fileHandler('client/main.js', 'application/javascript')(req, res)
- if (url.pathname === '/lib/store.js')
- return fileHandler('client/lib/store.js', 'application/javascript')(req, res)
- if (url.pathname === '/lib/router.js')
- return fileHandler('client/lib/router.js', 'application/javascript')(req, res)
- if (url.pathname === '/lib/utils.js')
- return fileHandler('client/lib/utils.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/video-element.js')
- return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/add-channel-form.js')
- return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/manage-channels-form.js')
- return fileHandler('client/components/manage-channels-form.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/video-quality-form.js')
- return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/manage-disk-space-form.js')
- return fileHandler('client/components/manage-disk-space-form.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/search-videos.js')
- return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/channels-list.js')
- return fileHandler('client/components/channels-list.js', 'application/javascript')(req, res)
- if (url.pathname === '/components/empty-state.js')
- return fileHandler('client/components/empty-state.js', 'application/javascript')(req, res)
-
- return fileHandler('client/index.html', 'text/html')(req, res)
+ return appHandler(req, res)
} catch (error) {
console.error(error)
}
})
}
-
-function fileHandler (filePath, contentType) {
- return (req, res) => {
- res.writeHead(200, { 'Content-Type': contentType })
- res.end(fs.readFileSync(filePath, 'utf8'))
- }
-}
-
-
-