|
| 1 | +import { Auth } from "@/auth" |
| 2 | +import { Log } from "@/util/log" |
| 3 | +import { ProviderID } from "@/provider/schema" |
| 4 | +import { Hono } from "hono" |
| 5 | +import { describeRoute, resolver, validator, openAPIRouteHandler } from "hono-openapi" |
| 6 | +import z from "zod" |
| 7 | +import { errors } from "../error" |
| 8 | +import { GlobalRoutes } from "../instance/global" |
| 9 | + |
| 10 | +export function ControlPlaneRoutes(): Hono { |
| 11 | + const app = new Hono() |
| 12 | + return app |
| 13 | + .route("/global", GlobalRoutes()) |
| 14 | + .put( |
| 15 | + "/auth/:providerID", |
| 16 | + describeRoute({ |
| 17 | + summary: "Set auth credentials", |
| 18 | + description: "Set authentication credentials", |
| 19 | + operationId: "auth.set", |
| 20 | + responses: { |
| 21 | + 200: { |
| 22 | + description: "Successfully set authentication credentials", |
| 23 | + content: { |
| 24 | + "application/json": { |
| 25 | + schema: resolver(z.boolean()), |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + ...errors(400), |
| 30 | + }, |
| 31 | + }), |
| 32 | + validator( |
| 33 | + "param", |
| 34 | + z.object({ |
| 35 | + providerID: ProviderID.zod, |
| 36 | + }), |
| 37 | + ), |
| 38 | + validator("json", Auth.Info.zod), |
| 39 | + async (c) => { |
| 40 | + const providerID = c.req.valid("param").providerID |
| 41 | + const info = c.req.valid("json") |
| 42 | + await Auth.set(providerID, info) |
| 43 | + return c.json(true) |
| 44 | + }, |
| 45 | + ) |
| 46 | + .delete( |
| 47 | + "/auth/:providerID", |
| 48 | + describeRoute({ |
| 49 | + summary: "Remove auth credentials", |
| 50 | + description: "Remove authentication credentials", |
| 51 | + operationId: "auth.remove", |
| 52 | + responses: { |
| 53 | + 200: { |
| 54 | + description: "Successfully removed authentication credentials", |
| 55 | + content: { |
| 56 | + "application/json": { |
| 57 | + schema: resolver(z.boolean()), |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + ...errors(400), |
| 62 | + }, |
| 63 | + }), |
| 64 | + validator( |
| 65 | + "param", |
| 66 | + z.object({ |
| 67 | + providerID: ProviderID.zod, |
| 68 | + }), |
| 69 | + ), |
| 70 | + async (c) => { |
| 71 | + const providerID = c.req.valid("param").providerID |
| 72 | + await Auth.remove(providerID) |
| 73 | + return c.json(true) |
| 74 | + }, |
| 75 | + ) |
| 76 | + .get( |
| 77 | + "/doc", |
| 78 | + openAPIRouteHandler(app, { |
| 79 | + documentation: { |
| 80 | + info: { |
| 81 | + title: "opencode", |
| 82 | + version: "0.0.3", |
| 83 | + description: "opencode api", |
| 84 | + }, |
| 85 | + openapi: "3.1.1", |
| 86 | + }, |
| 87 | + }), |
| 88 | + ) |
| 89 | + .use( |
| 90 | + validator( |
| 91 | + "query", |
| 92 | + z.object({ |
| 93 | + directory: z.string().optional(), |
| 94 | + workspace: z.string().optional(), |
| 95 | + }), |
| 96 | + ), |
| 97 | + ) |
| 98 | + .post( |
| 99 | + "/log", |
| 100 | + describeRoute({ |
| 101 | + summary: "Write log", |
| 102 | + description: "Write a log entry to the server logs with specified level and metadata.", |
| 103 | + operationId: "app.log", |
| 104 | + responses: { |
| 105 | + 200: { |
| 106 | + description: "Log entry written successfully", |
| 107 | + content: { |
| 108 | + "application/json": { |
| 109 | + schema: resolver(z.boolean()), |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + ...errors(400), |
| 114 | + }, |
| 115 | + }), |
| 116 | + validator( |
| 117 | + "json", |
| 118 | + z.object({ |
| 119 | + service: z.string().meta({ description: "Service name for the log entry" }), |
| 120 | + level: z.enum(["debug", "info", "error", "warn"]).meta({ description: "Log level" }), |
| 121 | + message: z.string().meta({ description: "Log message" }), |
| 122 | + extra: z |
| 123 | + .record(z.string(), z.any()) |
| 124 | + .optional() |
| 125 | + .meta({ description: "Additional metadata for the log entry" }), |
| 126 | + }), |
| 127 | + ), |
| 128 | + async (c) => { |
| 129 | + const { service, level, message, extra } = c.req.valid("json") |
| 130 | + const logger = Log.create({ service }) |
| 131 | + |
| 132 | + switch (level) { |
| 133 | + case "debug": |
| 134 | + logger.debug(message, extra) |
| 135 | + break |
| 136 | + case "info": |
| 137 | + logger.info(message, extra) |
| 138 | + break |
| 139 | + case "error": |
| 140 | + logger.error(message, extra) |
| 141 | + break |
| 142 | + case "warn": |
| 143 | + logger.warn(message, extra) |
| 144 | + break |
| 145 | + } |
| 146 | + |
| 147 | + return c.json(true) |
| 148 | + }, |
| 149 | + ) |
| 150 | +} |
0 commit comments