Compare commits

..
4 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
6 changed files with 73 additions and 44 deletions

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
FROM debian
COPY ./release /app
COPY ./app /app
EXPOSE 8080
CMD ["/app"]
+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 = "找不到请求路径"
}
+15 -15
View File
@@ -6,14 +6,14 @@ import { isEmpty } from "./utils/is_empty";
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/")
// routers
app.get("/user", (c) => {
const re = getUser()
return c.json(ApiResponse.success(200, "Get user successfully", re))
return c.json(ApiResponse.success(200, responseStr.USER_GET_SUCCESS, re))
})
app.post(
@@ -21,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 = postUser()
return c.json(ApiResponse.success(200, "Add successfully", re))
return c.json(ApiResponse.success(200, responseStr.USER_ADD_SUCCESS, re))
}
)
@@ -35,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))
}
)
@@ -48,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))
}
)
@@ -61,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))
}
)
@@ -75,19 +75,19 @@ 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
}))
@@ -98,7 +98,7 @@ app.get("/random",
validator("query", (val, c) => {
if (isEmpty(val.typeList, val.length)) {
c.status(400)
return c.json(ApiResponse.err(400, "Param is invalid"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
return val
}),
@@ -109,7 +109,7 @@ app.get("/random",
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 {
+8 -7
View File
@@ -1,6 +1,7 @@
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")
@@ -8,28 +9,28 @@ export function getRandom(c: Context) {
let len: number
if (typeInReq == "") {
c.status(400)
return c.json(ApiResponse.err(400, "Param is invalid"))
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, "Syntax error in parameter[typeList]"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
if (typeList.length == 0) {
c.status(400)
return c.json(ApiResponse.err(400, "Array is empty in parameter[typeList]"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
if (isNaN((Number(lenTmp)))) {
c.status(400)
return c.json(ApiResponse.err(400, "Parameter[length] must be a number"))
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, "Parameter[length] must > 0"))
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
}
}
@@ -41,13 +42,13 @@ export function getRandom(c: Context) {
c.status(400)
return c.json(ApiResponse.err(400, e.message))
} else {
console.log("Unexcepted error");
console.log(responseStr.UNEXPECTED_ERR);
}
}
if (result != undefined) {
return c.json(ApiResponse.success(200, "ok", result))
} else {
return c.json(ApiResponse.err(500, "Unexcepted error"))
return c.json(ApiResponse.err(500, responseStr.UNEXPECTED_ERR))
}
}