Skip to content

Commit

Permalink
feat: CORS setting, read origin from environment variables (#38)
Browse files Browse the repository at this point in the history
- Add corsMiddlewareHandler to get origins info from the environment
variables.
- Remove the [vars] section in .toml
- Add .node-version to specific node-version for CI/CD and engine inside
package.json.
  • Loading branch information
ZL-Asica authored Nov 4, 2024
1 parent 939feac commit 41ba4c8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.11.0
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
[![Test by Github Action][github-test-badge]][github-test-link]
[![GitHub License][license-badge]][license-link]
[![Yarn Version][yarn-badge]][yarn-link] |
[![Hono][hono-badge]][hono-link]
[![Node js][node-badge]][node-link]
[![Yarn Version][yarn-badge]][yarn-link] |
[![Cloudflare][cloudflare-badge]][cloudflare-link]
[![Eslint][eslint-badge]][eslint-link]
[![Prettier][prettier-badge]][prettier-link]
Expand Down Expand Up @@ -117,8 +118,9 @@

[github-test-badge]: https://img.shields.io/github/actions/workflow/status/ZL-Asica/KumoAuth/auto-test.yml?logo=github&label=Test
[license-badge]: https://img.shields.io/github/license/ZL-Asica/KumoAuth
[hono-badge]: https://img.shields.io/github/package-json/dependency-version/ZL-Asica/KumoAuth/hono?label=Hono&color=E36002&logo=hono&logoColor=white&labelColor=E36002
[node-badge]: https://img.shields.io/github/package-json/engines/node/ZL-Asica/KumoAuth?label=&logo=node.js&color=339933&logoColor=white&labelColor=339933
[yarn-badge]: https://img.shields.io/github/package-json/packageManager/ZL-Asica/KumoAuth?label=&logo=yarn&logoColor=fff
[hono-badge]: https://img.shields.io/badge/Hono-E36002?logo=hono&logoColor=fff
[cloudflare-badge]: https://img.shields.io/badge/Cloudflare-F38020?logo=Cloudflare&logoColor=white
[eslint-badge]: https://img.shields.io/badge/eslint-4B32C3?logo=eslint&logoColor=white
[prettier-badge]: https://img.shields.io/badge/Prettier-F7B93E?logo=Prettier&logoColor=white
Expand All @@ -127,8 +129,9 @@

[github-test-link]: https://github.com/ZL-Asica/KumoAuth/actions/workflows/auto-test.yml
[license-link]: https://github.com/ZL-Asica/KumoAuth?tab=GPL-3.0-1-ov-file#readme
[yarn-link]: https://yarnpkg.com/
[hono-link]: https://hono.dev/
[node-link]: https://nodejs.org/
[yarn-link]: https://yarnpkg.com/
[cloudflare-link]: https://www.cloudflare.com/
[eslint-link]: https://eslint.org/
[prettier-link]: https://prettier.io/
11 changes: 7 additions & 4 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
[![Test by Github Action][github-test-badge]][github-test-link]
[![GitHub License][license-badge]][license-link]
[![Yarn Version][yarn-badge]][yarn-link] |
[![Hono][hono-badge]][hono-link]
[![Node js][node-badge]][node-link]
[![Yarn Version][yarn-badge]][yarn-link] |
[![Cloudflare][cloudflare-badge]][cloudflare-link]
[![Eslint][eslint-badge]][eslint-link]
[![Prettier][prettier-badge]][prettier-link]
Expand Down Expand Up @@ -113,12 +114,13 @@ Designed for small applications and personal projects, this system provides a se

Thank you for your interest and support! Feel free to suggest features or contribute to help us improve this project 🙌

<!-- Badge Links -->
!-- Badge Links -->

[github-test-badge]: https://img.shields.io/github/actions/workflow/status/ZL-Asica/KumoAuth/auto-test.yml?logo=github&label=Test
[license-badge]: https://img.shields.io/github/license/ZL-Asica/KumoAuth
[hono-badge]: https://img.shields.io/github/package-json/dependency-version/ZL-Asica/KumoAuth/hono?label=Hono&color=E36002&logo=hono&logoColor=white&labelColor=E36002
[node-badge]: https://img.shields.io/github/package-json/engines/node/ZL-Asica/KumoAuth?label=&logo=node.js&color=339933&logoColor=white&labelColor=339933
[yarn-badge]: https://img.shields.io/github/package-json/packageManager/ZL-Asica/KumoAuth?label=&logo=yarn&logoColor=fff
[hono-badge]: https://img.shields.io/badge/Hono-E36002?logo=hono&logoColor=fff
[cloudflare-badge]: https://img.shields.io/badge/Cloudflare-F38020?logo=Cloudflare&logoColor=white
[eslint-badge]: https://img.shields.io/badge/eslint-4B32C3?logo=eslint&logoColor=white
[prettier-badge]: https://img.shields.io/badge/Prettier-F7B93E?logo=Prettier&logoColor=white
Expand All @@ -127,8 +129,9 @@ Thank you for your interest and support! Feel free to suggest features or contri

[github-test-link]: https://github.com/ZL-Asica/KumoAuth/actions/workflows/auto-test.yml
[license-link]: https://github.com/ZL-Asica/KumoAuth?tab=GPL-3.0-1-ov-file#readme
[yarn-link]: https://yarnpkg.com/
[hono-link]: https://hono.dev/
[node-link]: https://nodejs.org/
[yarn-link]: https://yarnpkg.com/
[cloudflare-link]: https://www.cloudflare.com/
[eslint-link]: https://eslint.org/
[prettier-link]: https://prettier.io/
3 changes: 3 additions & 0 deletions example.dev.vars
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ JWT_EXPIRE_IN=30

# Cloudflare D1 Database Configuration
D1_DATABASE_ID=your-database-id

# CORS settings (comma separate if more than one)
CORS_ORIGIN=localhost,https://zla.app
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
"vitest": "^2.1.4",
"wrangler": "^3.84.1"
},
"engines": {
"node": ">=20.11"
},
"packageManager": "yarn@4.5.1"
}
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import auth from '@/auth'
import { corsMiddlewareHandler } from '@/middleware/cors'
import { notFound } from '@/middleware/not-found'
import { onError } from '@/middleware/on-error'
import { workerLogger } from '@/middleware/worker-logger'
Expand All @@ -9,9 +10,10 @@ import type { Context } from 'hono'
type Bindings = {
JWT_SECRET: string
JWT_EXPIRE_IN: string
CORS_ORIGIN: string
}

