fix: should not use host from request (#220)

* ci: build and push images

* fix: should not use host from request
This commit is contained in:
tea artist 2023-10-25 22:47:35 +08:00 committed by GitHub
parent 17cabcfc37
commit 7640591312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 8 deletions

25
.github/workflows/docker-push.yml vendored Normal file
View File

@ -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}

View File

@ -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 {
@ -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();

View File

@ -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';
@ -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';
@ -97,24 +97,20 @@ 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, {
@ -122,7 +118,7 @@ MyApp.getInitialProps = async (appContext: AppContext) => {
});
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 || '');
@ -132,6 +128,7 @@ MyApp.getInitialProps = async (appContext: AppContext) => {
});
return res.end();
}
console.error(error);
}
return initialProps;

View File

@ -5,6 +5,7 @@ services:
container_name: integration-test
build:
context: ../
dockerfile: ./dockers/teable/Dockerfile
target: builder
args:
INTEGRATION_TEST: 1