public commit

This commit is contained in:
dswbx
2024-11-16 12:01:47 +01:00
commit 90f80c4280
582 changed files with 49291 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
import type { ClassController } from "core";
import { SimpleRenderer } from "core";
import { FetchTask, Flow, LogTask } from "flows";
import { Hono } from "hono";
import { endTime, startTime } from "hono/timing";
import type { App } from "../../App";
export class AppController implements ClassController {
constructor(
private readonly app: App,
private config: any = {}
) {}
getController(): Hono {
const hono = new Hono();
// @todo: add test endpoints
hono
.get("/config", (c) => {
return c.json(this.app.toJSON());
})
.get("/ping", (c) => {
//console.log("c", c);
try {
// @ts-ignore @todo: fix with env
const context: any = c.req.raw.cf ? c.req.raw.cf : c.env.cf;
const cf = {
colo: context.colo,
city: context.city,
postal: context.postalCode,
region: context.region,
regionCode: context.regionCode,
continent: context.continent,
country: context.country,
eu: context.isEUCountry,
lat: context.latitude,
lng: context.longitude,
timezone: context.timezone
};
return c.json({ pong: true, cf, another: 6 });
} catch (e) {
return c.json({ pong: true, cf: null });
}
});
// test endpoints
if (this.config?.registerTest) {
hono.get("/test/kv", async (c) => {
// @ts-ignore
const cache = c.env!.CACHE as KVNamespace;
startTime(c, "kv-get");
const value: any = await cache.get("count");
endTime(c, "kv-get");
console.log("value", value);
startTime(c, "kv-put");
if (!value) {
await cache.put("count", "1");
} else {
await cache.put("count", (Number(value) + 1).toString());
}
endTime(c, "kv-put");
let cf: any = {};
// @ts-ignore
if ("cf" in c.req.raw) {
cf = {
// @ts-ignore
colo: c.req.raw.cf?.colo
};
}
return c.json({ pong: true, value, cf });
});
hono.get("/test/flow", async (c) => {
const first = new LogTask("Task 0");
const second = new LogTask("Task 1");
const third = new LogTask("Task 2", { delay: 250 });
const fourth = new FetchTask("Fetch Something", {
url: "https://jsonplaceholder.typicode.com/todos/1"
});
const fifth = new LogTask("Task 4"); // without connection
const flow = new Flow("flow", [first, second, third, fourth, fifth]);
flow.task(first).asInputFor(second);
flow.task(first).asInputFor(third);
flow.task(fourth).asOutputFor(third);
flow.setRespondingTask(fourth);
const execution = flow.createExecution();
await execution.start();
const results = flow.tasks.map((t) => t.toJSON());
return c.json({ results, response: execution.getResponse() });
});
hono.get("/test/template", async (c) => {
const renderer = new SimpleRenderer({ var: 123 });
const template = "Variable: {{ var }}";
return c.text(await renderer.render(template));
});
}
return hono;
}
}

View File

@@ -0,0 +1,143 @@
import { Exception, isDebug } from "core";
import { type Static, StringEnum, Type } from "core/utils";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { timing } from "hono/timing";
import { Module } from "modules/Module";
const serverMethods = ["GET", "POST", "PATCH", "PUT", "DELETE"];
export const serverConfigSchema = Type.Object(
{
admin: Type.Object(
{
basepath: Type.Optional(Type.String({ default: "", pattern: "^(/.+)?$" })),
color_scheme: Type.Optional(StringEnum(["dark", "light"], { default: "light" })),
logo_return_path: Type.Optional(
Type.String({
default: "/",
description: "Path to return to after *clicking* the logo"
})
)
},
{ default: {}, additionalProperties: false }
),
cors: Type.Object(
{
origin: Type.String({ default: "*" }),
allow_methods: Type.Array(StringEnum(serverMethods), {
default: serverMethods,
uniqueItems: true
}),
allow_headers: Type.Array(Type.String(), {
default: ["Content-Type", "Content-Length", "Authorization", "Accept"]
})
},
{ default: {}, additionalProperties: false }
)
},
{
additionalProperties: false
}
);
export type AppServerConfig = Static<typeof serverConfigSchema>;
/*declare global {
interface Request {
cf: IncomingRequestCfProperties;
}
}*/
export class AppServer extends Module<typeof serverConfigSchema> {
private admin_html?: string;
override getRestrictedPaths() {
return [];
}
get client() {
return this.ctx.server;
}
getSchema() {
return serverConfigSchema;
}
override async build() {
//this.client.use(timing());
/*this.client.use("*", async (c, next) => {
console.log(`[${c.req.method}] ${c.req.url}`);
await next();
});*/
this.client.use(
"*",
cors({
origin: this.config.cors.origin,
allowMethods: this.config.cors.allow_methods,
allowHeaders: this.config.cors.allow_headers
})
);
/*this.client.use(async (c, next) => {
c.res.headers.set("X-Powered-By", "BKND");
try {
c.res.headers.set("X-Colo", c.req.raw.cf.colo);
} catch (e) {}
await next();
});
this.client.use(async (c, next) => {
console.log(`[${c.req.method}] ${c.req.url}`);
await next();
});*/
this.client.onError((err, c) => {
//throw err;
console.error(err);
if (err instanceof Response) {
return err;
}
/*if (isDebug()) {
console.log("accept", c.req.header("Accept"));
if (c.req.header("Accept") === "application/json") {
const stack = err.stack;
if ("toJSON" in err && typeof err.toJSON === "function") {
return c.json({ ...err.toJSON(), stack }, 500);
}
return c.json({ message: String(err), stack }, 500);
} else {
throw err;
}
}*/
if (err instanceof Exception) {
console.log("---is exception", err.code);
return c.json(err.toJSON(), err.code as any);
}
return c.json({ error: err.message }, 500);
});
this.setBuilt();
}
setAdminHtml(html: string) {
this.admin_html = html;
const basepath = (String(this.config.admin.basepath) + "/").replace(/\/+$/, "/");
this.client.get(basepath + "*", async (c, next) => {
return c.html(this.admin_html!);
});
}
getAdminHtml() {
return this.admin_html;
}
override toJSON(secrets?: boolean) {
return this.config;
}
}

