Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tidy server and enhance ci workflow #3

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ permissions:
contents: write

jobs:
publish:
publish-npm:
runs-on: ubuntu-latest
outputs:
server_updated: ${{ steps.publish.outputs.server_updated }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -42,4 +44,42 @@ jobs:
run: corepack yarn config set -H 'npmAuthToken' "${{ secrets.NPM_TOKEN }}"

- name: Publish and commit
id: publish
run: corepack yarn zx scripts/publish.mjs

- name: Upload server tgz
uses: actions/upload-artifact@v2
if: steps.publish.outputs.server_updated == 'true'
with:
name: server
path: apps/server/package.tgz

publish-docker-server:
runs-on: ubuntu-latest
needs: publish-npm
if: needs.publish-npm.outputs.server_updated == 'true'
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Download server tgz
uses: actions/download-artifact@v2
with:
name: server
path: apps/server

- name: Log in to Aliyun Container Registry
uses: docker/login-action@v2
with:
registry: registry.cn-hangzhou.aliyuncs.com
username: ${{ secrets.ALIYUN_USERNAME }}
password: ${{ secrets.ALIYUN_PASSWORD }}

- name: Build and push server Docker image
uses: docker/build-push-action@v4
with:
push: true
context: '.'
file: './docker/dockerfiles/server.dockerfile'
tags: |
registry.cn-hangzhou.aliyuncs.com/aoi-js/server:latest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
node_modules

*.tsbuildinfo
package.tgz
2 changes: 2 additions & 0 deletions .yarn/versions/b1f9f36a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@aoi-js/server": patch
6 changes: 2 additions & 4 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
"@aws-sdk/s3-request-presigner": "^3.385.0",
"@fastify/jwt": "^7.2.0",
"@fastify/sensible": "^5.2.0",
"@fastify/static": "^6.10.2",
"@fastify/swagger": "^8.8.0",
"@fastify/swagger-ui": "^1.9.2",
"@fastify/type-provider-typebox": "^3.3.0",
"@sinclair/typebox": "^0.29.4",
"bcrypt": "^5.1.1",
Expand All @@ -40,7 +37,8 @@
"pino-pretty": "^10.3.0"
},
"optionalDependencies": {
"@aoi-js/frontend": "workspace:^"
"@fastify/swagger": "^8.12.1",
"@fastify/swagger-ui": "^2.0.1"
},
"scripts": {
"build": "run -T tsc",
Expand Down
93 changes: 41 additions & 52 deletions apps/server/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,59 @@
import fastify from 'fastify'
import fastifySensible from '@fastify/sensible'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUi from '@fastify/swagger-ui'
import fastifyStatic from '@fastify/static'
import { apiRoutes } from '../routes/index.js'
import { logger } from '../utils/logger.js'
import { schemaRoutes } from './schemas.js'
import { loadEnv } from '../index.js'
import { frontendPath } from '../utils/module.js'
import { hasModule } from '../utils/module.js'

const server = fastify({ logger })

await server.register(fastifySensible)

await server.register(schemaRoutes, { prefix: '/schemas' })

await server.register(fastifySwagger, {
openapi: {
info: {
title: '@aoi-js/server',
description: 'API Server of the AOI Project',
version: 'latest'
},
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT'
},
runnerKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-Runner-Key'
},
runnerId: {
type: 'apiKey',
in: 'header',
name: 'X-Runner-Id'
}
}
},
security: [
{
bearerAuth: []
}
]
}
})
await server.register(fastifySwaggerUi, {
routePrefix: '/docs'
})

await server.register(apiRoutes, { prefix: '/api' })
if (hasModule('@fastify/swagger') && hasModule('@fastify/swagger-ui')) {
logger.warn('Swagger is enabled and is not recommended for production use')
const { default: fastifySwagger } = await import('@fastify/swagger')
const { default: fastifySwaggerUi } = await import('@fastify/swagger-ui')

const staticRoot = loadEnv('STATIC_ROOT', String, frontendPath)
if (staticRoot) {
await server.register(fastifyStatic, {
root: staticRoot
})
server.setNotFoundHandler((req, rep) => {
if (req.url.startsWith('/api')) {
return rep.notFound()
await server.register(fastifySwagger, {
openapi: {
info: {
title: '@aoi-js/server',
description: 'API Server of the AOI Project',
version: 'latest'
},
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT'
},
runnerKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-Runner-Key'
},
runnerId: {
type: 'apiKey',
in: 'header',
name: 'X-Runner-Id'
}
}
},
security: [
{
bearerAuth: []
}
]
}
rep.sendFile('index.html')
})
await server.register(fastifySwaggerUi, {
routePrefix: '/docs'
})
}

await server.register(apiRoutes, { prefix: '/api' })

export { server }
9 changes: 2 additions & 7 deletions apps/server/src/utils/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createRequire } from 'node:module'
import { join } from 'node:path'

