Compare commits
5
Commits
c1d3f4be67
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d778aaabf5 | ||
|
|
49567a2469 | ||
|
|
d6b753e405 | ||
|
|
d8880e98ea | ||
|
|
1d2b261856 |
No files matched your search
@@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
|
||||
build/
|
||||
@@ -0,0 +1,4 @@
|
||||
FROM debian
|
||||
COPY ./app /app
|
||||
EXPOSE 8080
|
||||
CMD ["/app"]
|
||||
@@ -1,15 +1,33 @@
|
||||
# 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_PORT="3306" # 数据库端口
|
||||
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
|
||||
```
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Required environment(local):
|
||||
# server_host
|
||||
# server_port
|
||||
# server_user
|
||||
# server_user_path
|
||||
|
||||
# Project env
|
||||
# DB_HOST
|
||||
# DB_PORT
|
||||
# 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"
|
||||
+3
-1
@@ -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";
|
||||
@@ -12,11 +12,13 @@ const app = appTmp.basePath("/auth")
|
||||
const db = new SQL({
|
||||
"adapter": "mariadb",
|
||||
"host": Bun.env.DB_HOST ?? "localhost",
|
||||
"port": Bun.env.DB_PORT ?? "3306",
|
||||
"user": Bun.env.DB_USER_NAME ?? "root",
|
||||
"password": Bun.env.DB_USER_PASSWD ?? "root",
|
||||
"database": Bun.env.DB_NAME ?? "user_auth",
|
||||
"allowPublicKeyRetrieval": true
|
||||
})
|
||||
await initAdmin(db)
|
||||
|
||||
// 鉴权
|
||||
app.get(
|
||||
|
||||
@@ -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})`
|
||||
}
|
||||
Reference in new issue
Block a user