Skip to content

Commit

Permalink
Improve log and GH token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Apr 27, 2024
1 parent 6476899 commit 2a14573
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/core/GitHubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export async function getToken(repository: Repository) {
const span = trace.getTracer("ano-file").startSpan("GHUtils.getToken");
span.setAttribute("repoId", repository.repoId);
try {
// only check the token if the repo has been visited more than one day ago
if (
repository.model.source.accessToken &&
repository.model.lastView > new Date(Date.now() - 1000 * 60 * 60 * 24)
) {
return repository.model.source.accessToken;
}
if (repository.model.source.accessToken) {
if (await checkToken(repository.model.source.accessToken)) {
return repository.model.source.accessToken;
Expand All @@ -48,6 +55,8 @@ export async function getToken(repository: Repository) {
repository.owner.model.accessTokens?.github
);
if (check) {
repository.model.source.accessToken =
repository.owner.model.accessTokens?.github;
return repository.owner.model.accessTokens?.github;
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ export default async function start() {
keyGenerator,
});

app.use(
express.static(join("public"), {
etag: true,
lastModified: true,
maxAge: 3600, // 1h
})
);

app.use(function (req, res, next) {
const start = Date.now();
res.on("finish", function () {
const time = Date.now() - start;
console.log(`${req.method} ${join(req.baseUrl, req.url)} ${time}ms`);
});
next();
});

app.use("/github", rate, speedLimiter, connectionRouter);

// api routes
Expand Down Expand Up @@ -201,14 +218,6 @@ export default async function start() {
.get("/r/:repoId/?*", indexResponse)
.get("/repository/:repoId/?*", indexResponse);

app.use(
express.static(join("public"), {
etag: true,
lastModified: true,
maxAge: 3600, // 1h
})
);

app.get("*", indexResponse);

// start schedules
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ router.get(
// cache the file for 5min
res.header("Cache-Control", "max-age=300");
}
await repo.countView();
await f.send(res);
await repo.countView();
} catch (error) {
return handleError(error, res, req);
}
Expand Down
1 change: 0 additions & 1 deletion src/streamer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ router.post("/", async (req: express.Request, res: express.Response) => {
commit: commit,
getToken: () => token,
});
console.log(`[FILE] ${repoId}/${filePath}`);
const content = await source.getFileContentCache(
filePath,
repoId,
Expand Down

0 comments on commit 2a14573

Please sign in to comment.