Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Dec 3, 2024
0 parents commit 957fd6b
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Docker Build

on:
push:
branches:
- master
workflow_dispatch:

permissions:
packages: write
contents: write

env:
IMAGE_TAG: ghcr.io/yeecord/api-proxy:latest
BUN_VERSION: 1.1.38

jobs:
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Log in to ghcr
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: action.Dockerfile
build-args: |
BUN_VERSION=${{ env.BUN_VERSION }}
secrets: |
"NPM_TOKEN=${{ secrets.GITHUB_TOKEN }}"
175 changes: 175 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# api-proxy

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run src/index.ts
```

This project was created using `bun init` in bun v1.1.38. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
23 changes: 23 additions & 0 deletions action.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ARG BUN_VERSION latest

FROM oven/bun:${BUN_VERSION:-1}-alpine AS base

WORKDIR /app

FROM base AS installer

COPY package.json bun.lockb ./

RUN bun install --frozen-lockfile --production

FROM base AS runner

# Install curl for healthcheck
RUN apk update && apk add curl --no-cache

COPY --from=installer /app/node_modules node_modules
COPY . .

ENTRYPOINT ["bun", "run"]

CMD ["start"]
Binary file added bun.lockb
Binary file not shown.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "api-proxy",
"module": "src/index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "^1.1.14"
},
"scripts": {
"start": "bun run src/index.ts"
},
"peerDependencies": {
"typescript": "^5.7.2"
},
"dependencies": {
"file-system-cache": "^3.0.0-alpha.8"
}
}
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { handleApiRequest } from "./routes/api";

Bun.serve({
fetch(request) {
const url = new URL(request.url);

if (url.pathname.startsWith("/api/")) {
return handleApiRequest(request, url);
}

return new Response("Not found", { status: 404 });
},
port: 3000,
});
49 changes: 49 additions & 0 deletions src/routes/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { cache, createCacheKey } from "../utils";

interface CachedResponse {
headers: [string, string][];
body: string | null;
}

export async function handleApiRequest(request: Request, url: URL) {
url.hostname = "discord.com";
url.port = "443";
url.protocol = "https:";

request.headers.set("Host", "discord.com");

if (request.method !== "GET") {
return fetch(url.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
});
}

const cacheKey = createCacheKey(request);

const existing = (await cache.get(cacheKey)) as CachedResponse | undefined;

if (existing) {
return new Response(existing.body, {
headers: new Headers(existing.headers),
});
}

const response = await fetch(url.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
});

const content = response.status === 204 ? null : await response.text();

await cache.set(cacheKey, {
headers: [...response.headers.entries()],
body: content,
} satisfies CachedResponse);

return new Response(content, {
headers: response.headers,
});
}
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Cache } from "file-system-cache";

export function createCacheKey(request: Request) {
return `${request.method} ${request.url} ${request.headers.get(
"Authorization"
)}`;
}

export const cache = new Cache({
hash: "sha1",
ttl: 60 * 5,
});
26 changes: 26 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}

0 comments on commit 957fd6b

Please sign in to comment.