const app = new OpenAPIHono<{ Bindings: Bindings }>().route('/auth', auth)
const app = new OpenAPIHono<{ Bindings: Bindings }>()

// Add worker logger middleware to all routes
app.use(workerLogger)
Expand All @@ -27,6 +29,12 @@ app.notFound(notFound)
// On error
app.onError(onError)

// CORS middleware
app.use(corsMiddlewareHandler)

// Auth routes
app.route('/auth', auth)

// Set OpenAPI documentation
app.doc31('/doc', {
openapi: '3.1.0',
Expand Down
12 changes: 12 additions & 0 deletions src/middleware/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Context, MiddlewareHandler } from 'hono'
import { cors } from 'hono/cors'

export const corsMiddlewareHandler: MiddlewareHandler = async (
c: Context,
next
) => {
const corsMiddlewareHandler = cors({
origin: c.env.CORS_ORIGIN?.split(',').map((s: string) => s.trim()) || '*',
})
return corsMiddlewareHandler(c, next)
}
4 changes: 0 additions & 4 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ compatibility_date = "2024-10-22"

compatibility_flags = [ "nodejs_compat" ]

[vars]
JWT_SECRET = "${JWT_SECRET}"
JWT_EXPIRE_IN = "${JWT_EXPIRE_IN}"

# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Expand Down

0 comments on commit 41ba4c8

Please sign in to comment.