diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..cff060a --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +BACKEND_URL=https://graphql-engine.asis-be.orb.local/v1/graphql diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 68decb4..9962d5b 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -17,6 +17,7 @@ jobs: branch: 'main' gh-app-id: ${{ vars.BOT_APP_ID }} pr-comment-enabled: false + backend-url: ${{ vars.STG_BACKEND_URL }} secrets: cloudflare-account-id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} cloudflare-api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }} diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index c58c4e0..4ceed43 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -20,6 +20,7 @@ jobs: branch: 'preview' gh-app-id: ${{ vars.BOT_APP_ID }} pr-comment-enabled: true + backend-url: ${{ vars.DEV_BACKEND_URL }} secrets: cloudflare-account-id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} cloudflare-api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 720f3d5..923da1f 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -18,6 +18,7 @@ jobs: branch: 'main' gh-app-id: ${{ vars.BOT_APP_ID }} pr-comment-enabled: false + backend-url: ${{ vars.PROD_BACKEND_URL }} secrets: cloudflare-account-id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} cloudflare-api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }} diff --git a/.github/workflows/wc-deploy-cloudflare-pages.yml b/.github/workflows/wc-deploy-cloudflare-pages.yml index 47da8cb..8d9b627 100644 --- a/.github/workflows/wc-deploy-cloudflare-pages.yml +++ b/.github/workflows/wc-deploy-cloudflare-pages.yml @@ -15,6 +15,9 @@ on: pr-comment-enabled: required: true type: boolean + backend-url: + required: true + type: string secrets: gh-app-private-key: required: true @@ -43,6 +46,9 @@ jobs: - name: Install deps run: bun install --frozen-lockfile + - name: Generate env + run: bun plop env -- --backendUrl "${{ inputs.backend-url }}" + - name: Build run: bun run build diff --git a/astro.config.mjs b/astro.config.mjs index e8da57f..3ed4de3 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,9 +1,11 @@ -import { defineConfig } from 'astro/config' - +import cloudflare from '@astrojs/cloudflare' import sitemap from '@astrojs/sitemap' +import { defineConfig } from 'astro/config' // https://astro.build/config export default defineConfig({ site: 'https://asis.quest', - integrations: [sitemap()] + integrations: [sitemap()], + output: 'server', + adapter: cloudflare() }) diff --git a/bun.lockb b/bun.lockb index 8cbb574..98b2073 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index c12f21f..5c74c11 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,9 @@ "plop": "plop" }, "dependencies": { + "@astrojs/cloudflare": "11.0.1", "@astrojs/sitemap": "3.1.6", - "astro": "4.11.3", - "sharp": "0.33.4" + "astro": "4.11.3" }, "devDependencies": { "@astrojs/check": "0.7.0", diff --git a/plopfile.mjs b/plopfile.mjs index 7775446..1932375 100644 --- a/plopfile.mjs +++ b/plopfile.mjs @@ -4,19 +4,40 @@ export default function (plop) { plop.setGenerator('sample', { description: 'sample', - prompts: [{ - type: 'input', - name: 'name', - message: 'sample name please' - }], + prompts: [ + { + type: 'input', + name: 'name', + message: 'sample name please' + } + ], actions: [ { type: 'addMany', templateFiles: 'templates/sample/**', destination: './sample/{{name}}', base: 'templates/sample', - abortOnFail: true, - }, + abortOnFail: true + } + ] + }) + + plop.setGenerator('env', { + description: 'env', + prompts: [ + { + type: 'input', + name: 'backendUrl', + message: 'backend url please' + } + ], + actions: [ + { + type: 'add', + templateFile: 'templates/env/.env.hbs', + path: '.env', + force: true + } ] - }); -}; + }) +} diff --git a/src/components/For.astro b/src/components/For.astro new file mode 100644 index 0000000..6eae88e --- /dev/null +++ b/src/components/For.astro @@ -0,0 +1,23 @@ +--- +import Show from './Show.astro'; + +interface Props { + each: Iterable; +} + +const { each } = Astro.props; +--- + +{ + (async function* () { + for await (const value of each) { + let html = await Astro.slots.render('default', [value]); + yield ; + yield '\n'; + } + })() +} + + + + diff --git a/src/components/Show.astro b/src/components/Show.astro new file mode 100644 index 0000000..ccb642f --- /dev/null +++ b/src/components/Show.astro @@ -0,0 +1,9 @@ +--- +interface Props { + when: T | number | boolean | undefined | null; +} + +const { when } = Astro.props; +--- + +{!!when ? : } diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 0000000..5038f93 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,88 @@ +import type { News } from '@/types.ts' + +/** + * Fetches the GraphQL API + * @param operationsDoc + * @param operationName + * @param variables + */ +async function fetchGraphQL( + operationsDoc: string, + operationName: string, + // biome-ignore lint/suspicious/noExplicitAny: + variables: Record +) { + const result = await fetch(`${import.meta.env.BACKEND_URL}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + query: operationsDoc, + variables, + operationName + }) + }) + return await result.json() +} + +/** + * Fetches the list of news + */ +export async function fetchNewsList(): Promise { + const { errors, data } = await fetchGraphQL( + ` +query GetNewsList { + news { + title + slug + publishedAt + excerpt + coverImageUrl + content + } +} +`, + 'GetNewsList', + {} + ) + + if (errors) { + // handle those errors like a pro + console.error(errors) + } + + console.log(data) + return data.news as News[] +} + +/** + * Fetches the list of news + */ +export async function fetchNewsBySlug(slug: string): Promise { + const { errors, data } = await fetchGraphQL( + ` +query GetNewsBySlug($slug: String) { + news(where: {slug: {_eq: $slug}}) { + title + slug + publishedAt + excerpt + coverImageUrl + content + } +} +`, + 'GetNewsBySlug', + { + slug + } + ) + + if (errors) { + // handle those errors like a pro + console.error(errors) + } + + console.log(data) + // biome-ignore lint/style/noNonNullAssertion: + return (data.news as News[])[0]! +} diff --git a/src/pages/404.astro b/src/pages/404.astro index 1a23ff9..961bd70 100644 --- a/src/pages/404.astro +++ b/src/pages/404.astro @@ -1,6 +1,8 @@ --- import Hero from '../components/Hero.astro' import BaseLayout from '../layouts/BaseLayout.astro' + +export const prerender = true --- diff --git a/src/pages/index.astro b/src/pages/index.astro index 14bef45..d8a1fb9 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -7,6 +7,8 @@ import Hero from '../components/Hero.astro' // Full Astro Component Syntax: // https://docs.astro.build/core-concepts/astro-components/ + +export const prerender = true --- diff --git a/src/pages/news/[slug].astro b/src/pages/news/[slug].astro new file mode 100644 index 0000000..821c347 --- /dev/null +++ b/src/pages/news/[slug].astro @@ -0,0 +1,16 @@ +--- +import BaseLayout from '@/layouts/BaseLayout.astro' +import { fetchNewsBySlug } from '../../lib/api' + +const { slug } = Astro.params as { slug: string } + +const news = await fetchNewsBySlug(slug) +--- + + +
+

{news.title}

+

{news.publishedAt}

+
+
+ diff --git a/src/pages/news/index.astro b/src/pages/news/index.astro new file mode 100644 index 0000000..5b49408 --- /dev/null +++ b/src/pages/news/index.astro @@ -0,0 +1,22 @@ +--- +import For from '@/components/For.astro' +import BaseLayout from '@/layouts/BaseLayout.astro' +import { fetchNewsList } from '@/lib/api' +import type { News } from '@/types' + +const newsList = await fetchNewsList() +--- + + + + diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..d49808d --- /dev/null +++ b/src/types.ts @@ -0,0 +1,8 @@ +export interface News { + title: string + slug: string + publishedAt: number + excerpt: string + coverImageUrl: string + content: string +} diff --git a/templates/env/.env.hbs b/templates/env/.env.hbs new file mode 100644 index 0000000..1a57eaa --- /dev/null +++ b/templates/env/.env.hbs @@ -0,0 +1 @@ +BACKEND_URL={{backendUrl}}