Compare commits
8
Commits
9c95404e93
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a6ab45920 | ||
|
|
ac53e4ed27 | ||
|
|
b26bee6f05 | ||
|
|
3ad5f0915b | ||
|
|
33172b0968 | ||
|
|
22a95e020f | ||
|
|
762a473d09 | ||
|
|
6515a6d84e |
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"]
|
||||
@@ -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
|
||||
```
|
||||
@@ -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"
|
||||
+3
-3
@@ -6,9 +6,9 @@
|
||||
"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"
|
||||
"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"
|
||||
|
||||
@@ -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
@@ -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 {
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user