Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Jan 11, 2024
1 parent 150101f commit ab0608b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.0/schema.json",
"$schema": "https://biomejs.dev/schemas/1.5.1/schema.json",
"organizeImports": {
"enabled": false
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/bun": "^1.0.1",
"@types/node": "^20.10.8",
"dotenv": "^16.3.1",
"tsx": "^4.7.0"
"tsx": "^4.7.0",
"typescript": "^5.3.3"
}
}
29 changes: 9 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,19 @@ import { timing } from "hono/timing"
import { chatProxyHandler } from "./chat/complete/ChatProxyHandler.ts"
import { Logger, gen_logger } from "./log.ts"

export const app = new Hono({ strict: true })
.use(
"*",
cors({
origin: (it) => it,
allowMethods: ["POST", "GET", "OPTIONS"],
}),
timing(),
logger(),
)
.use("*", async (c: ContextWithLogger, next) => {
const openAiRoute = new Hono<{ Variables: { log: Logger } }>()
.use("*", async (c, next) => {
const logger = gen_logger(crypto.randomUUID())
c.set("log", logger)
await next()
})
.options("*", (c) => {
return c.text("", 204)
})
.get("/", (c: ContextWithLogger) => {
const log = c.get("log") as Logger
const text = `Hello Gemini-OpenAI-Proxy from ${getRuntimeKey()}!`
log.info(text)
return c.text(text)
c.set("log", undefined)
})
.post("/v1/chat/completions", chatProxyHandler)

export const app = new Hono({ strict: true })
.use("*", cors(), timing(), logger())
.options("*", (c) => c.text("", 204))
.route("/", openAiRoute)
.get("/", (c) => c.text(`Hello Gemini-OpenAI-Proxy from ${getRuntimeKey()}!`))

export type ContextWithLogger = Context<{ Variables: { log: Logger } }>
20 changes: 9 additions & 11 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
const LogLevel = {
error: 3,
warn: 4,
info: 5,
debug: 7,
} as const
type Any = Parameters<typeof console.log>[0]

export type Logger = {
[K in keyof typeof LogLevel]: (msg: Any) => void
enum LogLevel {
error = 3,
warn = 4,
info = 5,
debug = 7,
}
type Any = Parameters<typeof console.log>[0]

const currentlevel = LogLevel.debug

export function gen_logger(id: string): Logger {
export function gen_logger(id: string) {
return mapValues(LogLevel, (value, name) => {
return (msg: Any) => {
out_func(name, value, `${id} ${msg}`)
}
})
}

export type Logger = ReturnType<typeof gen_logger>

function out_func(levelName: string, levelValue: number, msg: string) {
if (levelValue > currentlevel) {
return
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"useUnknownInCatchVariables": false,
"module": "Node16",
"moduleResolution": "Node16"
}
}
},
}

0 comments on commit ab0608b

Please sign in to comment.