Skip to content

Commit

Permalink
feat(app): add initial Render blueprint and hyper app
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 30, 2022
1 parent 3037352 commit 31a58a2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM denoland/deno:alpine

WORKDIR /app

# Prefer not to run as root.
USER deno

# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).
# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.
COPY deps.ts .
RUN deno cache deps.ts

# These steps will be re-run upon each file change in your working directory:
ADD . .
# Compile the app so that it doesn't need to be compiled each startup/entry.
RUN deno cache --reload mod.ts

CMD ["run", "-A", "mod.ts"]
30 changes: 30 additions & 0 deletions app/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// deno-lint-ignore-file no-explicit-any

import {
jwt,
type NextFunction,
type Opine,
type OpineRequest,
type OpineResponse,
} from './deps.ts';

export const authMiddleware = (secret: string) => (app: Opine) => {
app.use(jwt({ secret, algorithms: ['HS256'] }).unless({ path: ['/'] }));

app.use(
(
err: any,
_req: OpineRequest,
res: OpineResponse,
next: NextFunction,
): any => {
if (err.name === 'UnauthorizedError') {
return res.setStatus(401).send({ ok: false, msg: 'not authorized' });
}
// Trigger the next error handler
next(err);
},
);

return app;
};
12 changes: 12 additions & 0 deletions app/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export { expressjwt as jwt } from 'npm:express-jwt';

export { z } from 'https://deno.land/x/[email protected]/mod.ts';
export type {
NextFunction,
Opine,
OpineRequest,
OpineResponse,
} from 'https://deno.land/x/[email protected]/mod.ts';

export { default as hyper } from 'https://x.nest.land/[email protected]/mod.js';
export { default as app } from 'https://x.nest.land/[email protected]/mod.js';
17 changes: 17 additions & 0 deletions app/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { authMiddleware } from './auth.ts';
import { app, hyper, z } from './deps.ts';

function env(key: string): string {
const res = z.string().min(1).safeParse(Deno.env.get(key));
if (!res.success) {
console.error(`Error with ENV VAR: ${key}`);
throw res.error;
}
return res.data;
}

export default hyper({
app,
adapters: [],
middleware: [authMiddleware(env('SECRET'))],
});
16 changes: 16 additions & 0 deletions render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# If your hyper app exists in a sub-directory of your repo ie. monorepo,
# Set the path here to the root of your hyper configuration
# rootDir: apps/hyper

services:

# The Hyper Application
- type: web
name: hyper
plan: starter
env: docker
dockerfilePath: ./app/Dockerfile
dockerContext: ./app
envVars:
- key: SECRET
generateValue: true

0 comments on commit 31a58a2

Please sign in to comment.