Compare commits

...
8 Commits
Author SHA1 Message Date
cancerai 7a6ab45920 mod: 通用化部署脚本 2026-10-11 23:51:11 +08:00
cancerai ac53e4ed27 mod: 规范构建script 2026-10-11 23:08:21 +08:00
cancerai b26bee6f05 add: 自动部署上线脚本 2026-10-11 19:08:56 +08:00
cancerai 3ad5f0915b change: 规范响应描述 2026-10-11 18:36:38 +08:00
cancerai 33172b0968 删除了跨域,并删除一些无用库引用 2026-09-29 19:38:37 +08:00
cancerai 22a95e020f patch: README文件的编译和清理说明 2026-09-26 02:10:31 +08:00
cancerai 762a473d09 change: 修改了一些名字 2026-09-26 02:05:22 +08:00
cancerai 6515a6d84e add: 生成随机数接口 2026-09-26 02:04:21 +08:00
11 changed files with 231 additions and 43 deletions

No files matched your search

+2
View File
@@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Finder (MacOS) folder config
.DS_Store
build/
+4
View File
@@ -0,0 +1,4 @@
FROM debian
COPY ./app /app
EXPOSE 8080
CMD ["/app"]
+15
View File
@@ -14,3 +14,18 @@ To run:
```bash
bun run dev
```
To build:
``` bash
# MacOS
bun run build-macos
# Linux(x86)
bun run build-linux-x86
# Linux(arm64)
bun run build-linux-arm64
```
To clean:
```bash
bun run clean
```
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
# 上线脚本
app_name="test-api"
app_ver="v1.02"
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"
+21 -21
View File
@@ -1,22 +1,22 @@
{
"name": "test-api",
"module": "index.ts",
"type": "module",
"private": true,
"scripts": {
"dev": "bun run --hot src/index.ts",
"clean": "rm -rf ./build && rm -rf ./.*.bun-build",
"build-macos": "mkdir -p build && bun build src/index.ts --compile --minify --outfile build/test-api-macos",
"build-linux-x86": "bun build src/index.ts --compile --minify --target=bun-linux-x64 --outfile build/test-api-linux-x86",
"build-linux-arm64": "bun build src/index.ts --compile --minify --target=bun-linux-arm64 --outfile build/test-api-linux-arm64"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^7"
},
"dependencies": {
"hono": "^4.13.8"
}
}
"name": "test-api",
"module": "index.ts",
"type": "module",
"private": true,
"scripts": {
"dev": "bun run --hot src/index.ts",
"clean": "rm -rf ./build && rm -rf ./.*.bun-build",
"build-macos": "mkdir -p build && bun build src/index.ts --compile --minify --outfile build/macos_app",
"build-linux-x86": "bun build src/index.ts --compile --minify --target=bun-linux-x64 --outfile build/linux_x86_app",
"build-linux-arm64": "bun build src/index.ts --compile --minify --target=bun-linux-arm64 --outfile build/linux_arm64_app"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^7"
},
"dependencies": {
"hono": "^4.13.8"
}
}
+17
View File
@@ -0,0 +1,17 @@
export class responseStr {
static REQ_PARAM_ERR = "请求参数错误"
static REQ_SYNTAX_ERR = "请求解析错误"
static USER_GET_SUCCESS = "用户获取成功"
static USER_ADD_SUCCESS = "用户添加成功"
static USER_DEL_SUCCESS = "用户删除成功"
static USER_MOD_SUCCESS = "用户修改成功"
static FILE_SIZE_LIMIT_5M = "文件大小应不超过5MB"
static FILE_TYPE_INVALID = "不是合法的文件"
static FILE_UPLOAD_SUCCESS = "文件上传成功"
static UNEXPECTED_ERR = "未知错误"
static API_NOTFOUND = "找不到请求路径"
}
+32 -20
View File
@@ -1,20 +1,19 @@
import { Hono } from "hono";
import { ApiResponse } from "./modules/api_response";
import { validator } from "hono/validator";
import { postJson } from "./service/postJson";
import { postUser } from "./service/postUser";
import { isEmpty } from "./utils/is_empty";
import { get } from "./service/get";
import { cors } from "hono/cors";
import { getUser } from "./service/getUser";
import { bodyLimit } from "hono/body-limit";
import { getRandom } from "./service/getRandom";
import { responseStr } from "./const/response_str";
const tmpApp: Hono = new Hono()
export const app = tmpApp.basePath("/test/")
app.use("/test/*", cors())
// routers
app.get("/user", (c) => {
const re = get()
return c.json(ApiResponse.success(200, "Get user successfully", re))
const re = getUser()
return c.json(ApiResponse.success(200, responseStr.USER_GET_SUCCESS, re))
})
app.post(
@@ -22,13 +21,13 @@ app.post(
validator("json", (val, c) => {
if (isEmpty(val.name, val.passwd)) {
c.status(400)
return c.json(ApiResponse.err(400, "Param is invalid"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
return val
}),
c => {
const re = postJson()
return c.json(ApiResponse.success(200, "Add successfully", re))
const re = postUser()
return c.json(ApiResponse.success(200, responseStr.USER_ADD_SUCCESS, re))
}
)
@@ -36,12 +35,12 @@ app.patch("/user",
validator("json", (val, c) => {
if (isEmpty(val.id, val.changeFields)) {
c.status(400)
return c.json(ApiResponse.err(400, "Param is invalid"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
return val
}),
c => {
return c.json(ApiResponse.success(200, "Change user information successfully"))
return c.json(ApiResponse.success(200, responseStr.USER_MOD_SUCCESS))
}
)
@@ -49,12 +48,12 @@ app.put("/user",
validator("json", (val, c) => {
if (isEmpty(val.id, val.changeFields)) {
c.status(400)
return c.json(ApiResponse.err(400, "Param is invalid"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
return val
}),
c => {
return c.json(ApiResponse.success(200, "Change user information successfully"))
return c.json(ApiResponse.success(200, responseStr.USER_MOD_SUCCESS))
}
)
@@ -62,13 +61,13 @@ app.delete("/user",
validator("json", (val, c) => {
if (isEmpty(val.id)) {
c.status(400)
return c.json(ApiResponse.err(400, "Param is invalid"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
return val
}),
async (c) => {
const id = await c.req.json()
return c.json(ApiResponse.success(200, `Delete user successfully by id: ${id.id}`))
return c.json(ApiResponse.success(200, responseStr.USER_DEL_SUCCESS))
}
)
@@ -76,28 +75,41 @@ app.post("/file",
bodyLimit({
maxSize: 5 * 1024 * 1024,
onError: (c) => {
return c.json(ApiResponse.err(400, "File must less than 5MB"))
return c.json(ApiResponse.err(400, responseStr.FILE_SIZE_LIMIT_5M))
}
}),
async (c) => {
const body = await c.req.parseBody()
const file = body["file"]
if (!(file instanceof File)) {
return c.json(ApiResponse.err(400, "Not file"))
return c.json(ApiResponse.err(400, responseStr.FILE_TYPE_INVALID))
}
const fileName = file.name
const fileSize = file.size
return c.json(ApiResponse.success(200, "Upload file successfully", {
return c.json(ApiResponse.success(200, responseStr.FILE_UPLOAD_SUCCESS, {
fileName,
fileSize
}))
}
)
app.get("/random",
validator("query", (val, c) => {
if (isEmpty(val.typeList, val.length)) {
c.status(400)
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
return val
}),
c => {
return getRandom(c)
}
)
app.notFound((c) => {
console.log(`Not found [${c.req.method}]: ${c.req.path}`);
return c.json(ApiResponse.err(404, "Not Found"))
return c.json(ApiResponse.err(404, responseStr.API_NOTFOUND))
})
export default {
+54
View File
@@ -0,0 +1,54 @@
import type { Context } from "hono"
import { ApiResponse } from "../modules/api_response"
import { Random } from "../utils/random"
import { responseStr } from "../const/response_str"
export function getRandom(c: Context) {
const typeInReq = c.req.query("typeList")
const lenTmp = c.req.query("length")
let len: number
if (typeInReq == "") {
c.status(400)
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
let typeList: string[]
try {
typeList = JSON.parse(typeInReq!.toString())
} catch (e) {
c.status(400)
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
if (typeList.length == 0) {
c.status(400)
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
if (isNaN((Number(lenTmp)))) {
c.status(400)
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
} else {
len = Number(lenTmp)
if (len <= 0) {
c.status(400)
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
}
let result
try {
result = new Random(typeList, len).get()
} catch (e) {
if (e instanceof Error) {
c.status(400)
return c.json(ApiResponse.err(400, e.message))
} else {
console.log(responseStr.UNEXPECTED_ERR);
}
}
if (result != undefined) {
return c.json(ApiResponse.success(200, "ok", result))
} else {
return c.json(ApiResponse.err(500, responseStr.UNEXPECTED_ERR))
}
}
@@ -1,4 +1,4 @@
export function get() {
export function getUser() {
return [
{
"id": "001",
@@ -1,4 +1,4 @@
export function postJson() {
export function postUser() {
return {
"id": "004"
}
+73
View File
@@ -0,0 +1,73 @@
enum Type {
lcLetter = "lcLetter",
ucLetter = "ucLetter",
number = "number",
specialChar = "specialChar"
}
export class Random {
typeList: string[]
len: number
lowerCaseLetterList: string[] = ((): string[] => {
return [...Array(26)].map((_, i) => String.fromCharCode(i + 97))
})()
upperCaseLetterList: string[] = ((): string[] => {
return [...Array(26)].map((_, i) => String.fromCharCode(i + 65))
})()
numberList: string[] = ((): string[] => {
return [...Array(10)].map((_, i) => String.fromCharCode(i + 48))
})()
specialCharList = Array.from(`!@#$%^&*()_+-=[]{}|;':",./<>?`)
all: string[] = []
constructor(typeList: string[], len: number) {
this.typeList = typeList
this.len = len
if (
!(this.typeList.length == 1 &&
this.typeList[0] === "all")
) {
for (let i = 0; i < this.typeList.length; i++) {
switch (this.typeList[i]) {
case Type.lcLetter:
this.all = [...this.all, ...this.lowerCaseLetterList]
break
case Type.ucLetter:
this.all = [...this.all, ...this.upperCaseLetterList]
break
case Type.number:
this.all = [...this.all, ...this.numberList]
break
case Type.specialChar:
this.all = [...this.all, ...this.specialCharList]
break
default:
throw new Error(
`At least contains "all"|"${Type.lcLetter}"|"${Type.ucLetter}"|"${Type.number}"|"${Type.specialChar}" in parameter[typeList]`
);
}
}
} else {
this.all = [
...this.lowerCaseLetterList,
...this.upperCaseLetterList,
...this.numberList,
...this.specialCharList
]
}
}
get(): string {
let resultArr: string[] = []
for (let i = 0; i < this.len; i++) {
const randomNumber = Math.floor(Math.random() * this.all.length)
resultArr = [...resultArr, this.all[randomNumber]!]
}
let result = ""
resultArr.map((val, _) => {
result += val
})
return result
}
}