Skip to content

Commit

Permalink
fix: should not use host from request (#220)
Browse files Browse the repository at this point in the history
* ci: build and push images

* fix: should not use host from request
  • Loading branch information
tea-artist authored Oct 25, 2023
1 parent 17cabcf commit 7640591
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build and Push to Alibaba Cloud Registry

on:
push:
branches:
- develop

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to Alibaba Cloud Docker Registry
run: docker login --username=${{ secrets.ALI_DOCKER_USERNAME }} registry.cn-beijing.aliyuncs.com --password=${{ secrets.ALI_DOCKER_PASSWORD }}

- name: Build and push Docker image
run: |
docker build -t registry.cn-beijing.aliyuncs.com/bieber/teable:${GITHUB_SHA} -f dockers/teable/Dockerfile .
docker push registry.cn-beijing.aliyuncs.com/bieber/teable:${GITHUB_SHA}
9 changes: 9 additions & 0 deletions apps/nextjs-app/src/backend/api/rest/table.ssr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ITableFullVo, ITableListVo, IRecord } from '@teable-group/core';
import { FieldKeyType, HttpError } from '@teable-group/core';
import type { IGetBaseVo } from '@teable-group/openapi';
import type { IUser } from '@teable-group/sdk';
import axios from 'axios';

export class SsrApi {
Expand Down Expand Up @@ -54,6 +55,14 @@ export class SsrApi {
async getBaseById(baseId: string) {
return await this.axios.get<IGetBaseVo>(`/base/${baseId}`).then(({ data }) => data);
}

async getUserMe(cookie?: string) {
return await this.axios
.get<IUser>(`/auth/user/me`, {
headers: { cookie },
})
.then(({ data }) => data);
}
}

export const ssrApi = new SsrApi();
13 changes: 5 additions & 8 deletions apps/nextjs-app/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
import { HttpError, parseDsn } from '@teable-group/core';
import { axios } from '@teable-group/openapi';
import type { IUser } from '@teable-group/sdk';
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
Expand All @@ -12,6 +11,7 @@ import Head from 'next/head';
import { appWithTranslation } from 'next-i18next';
import type { ReactElement, ReactNode } from 'react';
import { z } from 'zod';
import { ssrApi } from '@/backend/api/rest/table.ssr';
import { colors } from '@/themes/colors';
import { INITIAL_THEME } from '@/themes/initial';
import { getColorsCssVariablesText } from '@/themes/utils';
Expand Down Expand Up @@ -97,32 +97,28 @@ MyApp.getInitialProps = async (appContext: AppContext) => {
// calls page's `getInitialProps` and fills `appProps.pageProps`
const appProps = await App.getInitialProps(appContext);
const res = appContext.ctx.res;
const host = appContext.ctx.req?.headers.host || '';
const isLoginPage = appContext.ctx.pathname.startsWith('/auth/login');

if (!res || !res?.writeHead) {
return appProps;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { driver } = parseDsn(process.env.PRISMA_DATABASE_URL!);
const { driver } = parseDsn(process.env.PRISMA_DATABASE_URL as string);
const initialProps = {
...appProps,
driver,
};

try {
const user = await axios.get<IUser>(`http://${host}/api/auth/user/me`, {
headers: { cookie: appContext.ctx.req?.headers.cookie },
});
const user = await ssrApi.getUserMe(appContext.ctx.req?.headers.cookie);
// Already logged in
if (user && isLoginPage) {
res.writeHead(302, {
Location: `/space`,
});
return res.end();
}
return { ...initialProps, user: user.data };
return { ...initialProps, user };
} catch (error) {
if (error instanceof HttpError && !isLoginPage) {
const redirect = encodeURIComponent(appContext.ctx.req?.url || '');
Expand All @@ -132,6 +128,7 @@ MyApp.getInitialProps = async (appContext: AppContext) => {
});
return res.end();
}
console.error(error);
}

return initialProps;
Expand Down
1 change: 1 addition & 0 deletions dockers/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
container_name: integration-test
build:
context: ../
dockerfile: ./dockers/teable/Dockerfile
target: builder
args:
INTEGRATION_TEST: 1
Expand Down
File renamed without changes.

0 comments on commit 7640591

Please sign in to comment.