Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
refactor: use index file in route folder to do group imports (#2343)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCharrier authored Jan 16, 2024
1 parent 77bf617 commit b320cba
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 38 deletions.
22 changes: 22 additions & 0 deletions src/config/jwt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import routes from '@/routes/routes';

export const PUBLIC_ROUTES = [
'/login',
routes.LOGIN_API,
'/signin',
routes.SIGNIN_API,
'/marrainage/accept',
'/marrainage/decline',
'/notifications/github',
routes.WHAT_IS_GOING_ON_WITH_MEMBER,
routes.WHAT_IS_GOING_ON_WITH_MEMBER_SIMPLE,
routes.WHAT_IS_GOING_ON_WITH_MEMBER_WITH_TYPO,
routes.PULL_REQUEST_GET_PRS,
routes.ONBOARDING,
routes.ONBOARDING_API,
routes.ONBOARDING_ACTION,
/api\/public\/users\/*/,
/hook\/*/,
/onboardingSuccess\/*/,
/api\/public\/account\/base-info\/*/,
];
59 changes: 22 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ import { rateLimiter } from './middlewares/rateLimiter';
import { getJwtTokenForUser, getToken } from '@/helpers/session';
import getAllIncubators from './controllers/incubatorController/api/getAllIncubators';
import getAllSponsors from './controllers/sponsorController/api/getAllSponsors';
import { userRouter, userApiRouter, userPublicApiRouter } from './routes/users';
import { marrainageRouter } from './routes/marrainage';
import { accountRouter } from './routes/account';
import { startupRouter } from './routes/startups';
import { communityRouter } from './routes/community';
import { adminRouter } from './routes/admin';
import { authRouter } from './routes/auth';
import { diagnosticRouter } from './routes/diagnostic';
import { badgeRouter } from './routes/badge';
import { newsletterRouter } from './routes/newsletter';
import setupStaticFiles from './routes/staticFiles';
import { onboardingRouter } from './routes/onboarding';
import { mapRouter } from './routes/map';
import {
accountRouter,
adminRouter,
authRouter,
badgeRouter,
communityRouter,
diagnosticRouter,
userRouter,
userApiRouter,
userPublicApiRouter,
mapRouter,
marrainageRouter,
newsletterRouter,
onboardingRouter,
setupStaticFiles,
startupRouter,
} from './routes';
import { corsOptions } from './utils/corsConfig';
import { errorHandler } from './middlewares/errorHandler';
import { setupSessionMiddleware } from './middlewares/sessionMiddleware';
import { PUBLIC_ROUTES } from './config/jwt.config';

export const app = express();
app.set('trust proxy', 1);
Expand All @@ -47,14 +52,14 @@ EventBus.init([...MARRAINAGE_EVENTS_VALUES]);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, './views/templates')); // the code is running in directory "dist".

// MIDDLEWARES
app.use(compression());
setupStaticFiles(app);
setupSessionMiddleware(app);
app.use(flash());
app.use(expressSanitizer());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(rateLimiter);

app.use(
expressjwt({
secret: config.secret,
Expand All @@ -63,39 +68,19 @@ app.use(
return getToken(req);
},
}).unless({
path: [
'/login',
routes.LOGIN_API,
'/signin',
routes.SIGNIN_API,
'/marrainage/accept',
'/marrainage/decline',
'/notifications/github',
routes.WHAT_IS_GOING_ON_WITH_MEMBER,
routes.WHAT_IS_GOING_ON_WITH_MEMBER_SIMPLE,
routes.WHAT_IS_GOING_ON_WITH_MEMBER_WITH_TYPO,
routes.PULL_REQUEST_GET_PRS,
routes.ONBOARDING,
routes.ONBOARDING_API,
routes.ONBOARDING_ACTION,
/api\/public\/users\/*/,
/hook\/*/,
/onboardingSuccess\/*/,
/api\/public\/account\/base-info\/*/,
],
path: PUBLIC_ROUTES,
})
);

// Save a token in cookie that expire after 7 days if user is logged
app.use((req: Request, res, next) => {
if (req.auth && req.auth.id) {
(req.session.token = getJwtTokenForUser(req.auth.id)), { sameSite: 'lax' };
}
next();
});

app.use(errorHandler);

//ROUTES
app.get('/', indexController.getIndex);
app.use(userRouter);
app.use(userApiRouter);
Expand All @@ -119,7 +104,7 @@ app.post(
githubNotificationController.processNotification
);

// INCUBTORS
// INCUBATORS
app.get(routes.API_PUBLIC_INCUBATORS_GET_ALL, getAllIncubators);

//sponsors
Expand Down
13 changes: 13 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export { authRouter } from './auth';
export { userRouter, userApiRouter, userPublicApiRouter } from './users';
export { accountRouter } from './account';
export { adminRouter } from './admin';
export { badgeRouter } from './badge';
export { communityRouter } from './community';
export { diagnosticRouter } from './diagnostic';
export { mapRouter } from './map';
export { marrainageRouter } from './marrainage';
export { newsletterRouter } from './newsletter';
export { onboardingRouter } from './onboarding';
export { startupRouter } from './startups';
export { setupStaticFiles } from './staticFiles';
2 changes: 1 addition & 1 deletion src/routes/staticFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ const setupStaticFiles = (app) => {
// ... autres configurations de fichiers statiques
};

export default setupStaticFiles;
export { setupStaticFiles };

0 comments on commit b320cba

Please sign in to comment.