Skip to content

Commit d69931b

Browse files
committed
fix: deploy cors issue
1 parent 7b19dc6 commit d69931b

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

apps/book-server/src/services/BookBuildService/index.mts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface Service {
2929
@singleton()
3030
export class BookBuildService implements Service {
3131
constructor(
32+
private readonly mongo: MongoService,
3233
private readonly utils: UtilsService,
3334
private readonly mq: MqService,
3435
private readonly bookService: BookService,
@@ -57,6 +58,17 @@ export class BookBuildService implements Service {
5758
throw new ConfilctError('Not owner of book')
5859
}
5960

61+
// create deploy code
62+
const deployCode = this.utils.randomString(10).toLocaleLowerCase()
63+
await this.mongo.book.update({
64+
where: {
65+
id: book.id,
66+
},
67+
data: {
68+
deploy_code: deployCode,
69+
},
70+
})
71+
6072
// create folder
6173
const dest = path.resolve(process.cwd(), 'books', book.id)
6274

@@ -93,7 +105,6 @@ export class BookBuildService implements Service {
93105
const pages = await this.pageService.getPages(book.url_slug, writer.id)
94106

95107
// create meta.json
96-
97108
await this.writeMetaJson({
98109
pages: pages || [],
99110
baseDest: pagesDir,
@@ -104,7 +115,7 @@ export class BookBuildService implements Service {
104115
`${dest}/next.config.mjs`,
105116
nextConfigTempate({
106117
bucketUrl: ENV.bookBucketUrl,
107-
username: writer.username,
118+
deployCode: deployCode,
108119
urlSlug: book.url_slug,
109120
}),
110121
)
@@ -198,16 +209,13 @@ export class BookBuildService implements Service {
198209
}
199210
private insertKey = (pages: Page[]) => {
200211
return pages.map((page) => ({
201-
key: `${page.title}_${customRandom(urlAlphabet, 10, random)().toLocaleLowerCase()}`.replace(
202-
/[^a-zA-Z0-9-]/g,
203-
'_',
204-
),
212+
key: `${this.utils.escapeForUrl(page.title)}-${page.code}`.toLocaleLowerCase(),
205213
...page,
206214
}))
207215
}
208216
}
209217

210-
type MetaJsonSerpator = { id: string; type: 'separator'; title: string }
218+
type MetaJsonSerpator = { id?: string; type: 'separator' | 'page'; title?: string }
211219
type MetaJsonValue = string | MetaJsonSerpator
212220
type MetaJson = Record<string, MetaJsonValue>
213221

apps/book-server/src/services/BookDeployService/index.mts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { UnauthorizedError } from '@errors/UnauthorizedError.mjs'
1111
import { ConfilctError } from '@errors/ConfilctError.mjs'
1212
import { DeployResult } from '@graphql/generated.js'
1313
import { UtilsService } from '@lib/utils/UtilsService.mjs'
14+
import { MongoService } from '@lib/mongo/MongoService.mjs'
1415

1516
interface Service {
1617
deploy: (bookId: string, signedWriterId?: string) => Promise<DeployResult>
@@ -19,6 +20,7 @@ interface Service {
1920
@singleton()
2021
export class BookDeployService implements Service {
2122
constructor(
23+
private readonly mongo: MongoService,
2224
private readonly awsS3: AwsS3Service,
2325
private readonly utils: UtilsService,
2426
private readonly writerService: WriterService,
@@ -65,29 +67,14 @@ export class BookDeployService implements Service {
6567
// upload to S3
6668
const baseUrl = `${book.url_slug}`.replace('/', '')
6769

68-
let existsDeployedBook = null
69-
try {
70-
existsDeployedBook = await this.awsS3.getObject({
71-
bucketName: ENV.bookBucketName,
72-
key: `${baseUrl}/index.html`,
73-
})
74-
} catch (_) {}
75-
76-
if (existsDeployedBook && existsDeployedBook['$metadata'].httpStatusCode === 200) {
77-
await this.awsS3.deleteFolder({
78-
bucketName: ENV.bookBucketName,
79-
key: `${baseUrl}/`,
80-
})
81-
}
82-
8370
const promises = targetFiles.map(async (filePath) => {
8471
const body = fs.readFileSync(filePath)
8572
let relativePath = filePath.replace(output, '')
8673
const ext = path.extname(relativePath)
8774
if (ext === '.html' && !relativePath.includes('index.html')) {
8875
relativePath = relativePath.replace('.html', '')
8976
}
90-
const key = `${baseUrl}${relativePath}`
77+
const key = `${baseUrl}/${book.deploy_code}${relativePath}`
9178
const contentType = mime.getType(filePath)
9279
await this.awsS3.uploadFile({
9380
bucketName: ENV.bookBucketName,
@@ -100,9 +87,16 @@ export class BookDeployService implements Service {
10087

10188
try {
10289
await Promise.all(promises)
103-
const published_url = `https://books.velog.io/${baseUrl}/index.html`
104-
105-
console.log(`Deployed URL: , ${published_url}`)
90+
const published_url = `https://books.velog.io/${baseUrl}/${book.deploy_code}`
91+
92+
await this.mongo.book.update({
93+
where: {
94+
id: book.id,
95+
},
96+
data: {
97+
published_url,
98+
},
99+
})
106100

107101
return {
108102
published_url,

apps/book-server/src/templates/nextConfigTemplate.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
type Props = {
22
bucketUrl: string
3-
username: string
3+
deployCode: string
44
urlSlug: string
55
}
66

7-
export const nextConfigTempate = ({ bucketUrl, username, urlSlug }: Props) => {
7+
export const nextConfigTempate = ({ bucketUrl, deployCode, urlSlug }: Props) => {
88
return `
99
import nextra from 'nextra'
1010
@@ -23,8 +23,8 @@ export const nextConfigTempate = ({ bucketUrl, username, urlSlug }: Props) => {
2323
images: {
2424
unoptimized: true,
2525
},
26-
assetPrefix: process.env.NODE_ENV === 'production' ? '${bucketUrl}${urlSlug}' : undefined,
27-
basePath: '${urlSlug}',
26+
assetPrefix: process.env.NODE_ENV === 'production' ? 'https://books.velog.io${urlSlug}/${deployCode}' : undefined,
27+
basePath: '${urlSlug}/${deployCode}',
2828
trailingSlash: false
2929
}
3030

packages/database/prisma/velog-book-mongo/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ model Book {
3838
released_at DateTime? @db.Timestamp
3939
url_slug String @unique // example $@{username}/${escapeUrl(title).toLowerCase()}
4040
published_url String @default("") @db.String
41+
deploy_code String? @default("") @db.String
4142
4243
created_at DateTime @default(now())
4344
updated_at DateTime @default(now())

0 commit comments

Comments
 (0)