mirror of
https://github.com/CompeyDev/fxtwitter-docker.git
synced 2025-04-05 10:30:55 +01:00
Fix routing and logging
This commit is contained in:
parent
e4b9efd793
commit
85eb2cead1
3 changed files with 26 additions and 13 deletions
|
@ -2,6 +2,7 @@ import { Hono } from 'hono';
|
|||
import { statusRequest } from '../twitter/routes/status';
|
||||
import { profileRequest } from '../twitter/routes/profile';
|
||||
import { Strings } from '../../strings';
|
||||
import { Constants } from '../../constants';
|
||||
|
||||
export const api = new Hono();
|
||||
|
||||
|
@ -11,3 +12,6 @@ api.get('/:handle/status/:id/:language?', statusRequest);
|
|||
|
||||
api.get('/:handle', profileRequest);
|
||||
api.get('/robots.txt', async (c) => c.text(Strings.ROBOTS_TXT));
|
||||
|
||||
|
||||
api.all('*', async (c) => c.redirect(Constants.API_DOCS_URL, 302));
|
|
@ -26,18 +26,18 @@ export const getBaseRedirectUrl = (c: Context) => {
|
|||
return Constants.TWITTER_ROOT;
|
||||
};
|
||||
|
||||
/* Workaround for some dumb maybe-build time issue where statusRequest isn't ready or something because none of these trigger*/
|
||||
const tweetRequest = async (c: Context) => await statusRequest(c);
|
||||
|
||||
twitter.get('/status/:id', statusRequest);
|
||||
twitter.get('/:handle/status/:id', statusRequest);
|
||||
twitter.get('/:prefix/:handle/status/:id/:language?', statusRequest);
|
||||
twitter.get('/:prefix?/:handle?/:endpoint{status(es)?}/:id/:language?', tweetRequest);
|
||||
twitter.get(':handle?/:endpoint{status(es)?}/:id/:language?', tweetRequest);
|
||||
twitter.get(
|
||||
'/:prefix/:handle/status/:id/:mediaType{(photos?|videos?)}/:mediaNumber{[1-4]}/:language?',
|
||||
statusRequest
|
||||
'/:prefix?/:handle/:endpoint{status(es)?}/:id/:mediaType{(photos?|videos?)}/:mediaNumber{[1-4]}/:language?',
|
||||
tweetRequest
|
||||
);
|
||||
twitter.get('/:handle?/:endpoint{status(es)?}/:id/:language?', statusRequest);
|
||||
twitter.get(
|
||||
'/:handle?/:endpoint{status(es)?}/:id/:mediaType{(photos?|videos?)}/:mediaNumber{[1-4]}/:language?',
|
||||
statusRequest
|
||||
'/:handle/:endpoint{status(es)?}/:id/:mediaType{(photos?|videos?)}/:mediaNumber{[1-4]}/:language?',
|
||||
tweetRequest
|
||||
);
|
||||
|
||||
twitter.get('/version', versionRoute);
|
||||
|
@ -49,4 +49,6 @@ twitter.get('/robots.txt', async (c) => c.text(Strings.ROBOTS_TXT));
|
|||
twitter.get('/i/events/:id', genericTwitterRedirect);
|
||||
twitter.get('/hashtag/:hashtag', genericTwitterRedirect);
|
||||
|
||||
twitter.get('/:handle', profileRequest);
|
||||
twitter.get('/:handle', profileRequest);
|
||||
|
||||
twitter.all('*', async (c) => c.redirect(Constants.REDIRECT_URL, 302));
|
|
@ -25,8 +25,13 @@ const app = new Hono<{ Bindings: { TwitterProxy: Fetcher; AnalyticsEngine: Analy
|
|||
/* Override if in API_HOST_LIST. Note that we have to check full hostname for this. */
|
||||
if (Constants.API_HOST_LIST.includes(url.hostname)) {
|
||||
realm = 'api';
|
||||
console.log('API realm');
|
||||
} else if (Constants.STANDARD_DOMAIN_LIST.includes(baseHostName)) {
|
||||
console.log()
|
||||
realm = 'twitter';
|
||||
console.log('Twitter realm');
|
||||
} else {
|
||||
console.log(`Domain not assigned to realm, falling back to Twitter: ${url.hostname}`);
|
||||
}
|
||||
/* Defaults to Twitter realm if unknown domain specified (such as the *.workers.dev hostname or deprecated domain) */
|
||||
|
||||
|
@ -59,16 +64,18 @@ app.use('*', async (c, next) => {
|
|||
|
||||
app.onError((err, c) => {
|
||||
c.get('sentry').captureException(err);
|
||||
/* workaround for silly TypeScript things */
|
||||
const error = err as Error;
|
||||
console.error(error.stack);
|
||||
console.error(err.stack);
|
||||
c.status(200);
|
||||
c.header('cache-control', noCache);
|
||||
|
||||
return c.html(Strings.ERROR_HTML);
|
||||
});
|
||||
|
||||
// app.use('*', logger());
|
||||
const customLogger = (message: string, ...rest: string[]) => {
|
||||
console.log(message, ...rest);
|
||||
};
|
||||
|
||||
app.use('*', logger(customLogger));
|
||||
|
||||
app.use('*', async (c, next) => {
|
||||
console.log(`Hello from ⛅ ${c.req.raw.cf?.colo || 'UNK'}`);
|
||||
|
|
Loading…
Add table
Reference in a new issue