View File

@@ -0,0 +1,311 @@
/// <reference types="@cloudflare/workers-types" />
import type { ClassController } from "core";
import { tbValidator as tb } from "core";
import { StringEnum, Type, TypeInvalidError } from "core/utils";
import { type Context, Hono } from "hono";
import { MODULE_NAMES, type ModuleKey, getDefaultConfig } from "modules/ModuleManager";
import * as SystemPermissions from "modules/permissions";
import { generateOpenAPI } from "modules/server/openapi";
import type { App } from "../../App";
const booleanLike = Type.Transform(Type.String())
.Decode((v) => v === "1")
.Encode((v) => (v ? "1" : "0"));
export class SystemController implements ClassController {
constructor(private readonly app: App) {}
get ctx() {
return this.app.modules.ctx();
}
private registerConfigController(client: Hono<any>): void {
const hono = new Hono();
/*hono.use("*", async (c, next) => {
//this.ctx.guard.throwUnlessGranted(SystemPermissions.configRead);
console.log("perm?", this.ctx.guard.hasPermission(SystemPermissions.configRead));
return next();
});*/
hono.get(
"/:module?",
tb("param", Type.Object({ module: Type.Optional(StringEnum(MODULE_NAMES)) })),
tb(
"query",
Type.Object({
secrets: Type.Optional(booleanLike)
})
),
async (c) => {
// @todo: allow secrets if authenticated user is admin
const { secrets } = c.req.valid("query");
const { module } = c.req.valid("param");
secrets && this.ctx.guard.throwUnlessGranted(SystemPermissions.configReadSecrets);
const config = this.app.toJSON(secrets);
return c.json(
module
? {
version: this.app.version(),
module,
config: config[module]
}
: config
);
}
);
async function handleConfigUpdateResponse(c: Context<any>, cb: () => Promise<object>) {
try {
return c.json(await cb(), { status: 202 });
} catch (e) {
console.error(e);
if (e instanceof TypeInvalidError) {
return c.json({ success: false, errors: e.errors }, { status: 400 });
}
return c.json({ success: false }, { status: 500 });
}
}
hono.post(
"/set/:module",
tb(
"query",
Type.Object({
force: Type.Optional(booleanLike)
})
),
async (c) => {
const module = c.req.param("module") as any;
const { force } = c.req.valid("query");
const value = await c.req.json();
this.ctx.guard.throwUnlessGranted(SystemPermissions.configWrite);
return await handleConfigUpdateResponse(c, async () => {
// you must explicitly set force to override existing values
// because omitted values gets removed
if (force === true) {
await this.app.mutateConfig(module).set(value);
} else {
await this.app.mutateConfig(module).patch("", value);
}
return {
success: true,
module,
config: this.app.module[module].config
};
});
}
);
hono.post("/add/:module/:path", async (c) => {
// @todo: require auth (admin)
const module = c.req.param("module") as any;
const value = await c.req.json();
const path = c.req.param("path") as string;
this.ctx.guard.throwUnlessGranted(SystemPermissions.configWrite);
const moduleConfig = this.app.mutateConfig(module);
if (moduleConfig.has(path)) {
return c.json({ success: false, path, error: "Path already exists" }, { status: 400 });
}
console.log("-- add", module, path, value);
return await handleConfigUpdateResponse(c, async () => {
await moduleConfig.patch(path, value);
return {
success: true,
module,
config: this.app.module[module].config
};
});
});
hono.patch("/patch/:module/:path", async (c) => {
// @todo: require auth (admin)
const module = c.req.param("module") as any;
const value = await c.req.json();
const path = c.req.param("path");
this.ctx.guard.throwUnlessGranted(SystemPermissions.configWrite);
return await handleConfigUpdateResponse(c, async () => {
await this.app.mutateConfig(module).patch(path, value);
return {
success: true,
module,
config: this.app.module[module].config
};
});
});
hono.put("/overwrite/:module/:path", async (c) => {
// @todo: require auth (admin)
const module = c.req.param("module") as any;
const value = await c.req.json();
const path = c.req.param("path");
this.ctx.guard.throwUnlessGranted(SystemPermissions.configWrite);
return await handleConfigUpdateResponse(c, async () => {
await this.app.mutateConfig(module).overwrite(path, value);
return {
success: true,
module,
config: this.app.module[module].config
};
});
});
hono.delete("/remove/:module/:path", async (c) => {
// @todo: require auth (admin)
const module = c.req.param("module") as any;
const path = c.req.param("path")!;
this.ctx.guard.throwUnlessGranted(SystemPermissions.configWrite);
return await handleConfigUpdateResponse(c, async () => {
await this.app.mutateConfig(module).remove(path);
return {
success: true,
module,
config: this.app.module[module].config
};
});
});
client.route("/config", hono);
}
getController(): Hono {
const hono = new Hono();
this.registerConfigController(hono);
hono.get(
"/schema/:module?",
tb(
"query",
Type.Object({
config: Type.Optional(booleanLike),
secrets: Type.Optional(booleanLike)
})
),
async (c) => {
const module = c.req.param("module") as ModuleKey | undefined;
const { config, secrets } = c.req.valid("query");
this.ctx.guard.throwUnlessGranted(SystemPermissions.schemaRead);
config && this.ctx.guard.throwUnlessGranted(SystemPermissions.configRead);
secrets && this.ctx.guard.throwUnlessGranted(SystemPermissions.configReadSecrets);
const { version, ...schema } = this.app.getSchema();
if (module) {
return c.json({
module,
version,
schema: schema[module],
config: config ? this.app.module[module].toJSON(secrets) : undefined
});
}
return c.json({
module,
version,
schema,
config: config ? this.app.toJSON(secrets) : undefined,
permissions: this.app.modules.ctx().guard.getPermissionNames()
});
}
);
hono.post(
"/build",
tb(
"query",
Type.Object({
sync: Type.Optional(booleanLike),
drop: Type.Optional(booleanLike),
save: Type.Optional(booleanLike)
})
),
async (c) => {
const { sync, drop, save } = c.req.valid("query") as Record<string, boolean>;
this.ctx.guard.throwUnlessGranted(SystemPermissions.build);
await this.app.build({ sync, drop, save });
return c.json({ success: true, options: { sync, drop, save } });
}
);
hono.get("/ping", async (c) => {
//console.log("c", c);
try {
// @ts-ignore @todo: fix with env
const context: any = c.req.raw.cf ? c.req.raw.cf : c.env.cf;
const cf = {
colo: context.colo,
city: context.city,
postal: context.postalCode,
region: context.region,
regionCode: context.regionCode,
continent: context.continent,
country: context.country,
eu: context.isEUCountry,
lat: context.latitude,
lng: context.longitude,
timezone: context.timezone
};
return c.json({ pong: true });
} catch (e) {
return c.json({ pong: true });
}
});
hono.get("/info", async (c) => {
return c.json({
version: this.app.version(),
test: 2,
// @ts-ignore
app: !!c.var.app
});
});
hono.get("/openapi.json", async (c) => {
//const config = this.app.toJSON();
const config = JSON.parse(getDefaultConfig() as any);
return c.json(generateOpenAPI(config));
});
/*hono.get("/test/sql", async (c) => {
// @ts-ignore
const ai = c.env?.AI as Ai;
const messages = [
{ role: "system", content: "You are a friendly assistant" },
{
role: "user",
content: "just say hello"
}
];
const stream = await ai.run("@cf/meta/llama-3.1-8b-instruct", {
messages,
stream: true
});
return new Response(stream, {
headers: { "content-type": "text/event-stream" }
});
});*/
return hono;
}
}