const require = createRequire(import.meta.url)

Expand All @@ -11,10 +10,6 @@ export function tryResolve(mod: string) {
}
}

function resolveFrontendPath() {
const frontendPackage = tryResolve('@aoi-js/frontend/package.json')
if (!frontendPackage) return ''
return join(frontendPackage, '..', 'dist')
export function hasModule(mod: string) {
return tryResolve(mod) !== null
}

export const frontendPath = resolveFrontendPath()
12 changes: 12 additions & 0 deletions docker/dockerfiles/server.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:18-alpine

RUN corepack enable
WORKDIR /opt
COPY apps/server/package.tgz /opt/package.tgz
RUN tar -xzf package.tgz && rm package.tgz && mv package aoi-server
WORKDIR /opt/aoi-server
RUN npm install --omit=dev --omit=optional

USER node

CMD [ "node", "lib/cli/index.js" ]
6 changes: 0 additions & 6 deletions docker/images/server-only/Dockerfile

This file was deleted.

6 changes: 0 additions & 6 deletions docker/images/server/Dockerfile

This file was deleted.

5 changes: 5 additions & 0 deletions scripts/publish.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ let shouldCommit = false
for (const { ident } of items) {
console.log(`Publishing ${chalk.greenBright(ident)}...`)
await $`yarn workspace ${ident} npm publish --access public`
await $`yarn workspace ${ident} pack --out package.tgz`
shouldCommit = true
if (process.env.CI) {
const name = ident.split('/')[1]
await $`echo "${name}_updated=true" >> "$GITHUB_OUTPUT"`
}
}

if (shouldCommit) {
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ __metadata:
languageName: unknown
linkType: soft

"@aoi-js/frontend@workspace:^, @aoi-js/frontend@workspace:apps/frontend":
"@aoi-js/frontend@workspace:apps/frontend":
version: 0.0.0-use.local
resolution: "@aoi-js/frontend@workspace:apps/frontend"
dependencies:
Expand Down Expand Up @@ -107,14 +107,12 @@ __metadata:
resolution: "@aoi-js/server@workspace:apps/server"
dependencies:
"@aoi-js/common": "workspace:^"
"@aoi-js/frontend": "workspace:^"
"@aws-sdk/client-s3": ^3.385.0
"@aws-sdk/s3-request-presigner": ^3.385.0
"@fastify/jwt": ^7.2.0
"@fastify/sensible": ^5.2.0
"@fastify/static": ^6.10.2
"@fastify/swagger": ^8.8.0
"@fastify/swagger-ui": ^1.9.2
"@fastify/swagger": ^8.12.1
"@fastify/swagger-ui": ^2.0.1
"@fastify/type-provider-typebox": ^3.3.0
"@sinclair/typebox": ^0.29.4
"@types/bcrypt": ^5.0.0
Expand All @@ -127,7 +125,9 @@ __metadata:
pino: ^8.17.1
pino-pretty: ^10.3.0
dependenciesMeta:
"@aoi-js/frontend":
"@fastify/swagger":
optional: true
"@fastify/swagger-ui":
optional: true
bin:
aoi-server: lib/cli/index.js
Expand Down Expand Up @@ -1543,7 +1543,7 @@ __metadata:
languageName: node
linkType: hard

"@fastify/static@npm:^6.0.0, @fastify/static@npm:^6.10.2":
"@fastify/static@npm:^6.0.0":
version: 6.12.0
resolution: "@fastify/static@npm:6.12.0"
dependencies:
Expand All @@ -1557,20 +1557,20 @@ __metadata:
languageName: node
linkType: hard

"@fastify/swagger-ui@npm:^1.9.2":
version: 1.10.2
resolution: "@fastify/swagger-ui@npm:1.10.2"
"@fastify/swagger-ui@npm:^2.0.1":
version: 2.0.1
resolution: "@fastify/swagger-ui@npm:2.0.1"
dependencies:
"@fastify/static": ^6.0.0
fastify-plugin: ^4.0.0
openapi-types: ^12.0.2
rfdc: ^1.3.0
yaml: ^2.2.2
checksum: 74bfc3a9499a39407161679434057c616accbaf6c142a3e11356b2e70f4759ca5e1813b05b9d6cbd2b933668574bf296fe9051ea4af2698641edd92f092c09f3
checksum: ec004af28ecc73d64bdf17569f2b86776b480392c0310d56ac1e850348d639a4872e773f1ec82a55e4bb35329b23fe7e8b8e04e40cb8da71808f3f1435b15bb3
languageName: node
linkType: hard

"@fastify/swagger@npm:^8.8.0":
"@fastify/swagger@npm:^8.12.1":
version: 8.12.1
resolution: "@fastify/swagger@npm:8.12.1"
dependencies:
Expand Down