Skip to content

Commit

Permalink
refactor(functions): split out the handler and clarify a few bits
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucktay committed Feb 10, 2024
1 parent 811b297 commit a70de1f
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import { runWith } from "firebase-functions";
import { Response } from "express";
import { Request, runWith } from "firebase-functions";

// Start writing Firebase Functions
// https://firebase.google.com/docs/functions/typescript

const goVCSBase = "https://github.com/jlucktay";
const goVCSBase: string = "https://github.com/jlucktay" as const;

export const goPackage = runWith({ memory: "128MB" }).https.onRequest(
(request, response) => {
const firstSlash = request.path.substring(1).indexOf("/");
let basePackage = request.path;

if (firstSlash > 0) {
basePackage = request.path.substring(0, firstSlash + 1);
}

// https://golang.org/cmd/go/#hdr-Remote_import_paths
// <meta name="go-import" content="import-prefix vcs repo-root">
const goImport: string[] = [
/* import-prefix */ request.hostname + basePackage,
/* vcs */ "git",
/* repo-root */ goVCSBase + basePackage,
];

const goImportMeta =
`<meta name="go-import" content="` + goImport.join(" ") + `">`;

// https://github.com/golang/gddo/wiki/Source-Code-Links
// <meta name="go-source" content="prefix home directory file">
const goSource: string[] = [
/* prefix */ request.hostname + basePackage,
/* home */ goVCSBase + basePackage,
/* directory */ goVCSBase + basePackage + "/tree/main{/dir}",
/* file */ goVCSBase + basePackage + "/tree/main{/dir}/{file}#L{line}",
];

const goSourceMeta =
`<meta name="go-source" content="` + goSource.join(" ") + `">`;

response.send(goImportMeta + "\n" + goSourceMeta + "\n");
},
myHandler,
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is the signature accepted by 'onRequest'.
export function myHandler(req: Request, resp: Response<any>): void {
const firstSlash = req.path.substring(1).indexOf("/");
let basePackage = req.path;

if (firstSlash > 0) {
basePackage = req.path.substring(0, firstSlash + 1);
}

// https://golang.org/cmd/go/#hdr-Remote_import_paths
// <meta name="go-import" content="import-prefix vcs repo-root">
const goImport: string[] = [
/* import-prefix */ req.hostname + basePackage,
/* vcs */ "git",
/* repo-root */ goVCSBase + basePackage,
];

const goImportMeta =
`<meta name="go-import" content="` + goImport.join(" ") + `">`;

// https://github.com/golang/gddo/wiki/Source-Code-Links
// <meta name="go-source" content="prefix home directory file">
const goSource: string[] = [
/* prefix */ req.hostname + basePackage,
/* home */ goVCSBase + basePackage,
/* directory */ goVCSBase + basePackage + "/tree/main{/dir}",
/* file */ goVCSBase + basePackage + "/tree/main{/dir}/{file}#L{line}",
];

const goSourceMeta =
`<meta name="go-source" content="` + goSource.join(" ") + `">`;

resp.send(goImportMeta + "\n" + goSourceMeta + "\n");
}

0 comments on commit a70de1f

Please sign in to comment.