-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(functions): split out the handler and clarify a few bits
- Loading branch information
Showing
1 changed file
with
39 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |