| import express from 'express' |
| import { createProxyMiddleware } from 'http-proxy-middleware' |
|
|
| import events from '@/events/middleware' |
| import anchorRedirect from '@/rest/api/anchor-redirect' |
| import aiSearch from '@/search/middleware/ai-search' |
| import aiSearchLocalProxy from '@/search/middleware/ai-search-local-proxy' |
| import search from '@/search/middleware/search-routes' |
| import pageList from '@/article-api/middleware/pagelist' |
| import article from '@/article-api/middleware/article' |
| import webhooks from '@/webhooks/middleware/webhooks' |
| import { ExtendedRequest } from '@/types' |
| import { noCacheControl } from './cache-control' |
|
|
| const router = express.Router() |
|
|
| router.use('/events', events) |
| router.use('/webhooks', webhooks) |
| router.use('/anchor-redirect', anchorRedirect) |
| router.use('/pagelist', pageList) |
| router.use('/article', article) |
|
|
| |
| |
| |
| |
| |
| |
| if (process.env.CSE_COPILOT_ENDPOINT || process.env.NODE_ENV === 'test') { |
| router.use('/ai-search', aiSearch) |
| } else { |
| console.log( |
| 'Proxying AI Search requests to docs.github.com. To use the cse-copilot endpoint, set the CSE_COPILOT_ENDPOINT environment variable.', |
| ) |
| router.use(aiSearchLocalProxy) |
| } |
| if (process.env.ELASTICSEARCH_URL) { |
| router.use('/search', search) |
| } else { |
| router.use( |
| '/search', |
| createProxyMiddleware({ |
| target: 'https://docs.github.com', |
| changeOrigin: true, |
| pathRewrite(path, req: ExtendedRequest) { |
| return req.originalUrl |
| }, |
| }), |
| ) |
| } |
|
|
| |
| |
| |
| router.get('/cookies', (req, res) => { |
| noCacheControl(res) |
| const cookies = { |
| isStaff: Boolean(req.cookies?.staffonly?.startsWith('yes')) || false, |
| } |
| res.json(cookies) |
| }) |
|
|
| |
| router.get('/', (req, res) => { |
| res.status(404).json({ error: `${req.path} not found` }) |
| }) |
|
|
| router.get('/*path', (req, res) => { |
| res.status(404).json({ error: `${req.path} not found` }) |
| }) |
|
|
| export default router |
|
|