Compare commits

...
3 Commits
Author SHA1 Message Date
cancerai d6b753e405 fea: 启动时自动根据环境变量添加管理员 2026-10-12 11:28:34 +08:00
cancerai d8880e98ea add: 部署教程 2026-10-12 11:27:56 +08:00
cancerai 1d2b261856 add: 部署脚本 2026-10-12 11:27:30 +08:00
5 changed files with 66 additions and 9 deletions

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
FROM debian
COPY ./app /app
EXPOSE 8080
CMD ["/app"]
+25 -8
View File
@@ -1,15 +1,32 @@
# user-auth
To install dependencies:
支持环境:
```bash
bun install
Linux / MacOS
所需环境:
``` bash
bun
mariadb/mysql
```
To run:
部署步骤:
```bash
bun run index.ts
```
``` bash
# 准备数据库
# 进入数据库
create database user_auth # 数据库名可根据实际替换
source /your/sql/file/path/init_table.sql # sql在 ./sql/init_table.sql中
# 退出数据库
This project was created using `bun init` in bun v1.4.2. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
# 设置环境变量,根据实际修改
export DB_HOST="localhost" # 数据库主机名/ip
export DB_USER_NAME="root" # 数据库用户名
export DB_USER_PASSWD="root" # 数据库密码
export DB_NAME="user_auth" # 数据库名字
export admin_name="admin" # 默认管理员名字
export admin_passwd="admin" # 默认管理员密码
# 运行项目
bun run dev
```
+23
View File
@@ -0,0 +1,23 @@
#!/bin/sh
# Required environment(local):
# server_host
# server_port
# server_user
# server_user_path
# Project env
# DB_HOST
# DB_USER_NAME
# DB_USER_PASSWD
# DB_NAME
# admin_name
# admin_passwd
app_name="user_auth"
app_ver="v1.0.0"
bun run build-linux-x86
scp -P $server_port Dockerfile ${server_user}@${server_host}:${server_user_path}
scp -P $server_port build/linux_x86_app ${server_user}@${server_host}:${server_user_path}/app
ssh -p $server_port root@${server_host} "cd ${server_user_path} && docker build -t ${app_name}:${app_ver} . && docker compose down && docker compose up -d"
ssh -p $server_port ${server_user}@${server_host} "cd ${server_user_path}/ && rm -rf Dockerfile app"
+2 -1
View File
@@ -1,7 +1,7 @@
import { sql, SQL } from "bun";
import { Hono } from "hono";
import { validator } from "hono/validator";
import { checkAdmin } from "./utils/admin";
import { checkAdmin, initAdmin } from "./utils/admin";
import { addUser, checkUser, getAllUser } from "./utils/user";
import { responseStr } from "./const/response_str";
import { checkEnv } from "./utils/env";
@@ -17,6 +17,7 @@ const db = new SQL({
"database": Bun.env.DB_NAME ?? "user_auth",
"allowPublicKeyRetrieval": true
})
await initAdmin(db)
// 鉴权
app.get(
+12
View File
@@ -14,4 +14,16 @@ export async function checkAdmin(
return false
}
return await Bun.password.verify(passwd, rows[0]!["passwd"])
}
export async function initAdmin(db: SQL) {
const admin_name = Bun.env.admin_name ?? ""
const admin_passwd = Bun.env.admin_passwd ?? ""
// 判断有没有管理员
const adminRows = await db`select * from admin`
if (adminRows.length != 0) {
return
}
const encryptPasswd = await Bun.password.hash(admin_passwd)
await db`insert into admin(name, passwd) values(${admin_name}, ${encryptPasswd})`
}