-
-
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.
- Loading branch information
Showing
14 changed files
with
3,179 additions
and
2,280 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Deploy Hono Worker | ||
on: | ||
push: | ||
branches: | ||
- master | ||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 60 | ||
needs: test | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Build & Deploy Worker | ||
uses: cloudflare/wrangler-action@v3 | ||
with: | ||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
workingDirectory: "new-functions" |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,62 +1,63 @@ | ||
import * as functions from "firebase-functions"; | ||
import * as functions from "firebase-functions/v2"; | ||
import * as FT from "../../website/src/types/functions-types"; | ||
import { receiveSubmissions } from "./submissions"; | ||
import lingdocsAuth from "./middleware/lingdocs-auth"; | ||
import publish from "./publish"; | ||
|
||
export const publishDictionary = functions | ||
.runWith({ | ||
const couchdbUrl = functions.params.defineString("ABC"); | ||
console.log({ couchdb: couchdbUrl.value() }); | ||
|
||
export const publishDictionary = functions.https.onRequest( | ||
{ | ||
timeoutSeconds: 525, | ||
memory: "2GB", | ||
}) | ||
.https.onRequest( | ||
lingdocsAuth( | ||
async ( | ||
req, | ||
res: functions.Response<FT.PublishDictionaryResponse | FT.FunctionError> | ||
) => { | ||
if (req.user.level !== "editor") { | ||
res.status(403).send({ ok: false, error: "403 forbidden" }); | ||
return; | ||
} | ||
try { | ||
const response = await publish(); | ||
res.send(response); | ||
} catch (e) { | ||
// @ts-ignore | ||
res.status(500).send({ ok: false, error: e.message }); | ||
} | ||
memory: "2GiB", | ||
}, | ||
lingdocsAuth( | ||
async ( | ||
req, | ||
res // : functions.Response<FT.PublishDictionaryResponse | FT.FunctionError> | ||
) => { | ||
if (req.user.level !== "editor") { | ||
res.status(403).send({ ok: false, error: "403 forbidden" }); | ||
return; | ||
} | ||
try { | ||
const response = await publish(); | ||
res.send(response); | ||
} catch (e) { | ||
// @ts-ignore | ||
res.status(500).send({ ok: false, error: e.message }); | ||
} | ||
) | ||
); | ||
} | ||
) | ||
); | ||
|
||
export const submissions = functions | ||
.runWith({ | ||
export const submissions = functions.https.onRequest( | ||
{ | ||
timeoutSeconds: 60, | ||
memory: "1GB", | ||
}) | ||
.https.onRequest( | ||
lingdocsAuth( | ||
async ( | ||
req, | ||
res: functions.Response<FT.SubmissionsResponse | FT.FunctionError> | ||
) => { | ||
if (!Array.isArray(req.body)) { | ||
res.status(400).send({ | ||
ok: false, | ||
error: "invalid submission", | ||
}); | ||
return; | ||
} | ||
const suggestions = req.body as FT.SubmissionsRequest; | ||
try { | ||
const response = await receiveSubmissions(suggestions, true); // req.user.level === "editor"); | ||
// TODO: WARN IF ANY OF THE EDITS DIDN'T HAPPEN | ||
res.send(response); | ||
} catch (e) { | ||
// @ts-ignore | ||
res.status(500).send({ ok: false, error: e.message }); | ||
} | ||
memory: "1GiB", | ||
}, | ||
lingdocsAuth( | ||
async ( | ||
req, | ||
res // : functions.Response<FT.SubmissionsResponse | FT.FunctionError> | ||
) => { | ||
if (!Array.isArray(req.body)) { | ||
res.status(400).send({ | ||
ok: false, | ||
error: "invalid submission", | ||
}); | ||
return; | ||
} | ||
const suggestions = req.body as FT.SubmissionsRequest; | ||
try { | ||
const response = await receiveSubmissions(suggestions, true); // req.user.level === "editor"); | ||
// TODO: WARN IF ANY OF THE EDITS DIDN'T HAPPEN | ||
res.send(response); | ||
} catch (e) { | ||
// @ts-ignore | ||
res.status(500).send({ ok: false, error: e.message }); | ||
} | ||
) | ||
); | ||
} | ||
) | ||
); |
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,43 +1,63 @@ | ||
import cors from "cors"; | ||
import fetch from "node-fetch"; | ||
import type { https, Response } from "firebase-functions"; | ||
import * as FT from "../../../website/src/types/functions-types"; | ||
import type { LingdocsUser } from "../../../website/src/types/account-types"; | ||
// unfortunately have to comment out all this typing because the new version | ||
// of firebase-functions doesn't include it? | ||
// import type { https, Response } from "firebase-functions"; | ||
// import * as FT from "../../../website/src/types/functions-types"; | ||
// import type { LingdocsUser } from "../../../website/src/types/account-types"; | ||
|
||
const useCors = cors({ credentials: true, origin: /\.lingdocs\.com$/ }); | ||
|
||
interface ReqWUser extends https.Request { | ||
user: LingdocsUser; | ||
} | ||
// interface ReqWUser extends https.Request { | ||
// user: LingdocsUser; | ||
// } | ||
|
||
/** | ||
* creates a handler to pass to a firebase https.onRequest function | ||
* creates a handler to pass to a firebase https.onRequest function | ||
* | ||
*/ | ||
export default function makeHandler(toRun: (req: ReqWUser, res: Response<FT.FunctionResponse>) => any | Promise<any>) { | ||
return function(reqPlain: https.Request, resPlain: Response<any>) { | ||
useCors(reqPlain, resPlain, async () => { | ||
const { req, res } = await authorize(reqPlain, resPlain); | ||
if (!req) { | ||
res.status(401).send({ ok: false, error: "unauthorized" }); | ||
return; | ||
}; | ||
toRun(req, res); | ||
return; | ||
}); | ||
} | ||
export default function makeHandler( | ||
toRun: ( | ||
req: any, //ReqWUser, | ||
res: any /*Response<FT.FunctionResponse> */ | ||
) => any | Promise<any> | ||
) { | ||
return function ( | ||
reqPlain: any /* https.Request */, | ||
resPlain: any /* Response<any> */ | ||
) { | ||
useCors(reqPlain, resPlain, async () => { | ||
const { req, res } = await authorize(reqPlain, resPlain); | ||
if (!req) { | ||
res.status(401).send({ ok: false, error: "unauthorized" }); | ||
return; | ||
} | ||
toRun(req, res); | ||
return; | ||
}); | ||
}; | ||
} | ||
|
||
async function authorize(req: https.Request, res: Response<any>): Promise<{ req: ReqWUser | null, res: Response<FT.FunctionResponse> }> { | ||
const { headers: { cookie }} = req; | ||
if (!cookie) { | ||
return { req: null, res }; | ||
} | ||
const r = await fetch("https://account.lingdocs.com/api/user", { headers: { cookie }}); | ||
const { ok, user } = await r.json(); | ||
if (ok === true && user) { | ||
req.user = user; | ||
return { req: req as ReqWUser, res }; | ||
} | ||
async function authorize( | ||
req: any /* https.Request*/, | ||
res: any /*Response<any>*/ | ||
): Promise<{ | ||
req: any; // ReqWUser | null; | ||
res: any /*Response<FT.FunctionResponse>*/; | ||
}> { | ||
const { | ||
headers: { cookie }, | ||
} = req; | ||
if (!cookie) { | ||
return { req: null, res }; | ||
} | ||
} | ||
const r = await fetch("https://account.lingdocs.com/api/user", { | ||
headers: { cookie }, | ||
}); | ||
const { ok, user } = await r.json(); | ||
if (ok === true && user) { | ||
req.user = user; | ||
return { req: req /* as ReqWUser*/, res }; | ||
} | ||
return { req: null, res }; | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# prod | ||
dist/ | ||
|
||
# dev | ||
.yarn/ | ||
!.yarn/releases | ||
.vscode/* | ||
!.vscode/launch.json | ||
!.vscode/*.code-snippets | ||
.idea/workspace.xml | ||
.idea/usage.statistics.xml | ||
.idea/shelf | ||
|
||
# deps | ||
node_modules/ | ||
.wrangler | ||
|
||
# env | ||
.env | ||
.env.production | ||
.dev.vars | ||
|
||
# logs | ||
logs/ | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
# misc | ||
.DS_Store |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
``` | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
``` | ||
npm run deploy | ||
``` |
Oops, something went wrong.