Skip to content

Commit

Permalink
Introduce Favicon Support (#80)
Browse files Browse the repository at this point in the history
* Update linting

* Introduce Favicon and Last Deploy
  • Loading branch information
lucemans authored Aug 8, 2022
1 parent 97e81d9 commit f4cd36d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"rules": {
"prefer-at": "off",
"curly": ["error", "multi-or-nest"]
"curly": ["error", "multi-or-nest"],
"unused-imports/no-unused-vars": "off"
}
}
31 changes: 30 additions & 1 deletion src/routes/api/apps/[app]/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FastifyPluginAsync } from 'fastify';
import { generateSunflake } from 'sunflake';
import { CACHE } from '../../../../cache';

import { DB } from '../../../../database';
import { SafeError } from '../../../../util/error/SafeError';
import { AppEntryDeleteRoute } from './delete';
import { AppDeploysRoute } from './deploys';
import { AppEntryLinkRoute } from './link';
Expand All @@ -13,6 +15,7 @@ export type AppIDParameters = {
app_id: string;
};
};

export const AppEntryRoute: FastifyPluginAsync = async (router, _options) => {
router.get<AppIDParameters>('/', async (_request, reply) => {
const parameters = _request.params;
Expand All @@ -22,7 +25,33 @@ export const AppEntryRoute: FastifyPluginAsync = async (router, _options) => {
app_id: parameters.app_id,
});

reply.send(app);
if (!app) throw new SafeError(404, '', 'no_app');

const [last_deploy] = await DB.selectFrom(
'deployments',
['deploy_id'],
{ app_id: app?.app_id },
'ORDER BY deploy_id DESC'
);

const returner: object[] = [];

if (last_deploy) {
returner.push({
last_deploy: last_deploy.deploy_id,
preview_url: '/api/image/deploy/' + last_deploy.deploy_id,
});

const favicon_url = await CACHE.GET(
`favicon:${last_deploy.deploy_id}`
);

if (favicon_url) {
returner.push({ favicon_url });
}
}

reply.send(Object.assign(app, ...returner));
});

router.register(AppEntryLinkRoute, { prefix: '/link' });
Expand Down
16 changes: 16 additions & 0 deletions src/routes/api/apps/ls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FastifyPluginAsync } from 'fastify';
import { generateSunflake } from 'sunflake';

import { CACHE } from '../../../cache';
import { DB } from '../../../database';
import { useAuth } from '../../../util/http/useAuth';
import { log } from '../../../util/logging';
Expand Down Expand Up @@ -36,9 +37,24 @@ export const AppLsRoute: FastifyPluginAsync = async (router, _options) => {

if (!last_deploy) return app;

const favicon_url = await CACHE.GET(
`favicon:${last_deploy.deploy_id}`
);

if (!favicon_url) {
return {
...app,
last_deploy: last_deploy.deploy_id,
preview_url:
'/api/image/deploy/' + last_deploy.deploy_id,
};
}

return {
...app,
last_deploy: last_deploy.deploy_id,
preview_url: '/api/image/deploy/' + last_deploy.deploy_id,
favicon_url,
};
})
);
Expand Down

0 comments on commit f4cd36d

Please sign in to comment.