-
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
0 parents
commit 7af0704
Showing
12 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
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,35 @@ | ||
name: Continuous Delivery | ||
|
||
on: [workflow_dispatch] | ||
|
||
jobs: | ||
deploy: | ||
name: deploy | ||
runs-on: ubuntu-latest | ||
|
||
permissions: | ||
contents: read | ||
deployments: write | ||
|
||
strategy: | ||
matrix: | ||
node-version: ["18"] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: volta-cli/action@v4 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Install | ||
run: npm ci . | ||
|
||
- name: Deploy | ||
uses: cloudflare/wrangler-action@v3 | ||
with: | ||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
env: | ||
WEB_PROD_URL: https://meguro.es | ||
CONTENTFUL_SPACE_ID: ${{ secrets.CONTENTFUL_SPACE_ID }} | ||
CONTENTFUL_ACCESS_TOKEN: ${{ secrets.CONTENTFUL_ACCESS_TOKEN }} |
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,9 @@ | ||
node_modules | ||
dist | ||
.wrangler | ||
.dev.vars | ||
|
||
# Change them to your taste: | ||
package-lock.json | ||
yarn.lock | ||
pnpm-lock.yaml |
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,26 @@ | ||
# `backend` | ||
|
||
## Setup | ||
|
||
wranger で `wranger.toml` を生成し、以下を追記してください。 | ||
|
||
```toml | ||
[vars] | ||
WEB_LOCAL_URL = "http://localhost:3000" | ||
WEB_PROD_URL = "https://meguro.es" | ||
CONTENTFUL_SPACE_ID = "XXXX" | ||
CONTENTFUL_ACCESS_TOKEN = "XXX" | ||
``` | ||
|
||
`CONTENTFUL_SPACE_ID` と `CONTENTFUL_ACCESS_TOKEN` は、値を管理者から受け取って置き換えてください。 | ||
|
||
## Quick Start | ||
|
||
``` | ||
npm ci . | ||
npm run dev | ||
``` | ||
|
||
``` | ||
npm run deploy | ||
``` |
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,14 @@ | ||
{ | ||
"scripts": { | ||
"dev": "wrangler dev src/index.ts --port 5432", | ||
"deploy": "wrangler deploy --minify src/index.ts" | ||
}, | ||
"dependencies": { | ||
"hono": "^3.11.12" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20231218.0", | ||
"prettier": "^3.1.1", | ||
"wrangler": "^3.22.0" | ||
} | ||
} |
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 @@ | ||
import { Hono } from "hono"; | ||
import post from "./post"; | ||
|
||
const app = new Hono(); | ||
|
||
app.route("/post", post); | ||
|
||
export default app; |
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,17 @@ | ||
import { Hono } from "hono"; | ||
|
||
const app = new Hono(); | ||
|
||
app.get("/", async (c) => { | ||
const client = c.get("contentful"); | ||
const { limit, skip } = c.req.query(); | ||
const post = await client.getEntries({ | ||
content_type: "post", | ||
order: "-fields.createdAt", | ||
...(limit && { limit }), | ||
...(skip && { skip }), | ||
}); | ||
return c.json(post); | ||
}); | ||
|
||
export default app; |
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,47 @@ | ||
import { Hono } from "hono"; | ||
import { cors } from "hono/cors"; | ||
import { env } from "hono/adapter"; | ||
import { secureHeaders } from "hono/secure-headers"; | ||
import api from "./api"; | ||
import { Env } from "./type/env"; | ||
import { Contentful } from "./type/contentful"; | ||
|
||
const app = new Hono(); | ||
|
||
app.use("*", secureHeaders()); | ||
|
||
app.use("/api/*", async (c, next) => { | ||
const { WEB_LOCAL_URL, WEB_PROD_URL } = env<Env>(c); | ||
/** @see https://github.com/honojs/hono/issues/895#issuecomment-1431012601 */ | ||
const middleware = cors({ | ||
origin: [WEB_LOCAL_URL || "", WEB_PROD_URL], | ||
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"], | ||
allowHeaders: ["Content-Type", "Authorization"], | ||
}); | ||
await middleware(c, next); | ||
}); | ||
|
||
app.use("/api/*", async (c, next) => { | ||
const { CONTENTFUL_SPACE_ID, CONTENTFUL_ACCESS_TOKEN } = env<Env>(c); | ||
const contentful: Contentful = { | ||
getEntries: async (queryObject) => { | ||
const query = new URLSearchParams(queryObject).toString(); | ||
const res = await fetch( | ||
`https://cdn.contentful.com/spaces/${CONTENTFUL_SPACE_ID}/entries?${query}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${CONTENTFUL_ACCESS_TOKEN}`, | ||
}, | ||
} | ||
); | ||
const data = await res.json(); | ||
return data; | ||
}, | ||
}; | ||
c.set("contentful", contentful); | ||
await next(); | ||
}); | ||
|
||
app.route("/api", api); | ||
|
||
export default app; |
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 @@ | ||
export type Contentful = { | ||
getEntries: (queryObject: { | ||
content_type?: string; | ||
order?: string; | ||
limit?: string; | ||
skip?: string; | ||
}) => Promise<unknown>; | ||
}; |
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,6 @@ | ||
export type Env = { | ||
WEB_LOCAL_URL?: string; | ||
WEB_PROD_URL: string; | ||
CONTENTFUL_SPACE_ID: string; | ||
CONTENTFUL_ACCESS_TOKEN: string; | ||
}; |
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 @@ | ||
import { Contentful } from "./contentful"; | ||
|
||
declare module "hono" { | ||
/** @see https://github.com/honojs/hono/issues/414#issuecomment-1547014723 */ | ||
interface ContextVariableMap { | ||
contentful: Contentful; | ||
} | ||
} |
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,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"lib": [ | ||
"esnext" | ||
], | ||
"types": [ | ||
"@cloudflare/workers-types" | ||
], | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "hono/jsx" | ||
}, | ||
} |
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,2 @@ | ||
name = "meguro-es-backend" | ||
compatibility_date = "2023-01-01" |