feat: enhance deployment process and server configuration

- Updated `deno.json` to allow environment variable access in the start task.
- Added `deploy.sh` script for building and deploying the application to a remote server.
- Modified `main.ts` to retrieve the `AUTH_SECRET` and `PORT` from environment variables.
- Created `my-score.service` for managing the Deno service with systemd.
- Cleaned up `user.ts` by removing unnecessary console logs.
This commit is contained in:
ethan.chen
2025-06-19 16:28:50 +08:00
parent 499321a842
commit e511ab9db6
5 changed files with 67 additions and 7 deletions

View File

@@ -3,7 +3,7 @@
"hono": "jsr:@hono/hono@^4.7.11"
},
"tasks": {
"start": "deno run --allow-net --allow-read --allow-write --watch main.ts"
"start": "deno run --allow-net --allow-read --allow-write --allow-env --watch main.ts"
},
"compilerOptions": {
"jsx": "precompile",

36
deploy.sh Normal file
View File

@@ -0,0 +1,36 @@
#!/bin/bash
###
# @Date: 2025-06-13 16:11:38
# @LastEditors: 陈子健
# @LastEditTime: 2025-06-18 16:46:20
# @FilePath: /my-score/honoback/deploy.sh
###
# 服务器配置
SERVER="123.57.93.143"
USER="root"
REMOTE_DIR="/home/media-backend"
# 本地构建
echo "Building project..."
deno cache main.ts
# 创建远程目录
echo "Creating remote directory..."
ssh $USER@$SERVER "mkdir -p $REMOTE_DIR"
# 同步文件到服务器
echo "Syncing files to server..."
rsync -avz --exclude 'db/media.db' \
--exclude '.git' \
./ $USER@$SERVER:$REMOTE_DIR/
# 在服务器上安装依赖并启动服务
echo "Installing and starting systemd service..."
ssh $USER@$SERVER "cd $REMOTE_DIR && \
deno cache main.ts && \
sudo cp my-score.service /etc/systemd/system/ && \
sudo systemctl daemon-reload && \
sudo systemctl enable my-score.service && \
sudo systemctl restart my-score.service && \
sudo systemctl status my-score.service"

14
main.ts
View File

@@ -1,7 +1,7 @@
/*
* @Date: 2025-06-02 19:52:12
* @LastEditors: 陈子健
* @LastEditTime: 2025-06-13 13:52:21
* @LastEditTime: 2025-06-18 14:28:41
* @FilePath: /my-score/honoback/main.ts
*/
import { Hono } from 'hono'
@@ -12,7 +12,7 @@ import media from './routes/media.ts'
const app = new Hono<{ Variables: JwtVariables }>()
const AUTH_SECRET = 'it-is-a-secret'
const AUTH_SECRET = Deno.env.get('AUTH_SECRET') || 'it-is-a-secret'
// 添加请求日志中间件
app.use('*', async (c, next) => {
@@ -33,8 +33,14 @@ app.use('/api/test/*', jwt({
// 添加404处理
app.notFound((c) => {
console.log('404 Not Found:', c.req.method, c.req.url)
return c.json({ code: 404, message: 'Not Found' }, 404)
})
Deno.serve(app.fetch)
// 获取端口配置
const port = parseInt(Deno.env.get('PORT') || '8000')
// 启动服务器
if (import.meta.main) {
Deno.serve({ port }, app.fetch)
console.log(`Server running on port ${port}`)
}

19
my-score.service Normal file
View File

@@ -0,0 +1,19 @@
[Unit]
Description=My Score Deno Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/media-backend
Environment=PATH=/usr/local/bin:/usr/bin:/bin
Environment=PORT=8000
Environment=AUTH_SECRET=it-is-a-secret
ExecStart=/usr/local/bin/deno run --allow-net --allow-read --allow-write --allow-env main.ts
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target

View File

@@ -1,7 +1,7 @@
/*
* @Date: 2025-06-11 17:57:58
* @LastEditors: 陈子健
* @LastEditTime: 2025-06-13 14:23:37
* @LastEditTime: 2025-06-16 11:17:09
* @FilePath: /my-score/honoback/routes/user.ts
*/
import { Hono } from 'hono'
@@ -15,7 +15,6 @@ const AUTH_SECRET = 'it-is-a-secret'
// 登录路由
user.post('/login', async (c) => {
console.log(c.req.json(), 111)
const { username, password } = await c.req.json()
// 从数据库验证用户