Skip to content

Commit

Permalink
chore: init
Browse files Browse the repository at this point in the history
  • Loading branch information
shuta13 committed Jan 1, 2024
0 parents commit 7af0704
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/continuous_delivery.yaml
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 }}
9 changes: 9 additions & 0 deletions .gitignore
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
26 changes: 26 additions & 0 deletions README.md
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
```
14 changes: 14 additions & 0 deletions package.json
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"
}
}
8 changes: 8 additions & 0 deletions src/api/index.ts
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;
17 changes: 17 additions & 0 deletions src/api/post.ts
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;
47 changes: 47 additions & 0 deletions src/index.ts
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;
8 changes: 8 additions & 0 deletions src/type/contentful.ts
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>;
};
6 changes: 6 additions & 0 deletions src/type/env.ts
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;
};
8 changes: 8 additions & 0 deletions src/type/hono.d.ts
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;
}
}
17 changes: 17 additions & 0 deletions tsconfig.json
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"
},
}
2 changes: 2 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "meguro-es-backend"
compatibility_date = "2023-01-01"

0 comments on commit 7af0704

Please sign in to comment.