View File

@@ -0,0 +1,312 @@
import { Type } from "core/utils";
import type { ModuleConfigs } from "modules/ModuleManager";
import type { OpenAPIV3 as OAS } from "openapi-types";
function prefixPaths(paths: OAS.PathsObject, prefix: string): OAS.PathsObject {
const result: OAS.PathsObject = {};
for (const [path, pathItem] of Object.entries(paths)) {
result[`${prefix}${path}`] = pathItem;
}
return result;
}
function systemRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
const tags = ["system"];
const paths: OAS.PathsObject = {
"/ping": {
get: {
summary: "Ping",
responses: {
"200": {
description: "Pong",
content: {
"application/json": {
schema: Type.Object({
pong: Type.Boolean({ default: true })
})
}
}
}
},
tags
}
},
"/config": {
get: {
summary: "Get config",
responses: {
"200": {
description: "Config",
content: {
"application/json": {
schema: Type.Object({
version: Type.Number() as any,
server: Type.Object({}),
data: Type.Object({}),
auth: Type.Object({}),
flows: Type.Object({}),
media: Type.Object({})
})
}
}
}
},
tags
}
},
"/schema": {
get: {
summary: "Get config",
responses: {
"200": {
description: "Config",
content: {
"application/json": {
schema: Type.Object({
version: Type.Number() as any,
schema: Type.Object({
server: Type.Object({}),
data: Type.Object({}),
auth: Type.Object({}),
flows: Type.Object({}),
media: Type.Object({})
})
})
}
}
}
},
tags
}
}
};
return { paths: prefixPaths(paths, "/api/system") };
}
function dataRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
const schemas = {
entityData: Type.Object({
id: Type.Number() as any
})
};
const repoManyResponses: OAS.ResponsesObject = {
"200": {
description: "List of entities",
content: {
"application/json": {
schema: Type.Array(schemas.entityData)
}
}
}
};
const repoSingleResponses: OAS.ResponsesObject = {
"200": {
description: "Entity",
content: {
"application/json": {
schema: schemas.entityData
}
}
}
};
const params = {
entity: {
name: "entity",
in: "path",
required: true,
schema: Type.String()
},
entityId: {
name: "id",
in: "path",
required: true,
schema: Type.Number() as any
}
};
const tags = ["data"];
const paths: OAS.PathsObject = {
"/{entity}": {
get: {
summary: "List entities",
parameters: [params.entity],
responses: repoManyResponses,
tags
},
post: {
summary: "Create entity",
parameters: [params.entity],
requestBody: {
content: {
"application/json": {
schema: Type.Object({})
}
}
},
responses: repoSingleResponses,
tags
}
},
"/{entity}/{id}": {
get: {
summary: "Get entity",
parameters: [params.entity, params.entityId],
responses: repoSingleResponses,
tags
},
patch: {
summary: "Update entity",
parameters: [params.entity, params.entityId],
requestBody: {
content: {
"application/json": {
schema: Type.Object({})
}
}
},
responses: repoSingleResponses,
tags
},
delete: {
summary: "Delete entity",
parameters: [params.entity, params.entityId],
responses: {
"200": {
description: "Entity deleted"
}
},
tags
}
}
};
return { paths: prefixPaths(paths, config.data.basepath!) };
}
function authRoutes(config: ModuleConfigs): { paths: OAS.Document["paths"] } {
const schemas = {
user: Type.Object({
id: Type.String(),
email: Type.String(),
name: Type.String()
})
};
const tags = ["auth"];
const paths: OAS.PathsObject = {
"/password/login": {
post: {
summary: "Login",
requestBody: {
content: {
"application/json": {
schema: Type.Object({
email: Type.String(),
password: Type.String()
})
}
}
},
responses: {
"200": {
description: "User",
content: {
"application/json": {
schema: Type.Object({
user: schemas.user
})
}
}
}
},
tags
}
},
"/password/register": {
post: {
summary: "Register",
requestBody: {
content: {
"application/json": {
schema: Type.Object({
email: Type.String(),
password: Type.String()
})
}
}
},
responses: {
"200": {
description: "User",
content: {
"application/json": {
schema: Type.Object({
user: schemas.user
})
}
}
}
},
tags
}
},
"/me": {
get: {
summary: "Get me",
responses: {
"200": {
description: "User",
content: {
"application/json": {
schema: Type.Object({
user: schemas.user
})
}
}
}
},
tags
}
},
"/strategies": {
get: {
summary: "Get auth strategies",
responses: {
"200": {
description: "Strategies",
content: {
"application/json": {
schema: Type.Object({
strategies: Type.Object({})
})
}
}
}
},
tags
}
}
};
return { paths: prefixPaths(paths, config.auth.basepath!) };
}
export function generateOpenAPI(config: ModuleConfigs): OAS.Document {
const system = systemRoutes(config);
const data = dataRoutes(config);
const auth = authRoutes(config);
return {
openapi: "3.1.0",
info: {
title: "bknd API",
version: "0.0.0"
},
paths: {
...system.paths,
...data.paths,
...auth.paths
}
};
}