Skip to content

Commit

Permalink
add an uptime endpoint to prevent to go down a backend service on ren…
Browse files Browse the repository at this point in the history
…der.com cuz of limitation for a free tier
  • Loading branch information
stasguma committed May 1, 2024
1 parent 6603ec9 commit 4972147
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
1 change: 0 additions & 1 deletion db.json

This file was deleted.

17 changes: 15 additions & 2 deletions mock-config-server/mock-server.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const mockServerConfig = {
rest: {
baseUrl: '/api',
configs: [
{
path: '/uptime',
method: 'get',
routes: [
{
data: { success: 'ok' },
},
],
},
{
path: '/login',
method: 'post',
Expand Down Expand Up @@ -112,10 +121,14 @@ const mockServerConfig = {
request: (params) => {
console.log('request')
},
response: (data, {getHeader, getHeaders, request, setStatusCode}) => {
response: (data, {request, setStatusCode}) => {
console.log(data)
console.log('originalUrl: ', request.originalUrl)
if (!request.header('Authorization') && !request.originalUrl.includes('login')) {
if (
!request.header('Authorization')
&& !request.originalUrl.includes('login')
&& !request.originalUrl.includes('uptime')
) {
setStatusCode(401);
return { error: 'Unauthorized', message: 'You are not authorized to access this resource' };
}
Expand Down
7 changes: 6 additions & 1 deletion src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect } from 'react';

import { AppRouterProvider } from '@/app/providers/router';
import { useAppDispatch } from '@/shared/model';
import { initSession } from '@/entities/Session';
import { initSession, useUptimeQuery } from '@/entities/Session';

const App: FC = () => {
const dispatch = useAppDispatch();
Expand All @@ -13,6 +13,11 @@ const App: FC = () => {
dispatch(initSession());
}, [dispatch]);

/** this endpoint only need cuz of backend service go down after 15 min idle on render.com */
useUptimeQuery(null, {
pollingInterval: 14 * 1000 * 60, // 14 minutes
});

return (
<div className="app">
<AppRouterProvider />
Expand Down
11 changes: 10 additions & 1 deletion src/entities/Session/api/sessionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export const sessionApi = baseApi.injectEndpoints({
invalidatesTags: [SESSION_TAG],
// transformResponse: (response: { data: ILoginResponse; }, meta, arg) => response.data,
}),
/** this endpoint only need cuz of backend service go down after 15 min idle on render.com */
uptime: builder.query<void, null>({
query: () => ({
url: `uptime`,
method: 'GET',
}),
providesTags: [SESSION_TAG],
// transformResponse: (response: { data: ILoginResponse; }, meta, arg) => response.data,
}),
}),
});

export const { useLoginMutation } = sessionApi;
export const { useLoginMutation, useUptimeQuery } = sessionApi;
2 changes: 1 addition & 1 deletion src/entities/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export {
selectIsAuth,
selectError,
} from './model/slice/sessionSlice';
export { sessionApi } from './api/sessionApi';
export { sessionApi, useUptimeQuery } from './api/sessionApi';
export { sessionHandlers } from './api/__mocks__/sessionHandlers';

0 comments on commit 4972147

Please sign in to comment.