rm next based api; rm custom next server; rm koa

This commit is contained in:
tmshv committed 2020-02-15 04:23:22 +03:00
1 parent d3b5b2d5c6
commit c0b43f8afd
21 files changed
+13 -428

No files matched your search

+1 -9
View File
@@ -4,8 +4,7 @@
"version": "6.0.0",
"description": "",
"scripts": {
"start": "ts-node --project server.tsconfig.json src/server.ts",
"next-dev": "next dev",
"start": "next dev",
"build": "next build && next export -o dist && npm run sitemap && npm run feed",
"sitemap": "node scripts/sitemap.js",
"feed": "node scripts/feed.js",
@@ -20,11 +19,8 @@
"dependencies": {
"axios": "^0.19.0",
"classnames": "^2.2.6",
"koa": "^2.11.0",
"koa-route": "^3.1.0",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"mongodb": "^2.1.14",
"next": "^9.2.2-canary.18",
"react": "^16.12.0",
"react-dom": "^16.12.0",
@@ -33,10 +29,7 @@
},
"devDependencies": {
"@types/classnames": "^2.2.9",
"@types/koa": "^2.0.52",
"@types/koa-route": "^3.2.4",
"@types/lodash": "^4.14.149",
"@types/mongodb": "^3.3.11",
"@types/node": "^12.12.12",
"@types/react": "^16.9.12",
"@zeit/next-css": "^1.0.1",
@@ -50,7 +43,6 @@
"mocha": "^6.2.2",
"node-sass": "^4.13.0",
"should": "^8.2.2",
"ts-node": "^8.5.2",
"typescript": "^3.7.5"
}
}
+12 -3
View File
@@ -9,6 +9,12 @@ import { Meta } from '../src/components/Meta'
import { meta } from '../src/lib/meta'
import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib'
function array<T>(value: T | T[]) {
return Array.isArray(value)
? value
: [value]
}
const Index = props => (
<App
menu={buildMenu(props.pageUrl, menuModel)}
@@ -30,13 +36,16 @@ const Index = props => (
)
Index.getInitialProps = wrapInitialProps(async (ctx) => {
const pageUrl = ctx.req.url
const page = await requestGet(createApiUrl(`/api/page?page=${pageUrl}`), {})
const slug = '/' + array(ctx.query.slug).join('/')
const page = await requestGet(createApiUrl(`/api/page?page=${slug}`), null)
if (!page) {
throw new Error(`Not found: ${slug}`)
}
const image = page.preview ? page.preview.artifacts.fb : {}
return {
content: page.data,
pageUrl,
pageUrl: slug,
title: page.title,
meta: meta({
title: page.title,
-19
View File
@@ -1,19 +0,0 @@
import { connect } from '../../../src/core/db'
import Album from '../../../src/core/Album'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const id = req.query.album
const album = await Album.findById(id)
if (album) {
res.json(album)
} else {
res.status(404)
res.json({
error: `Album ${id} not found`
})
}
})
-19
View File
@@ -1,19 +0,0 @@
import { connect } from '../../../src/core/db'
import Album from '../../../src/core/Album'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const data = await Album.find({}, { sort: { date: -1 } })
if (data) {
res.json({
items: data,
})
} else {
res.status(404)
res.json({
error: `Article not found`
})
}
})
-20
View File
@@ -1,20 +0,0 @@
import { connect } from '../../../src/core/db'
import Album from '../../../src/core/Album'
export default async (req, res) => {
await connect()
const data = await Album.find({})
if (data) {
const items = data.map(x => `/album/${x.id}`)
res.json({
items
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
}
-21
View File
@@ -1,21 +0,0 @@
import { NextApiRequest, NextApiResponse } from 'next'
import Article from '../../../src/core/Article'
import { encodeArticle } from '../../../src/server/articles'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req: NextApiRequest, res: NextApiResponse) => {
const {
query: { article: id }
} = req;
const article = await Article.findById(id)
if (article) {
res.json(encodeArticle(article))
} else {
res.status(404)
res.json({
error: `Article ${id} not found`
})
}
})
-24
View File
@@ -1,24 +0,0 @@
import { findArticles } from '../../../src/server/articles'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
const int = (value: string | string[]) => parseInt(Array.isArray(value)
? value[0]
: value
)
export default withMiddleware(async (req, res) => {
const { query } = req
const page = int(query.page)
const pageSize = int(query.pageSize)
const data = await findArticles(page, pageSize)
if (data) {
res.json(data)
} else {
res.status(404)
res.json({
error: `Articles not found`
})
}
})
-21
View File
@@ -1,21 +0,0 @@
import { connect } from '../../../src/core/db'
import Article from '../../../src/core/Article'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const data = await Article.find({})
if (data) {
const items = data.map(x => `/article/${x.id}`)
res.json({
items
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
})
-20
View File
@@ -1,20 +0,0 @@
import { connect } from '../../../src/core/db'
import Document from '../../../src/core/Document'
import { encodeFile } from '../../../src/api/files'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const id = req.query.document
const document = await Document.findById(id)
if (document) {
res.json(encodeFile(document))
} else {
res.status(404)
res.json({
error: `Document ${id} not found`
})
}
})
-24
View File
@@ -1,24 +0,0 @@
import { connect } from '../../../src/core/db'
import Document from '../../../src/core/Document'
import { encodeFile } from '../../../src/api/files'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const documents = await Document.find({hidden: false})
if (documents) {
const items = documents
.map(encodeFile)
.filter(x => x.imageUrl) // skip documents without preview image; needs to be refactored
res.json({
items,
})
} else {
res.status(404)
res.json({
error: `Documents not found`
})
}
})
-21
View File
@@ -1,21 +0,0 @@
import { connect } from '../../../src/core/db'
import Document from '../../../src/core/Document'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const data = await Document.find({})
if (data) {
const items = data.map(x => `/document/${x.id}`)
res.json({
items
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
})
-17
View File
@@ -1,17 +0,0 @@
import Image from '../../src/core/Image'
import { encodeImage } from '../../src/api/image'
import { withMiddleware } from '../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
const file = req.query.file
const image = await Image.findByFile(file)
if (image) {
res.json(encodeImage(image))
} else {
res.status(404)
res.json({
error: `Image ${file} not found`
})
}
})
-21
View File
@@ -1,21 +0,0 @@
import { connect } from '../../src/core/db'
import Page from '../../src/core/Page'
import { getPathWithNoTrailingSlash } from '../../src/lib/url'
import { encodePage } from '../../src/api/page'
import { withMiddleware } from '../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const page = getPathWithNoTrailingSlash(req.query.page)
const resource = await Page.findByUrl(page)
if (resource) {
res.json(encodePage(resource))
} else {
res.status(404)
res.json({
error: `Image ${page} not found`
})
}
})
-17
View File
@@ -1,17 +0,0 @@
import Page from '../../../src/core/Page'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
const items = await Page.find({})
if (items) {
res.json({
items,
})
} else {
res.status(404)
res.json({
error: `Article ${id} not found`
})
}
})
-21
View File
@@ -1,21 +0,0 @@
import { connect } from '../../../src/core/db'
import Page from '../../../src/core/Page'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const data = await Page.find({})
if (data) {
const items = data.map(x => x.url)
res.json({
items,
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
})
-20
View File
@@ -1,20 +0,0 @@
import { connect } from '../../../src/core/db'
import Teacher from '../../../src/core/Teacher'
import { encodePerson } from '../../../src/api/persons'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const id = req.query.person
const data = await Teacher.findById(id)
if (data) {
res.json(encodePerson(data))
} else {
res.status(404)
res.json({
error: `Article ${id} not found`
})
}
})
-33
View File
@@ -1,33 +0,0 @@
import { connect } from '../../../src/core/db'
import Teacher from '../../../src/core/Teacher'
import { prioritySort } from '../../../src/lib/sort'
import { encodePerson } from '../../../src/api/persons'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const order = [
'mg-timasheva',
'va-sarzhin',
'od-gogoleva',
'my-valkova',
'nv-andreeva',
'vv-voronova',
]
const teachersSorted = prioritySort.bind(null, [...order], t => t.id)
const teachers = await Teacher.find({ hidden: false })
const data = teachersSorted(teachers).map(encodePerson)
if (data) {
res.json({
items: data,
})
} else {
res.status(404)
res.json({
error: `Article ${id} not found`
})
}
})
-21
View File
@@ -1,21 +0,0 @@
import { connect } from '../../../src/core/db'
import Teacher from '../../../src/core/Teacher'
import { withMiddleware } from '../../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
await connect()
const data = await Teacher.find({})
if (data) {
const items = data.map(x => `/teacher/${x.id}`)
res.json({
items
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
})
-18
View File
@@ -1,18 +0,0 @@
import { findSchedule } from '../../src/server/schedule'
import { withMiddleware } from '../../src/middlewares/withMiddleware'
export default withMiddleware(async (req, res) => {
const period = req.query.period
const semester = req.query.semester
const data = await findSchedule(req.collection, period, semester)
if (data) {
res.json(data)
} else {
res.status(404)
res.json({
error: `Schedule ${period}:${semester} not found`
})
}
})
-14
View File
@@ -1,14 +0,0 @@
{
"compilerOptions": {
"noEmitOnError": false,
"target": "es5",
"module": "commonjs",
"jsx": "preserve",
"outDir": "./build",
"strict": false,
"esModuleInterop": true
},
"include": [
"src/**/*.ts"
]
}
-45
View File
@@ -1,45 +0,0 @@
import Koa from 'koa'
import next from 'next'
import { get } from 'koa-route'
import { connect } from './core/db'
import Page from './core/Page'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const port = parseInt(process.env.PORT, 10) || 3000
app.prepare().then(async () => {
try {
await connect()
} catch (e) {
console.error(e.message)
process.exit(1)
}
const pages = await Page.find({})
const pagesUrls = pages.map(x => x.url)
const server = new Koa()
server.use(async (ctx, next) => {
ctx.res.statusCode = 200
await next()
})
server.use(get('*', async ctx => {
const url = ctx.req.url
if (pagesUrls.includes(url)) {
await app.render(ctx.req, ctx.res, '/next', ctx.query)
} else {
await handle(ctx.req, ctx.res)
}
ctx.respond = false
}))
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`)
})
})