Skip to content

Commit

Permalink
Fix cors
Browse files Browse the repository at this point in the history
* Fix cors to debug local instance
* Format config json
  • Loading branch information
K0IN committed Jul 22, 2023
1 parent 83b64af commit 7d5f982
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

strategy:
matrix:
platform: [linux/amd64, linux/arm/v6, linux/arm/v7, linux/arm64/v8]
platform: [linux/amd64, linux/arm64/v8] # linux/arm/v6, linux/arm/v7,

steps:
- name: Checkout repository
Expand Down
13 changes: 7 additions & 6 deletions app/backend/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
// "--unstable",
// "--inspect"
// "--allow-all"
"--unstable",
"--inspect-brk",
"--allow-all"
],
"args": [
"args": [
"run",
"--sub",
"mailto:a@a.com",
"--vapidkey",
"eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwiYWxnIjoiRVMyNTYiLCJ4IjoiUV92WlVXUExOUlFMRnU5QWRNaGRDQlFpY1FKamxYajVHZ2lwY19BS1E5USIsInkiOiJILXlDUF9hZ3FzRmpGMmgtZ2dNTTdVT1UxdktJN1JTcU1XSVhfZjBJekhnIiwiZCI6IjVXdzg1TnFxN09lY0pyaDN5MDl6a1JLWWR3TEhUVTVObjlNZUNqMkh6Y2MiLCJrZXlfb3BzIjpbInNpZ24iXSwiZXh0Ijp0cnVlfQ=="
"eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwiYWxnIjoiRVMyNTYiLCJ4IjoiUV92WlVXUExOUlFMRnU5QWRNaGRDQlFpY1FKamxYajVHZ2lwY19BS1E5USIsInkiOiJILXlDUF9hZ3FzRmpGMmgtZ2dNTTdVT1UxdktJN1JTcU1XSVhfZjBJekhnIiwiZCI6IjVXdzg1TnFxN09lY0pyaDN5MDl6a1JLWWR3TEhUVTVObjlNZUNqMkh6Y2MiLCJrZXlfb3BzIjpbInNpZ24iXSwiZXh0Ijp0cnVlfQ==",
"--cors"
],
// "attachSimplePort": 9229
"attachSimplePort": 9229
}
]
}
9 changes: 7 additions & 2 deletions app/backend/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
"version": "2.0.0",
"tasks": [
{
"runOptions": { "runOn": "folderOpen" },
"runOptions": {
"runOn": "folderOpen"
},
"label": "prepare",
"type": "process",
"command": "deno",
"args": ["cache", "main.ts"]
"args": [
"cache",
"main.ts"
]
}
]
}
9 changes: 5 additions & 4 deletions app/backend/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export async function serve(params: AppParameters, listen = true): Promise<Appli
const { port, vapidKey, sub, frontend, cors, sendkey, loginkey } = parsed.data;

const app = new Application();

if (cors) {
app.use(oakCors());
}

app.use(logger.logger, logger.responseTime);
app.use(apiRouter.routes(), apiRouter.allowedMethods());

Expand All @@ -37,10 +42,6 @@ export async function serve(params: AppParameters, listen = true): Promise<Appli
app.state.sendkey = sendkey;
app.state.loginkey = loginkey;

if (cors) {
app.use(oakCors());
}

console.log(`Listening on http://localhost:${port}/ Config: ${JSON.stringify(params)}`);

if (listen) {
Expand Down
8 changes: 0 additions & 8 deletions app/backend/types/apiresponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ import { ApiError } from "./failures.ts";
export function success<T>(data: T, additions?: unknown): Response {
const responseMetaData = Object.assign({
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
}
}, additions);
return new Response(JSON.stringify({ successful: true, data }), responseMetaData);
}

export function failure(error: ApiError, additions?: unknown): Response {
const responseMetaData = Object.assign({
status: 500,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
}
}, additions);
return new Response(JSON.stringify({ successful: false, error }), responseMetaData);
}
1 change: 0 additions & 1 deletion app/backend/util/commandvalidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export function validatePort(port: string) {
const portNumber = parseInt(port);
if (portNumber < 1 || portNumber > 65535) {
Expand Down
9 changes: 7 additions & 2 deletions app/backend/util/oakreturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { failure } from "../types/apiresponse.ts";


export function responseToContext(context: RouterContext<any, any, any>, response: Response) {
context.response.body = response.body;
context.response.body = response.body ?? {};
context.response.status = response.status;
context.response.headers = response.headers;
if (!context.response.headers) {
context.response.headers = new Headers();
}

context.response.headers.set('Content-Type', 'application/json');
context.response.headers.set('Cache-Control', 'no-cache');
}

export function toReturn(fn: (ctx: RouterContext<any, any, any>) => Promise<Response>) {
Expand Down

0 comments on commit 7d5f982

Please sign in to comment.