rm next based api; rm custom next server; rm koa
This commit is contained in:
1 parent
d3b5b2d5c6
commit
c0b43f8afd
21 files changed
+13
-428
No files matched your search
+1
-9
@@ -4,8 +4,7 @@
|
|||||||
"version": "6.0.0",
|
"version": "6.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "ts-node --project server.tsconfig.json src/server.ts",
|
"start": "next dev",
|
||||||
"next-dev": "next dev",
|
|
||||||
"build": "next build && next export -o dist && npm run sitemap && npm run feed",
|
"build": "next build && next export -o dist && npm run sitemap && npm run feed",
|
||||||
"sitemap": "node scripts/sitemap.js",
|
"sitemap": "node scripts/sitemap.js",
|
||||||
"feed": "node scripts/feed.js",
|
"feed": "node scripts/feed.js",
|
||||||
@@ -20,11 +19,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.19.0",
|
"axios": "^0.19.0",
|
||||||
"classnames": "^2.2.6",
|
"classnames": "^2.2.6",
|
||||||
"koa": "^2.11.0",
|
|
||||||
"koa-route": "^3.1.0",
|
|
||||||
"lodash": "^4.17.15",
|
"lodash": "^4.17.15",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
"mongodb": "^2.1.14",
|
|
||||||
"next": "^9.2.2-canary.18",
|
"next": "^9.2.2-canary.18",
|
||||||
"react": "^16.12.0",
|
"react": "^16.12.0",
|
||||||
"react-dom": "^16.12.0",
|
"react-dom": "^16.12.0",
|
||||||
@@ -33,10 +29,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/classnames": "^2.2.9",
|
"@types/classnames": "^2.2.9",
|
||||||
"@types/koa": "^2.0.52",
|
|
||||||
"@types/koa-route": "^3.2.4",
|
|
||||||
"@types/lodash": "^4.14.149",
|
"@types/lodash": "^4.14.149",
|
||||||
"@types/mongodb": "^3.3.11",
|
|
||||||
"@types/node": "^12.12.12",
|
"@types/node": "^12.12.12",
|
||||||
"@types/react": "^16.9.12",
|
"@types/react": "^16.9.12",
|
||||||
"@zeit/next-css": "^1.0.1",
|
"@zeit/next-css": "^1.0.1",
|
||||||
@@ -50,7 +43,6 @@
|
|||||||
"mocha": "^6.2.2",
|
"mocha": "^6.2.2",
|
||||||
"node-sass": "^4.13.0",
|
"node-sass": "^4.13.0",
|
||||||
"should": "^8.2.2",
|
"should": "^8.2.2",
|
||||||
"ts-node": "^8.5.2",
|
|
||||||
"typescript": "^3.7.5"
|
"typescript": "^3.7.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,12 @@ import { Meta } from '../src/components/Meta'
|
|||||||
import { meta } from '../src/lib/meta'
|
import { meta } from '../src/lib/meta'
|
||||||
import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib'
|
import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib'
|
||||||
|
|
||||||
|
function array<T>(value: T | T[]) {
|
||||||
|
return Array.isArray(value)
|
||||||
|
? value
|
||||||
|
: [value]
|
||||||
|
}
|
||||||
|
|
||||||
const Index = props => (
|
const Index = props => (
|
||||||
<App
|
<App
|
||||||
menu={buildMenu(props.pageUrl, menuModel)}
|
menu={buildMenu(props.pageUrl, menuModel)}
|
||||||
@@ -30,13 +36,16 @@ const Index = props => (
|
|||||||
)
|
)
|
||||||
|
|
||||||
Index.getInitialProps = wrapInitialProps(async (ctx) => {
|
Index.getInitialProps = wrapInitialProps(async (ctx) => {
|
||||||
const pageUrl = ctx.req.url
|
const slug = '/' + array(ctx.query.slug).join('/')
|
||||||
const page = await requestGet(createApiUrl(`/api/page?page=${pageUrl}`), {})
|
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 : {}
|
const image = page.preview ? page.preview.artifacts.fb : {}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content: page.data,
|
content: page.data,
|
||||||
pageUrl,
|
pageUrl: slug,
|
||||||
title: page.title,
|
title: page.title,
|
||||||
meta: meta({
|
meta: meta({
|
||||||
title: page.title,
|
title: page.title,
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -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`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"noEmitOnError": false,
|
|
||||||
"target": "es5",
|
|
||||||
"module": "commonjs",
|
|
||||||
"jsx": "preserve",
|
|
||||||
"outDir": "./build",
|
|
||||||
"strict": false,
|
|
||||||
"esModuleInterop": true
|
|
||||||
},
|
|
||||||
"include": [
|
|
||||||
"src/**/*.ts"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -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}`)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in new issue
Block a user