Compare commits

...
4 Commits
Author SHA1 Message Date
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
8 changed files with 167 additions and 8 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 ./release /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
```
+18 -6
View File
@@ -1,19 +1,18 @@
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";
const tmpApp: Hono = new Hono()
export const app = tmpApp.basePath("/test/")
app.use("/test/*", cors())
// routers
app.get("/user", (c) => {
const re = get()
const re = getUser()
return c.json(ApiResponse.success(200, "Get user successfully", re))
})
@@ -27,7 +26,7 @@ app.post(
return val
}),
c => {
const re = postJson()
const re = postUser()
return c.json(ApiResponse.success(200, "Add successfully", re))
}
)
@@ -95,6 +94,19 @@ app.post("/file",
}
)
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 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"))
+53
View File
@@ -0,0 +1,53 @@
import type { Context } from "hono"
import { ApiResponse } from "../modules/api_response"
import { Random } from "../utils/random"
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, "Param is invalid"))
}
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]"))
}
if (typeList.length == 0) {
c.status(400)
return c.json(ApiResponse.err(400, "Array is empty in parameter[typeList]"))
}
if (isNaN((Number(lenTmp)))) {
c.status(400)
return c.json(ApiResponse.err(400, "Parameter[length] must be a number"))
} else {
len = Number(lenTmp)
if (len <= 0) {
c.status(400)
return c.json(ApiResponse.err(400, "Parameter[length] must > 0"))
}
}
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("Unexcepted error");
}
}
if (result != undefined) {
return c.json(ApiResponse.success(200, "ok", result))
} else {
return c.json(ApiResponse.err(500, "Unexcepted error"))
}
}
@@ -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
}
}