mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
One path: /v1/*. We do not use /api/ — that's Casdoor's path. The
BASE_API_PREFIX env knob is gone; the prefix is hard-coded in
apis/base.go and apis/serve.go banner.
Server-side mounts:
- apis: bindHealthApi, bindCollectionApi, bindRecordCrudApi, etc. all
attach under r.Group("/v1") (was r.Group(BASE_API_PREFIX, default /api))
- platform plugin: /api/platform/* -> /v1/platform/* (including auth and
org sub-groups)
- kms plugin: /api/kms/* -> /v1/kms/*
- functions plugin: /api/functions/* -> /v1/functions/*
- cloudsql plugin: /api/cloud-sql/*, /api/meta/* -> /v1/*
- superuser OIDC: /_/api/oidc/config -> /_/v1/oidc/config
- core settings: default RateLimitRule labels /api/batch, /api/ ->
/v1/batch, /v1/
- apis/batch.go: ValidBatchActions regexes match /v1/collections/*;
internal rewrite targets /v1/collections/* (was /v1/base/collections/*,
a stale artifact)
Clients:
- sdk/base-js (BaseClient.collections, files, realtime, CRDT WS,
signIn/refresh/health) targets /v1/*
- sdk/base-react (use-crdt WS path) targets /v1/crdt
- ui-react admin (lib/api.ts, settings.application copy) targets /v1/*
- cmd/cli (NewClient.APIPrefix) defaults to /v1; client_test.go and
cmd/cli_test.go stubServer use /v1/*
- cmd/typegen calls /v1/collections
Upstream paths untouched: Casdoor IAM (/api/login, /api/userinfo,
/api/signup, /api/send-verification-code, /api/get-user), KMS Infisical
(/api/v1/auth/universal-auth/login, /api/v2/workspace/environments),
Kubernetes tasks (/api/v1/namespaces/...), IPFS (/api/v0/...), Gitea/
GitLab/Discord/Strava/Patreon/Box/Wakatime upstream APIs. Those are
their contracts, not ours.
Tests: all 437 prior /api/ test URLs across apis/*_test.go and
plugins/platform/auth_proxy_test.go updated to /v1/. Build + go vet
clean. ./apis ./core ./plugins/platform ./plugins/jsvm ./forms all
green. Pre-existing red probes (network/TestRedProbe_*,
migratecmd/TestAutomigrateCollection*) unchanged.
254 lines
6.8 KiB
Go
254 lines
6.8 KiB
Go
package functions
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
// handleDeployFunction handles POST /v1/functions
|
|
func (p *plugin) handleDeployFunction(e *core.RequestEvent) error {
|
|
if e.Auth == nil {
|
|
return e.UnauthorizedError("authentication required", nil)
|
|
}
|
|
|
|
var input struct {
|
|
Name string `json:"name"`
|
|
Image string `json:"image"`
|
|
Runtime string `json:"runtime"`
|
|
Handler string `json:"handler"`
|
|
EnvVars map[string]string `json:"envVars"`
|
|
Memory string `json:"memory"`
|
|
CPU string `json:"cpu"`
|
|
}
|
|
if err := e.BindBody(&input); err != nil {
|
|
return e.BadRequestError("invalid request body", err)
|
|
}
|
|
if input.Name == "" {
|
|
return e.BadRequestError("function name is required", nil)
|
|
}
|
|
|
|
tenantSlug := e.Auth.GetString("tenantSlug")
|
|
if tenantSlug == "" {
|
|
tenantSlug = "default"
|
|
}
|
|
|
|
qualifiedName := fmt.Sprintf("t-%s-%s", tenantSlug, input.Name)
|
|
|
|
// Build deploy request with default env vars
|
|
envVars := map[string]string{
|
|
"IAM_URL": "https://hanzo.id",
|
|
"STORAGE_URL": "http://s3.hanzo.svc:9000",
|
|
}
|
|
for k, v := range input.EnvVars {
|
|
envVars[k] = v
|
|
}
|
|
|
|
req := DeployRequest{
|
|
Service: qualifiedName,
|
|
Image: input.Image,
|
|
Namespace: p.config.FunctionNamespace,
|
|
Labels: map[string]string{
|
|
"hanzo.ai/tenant": tenantSlug,
|
|
"hanzo.ai/function": input.Name,
|
|
},
|
|
EnvVars: envVars,
|
|
}
|
|
if input.Memory != "" || input.CPU != "" {
|
|
req.Limits = &FunctionLimits{Memory: input.Memory, CPU: input.CPU}
|
|
}
|
|
|
|
if err := p.deployFunction(req); err != nil {
|
|
return e.InternalServerError("failed to deploy function", err)
|
|
}
|
|
|
|
// Persist to system collection
|
|
col, err := p.app.FindCollectionByNameOrId(collectionFunctions)
|
|
if err != nil {
|
|
return e.InternalServerError("functions collection not found", err)
|
|
}
|
|
|
|
record := core.NewRecord(col)
|
|
record.Set("name", input.Name)
|
|
record.Set("qualifiedName", qualifiedName)
|
|
record.Set("tenantId", tenantSlug)
|
|
record.Set("image", input.Image)
|
|
record.Set("runtime", input.Runtime)
|
|
record.Set("handler", input.Handler)
|
|
record.Set("status", "deployed")
|
|
|
|
if err := p.app.Save(record); err != nil {
|
|
return e.InternalServerError("failed to save function record", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusCreated, map[string]any{
|
|
"name": input.Name,
|
|
"qualifiedName": qualifiedName,
|
|
"status": "deployed",
|
|
})
|
|
}
|
|
|
|
// handleListFunctions handles GET /v1/functions
|
|
func (p *plugin) handleListFunctions(e *core.RequestEvent) error {
|
|
if e.Auth == nil {
|
|
return e.UnauthorizedError("authentication required", nil)
|
|
}
|
|
|
|
tenantSlug := e.Auth.GetString("tenantSlug")
|
|
if tenantSlug == "" {
|
|
tenantSlug = "default"
|
|
}
|
|
|
|
functions, err := p.listFunctions(p.config.FunctionNamespace)
|
|
if err != nil {
|
|
return e.InternalServerError("failed to list functions", err)
|
|
}
|
|
|
|
prefix := fmt.Sprintf("t-%s-", tenantSlug)
|
|
result := make([]map[string]any, 0)
|
|
for _, fn := range functions {
|
|
if strings.HasPrefix(fn.Name, prefix) {
|
|
result = append(result, map[string]any{
|
|
"name": strings.TrimPrefix(fn.Name, prefix),
|
|
"qualifiedName": fn.Name,
|
|
"replicas": fn.Replicas,
|
|
"availableReplicas": fn.AvailableReplicas,
|
|
"invocationCount": fn.InvocationCount,
|
|
"image": fn.Image,
|
|
})
|
|
}
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// handleGetFunction handles GET /v1/functions/{name}
|
|
func (p *plugin) handleGetFunction(e *core.RequestEvent) error {
|
|
if e.Auth == nil {
|
|
return e.UnauthorizedError("authentication required", nil)
|
|
}
|
|
|
|
name := e.Request.PathValue("name")
|
|
tenantSlug := e.Auth.GetString("tenantSlug")
|
|
if tenantSlug == "" {
|
|
tenantSlug = "default"
|
|
}
|
|
|
|
qualifiedName := fmt.Sprintf("t-%s-%s", tenantSlug, name)
|
|
|
|
fn, err := p.getFunction(qualifiedName, p.config.FunctionNamespace)
|
|
if err != nil {
|
|
return e.InternalServerError("failed to get function", err)
|
|
}
|
|
if fn == nil {
|
|
return e.NotFoundError("function not found", nil)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]any{
|
|
"name": name,
|
|
"qualifiedName": fn.Name,
|
|
"replicas": fn.Replicas,
|
|
"availableReplicas": fn.AvailableReplicas,
|
|
"invocationCount": fn.InvocationCount,
|
|
"image": fn.Image,
|
|
"envVars": fn.EnvVars,
|
|
"labels": fn.Labels,
|
|
})
|
|
}
|
|
|
|
// handleDeleteFunction handles DELETE /v1/functions/{name}
|
|
func (p *plugin) handleDeleteFunction(e *core.RequestEvent) error {
|
|
if e.Auth == nil {
|
|
return e.UnauthorizedError("authentication required", nil)
|
|
}
|
|
|
|
name := e.Request.PathValue("name")
|
|
tenantSlug := e.Auth.GetString("tenantSlug")
|
|
if tenantSlug == "" {
|
|
tenantSlug = "default"
|
|
}
|
|
|
|
qualifiedName := fmt.Sprintf("t-%s-%s", tenantSlug, name)
|
|
|
|
if err := p.deleteFunction(qualifiedName, p.config.FunctionNamespace); err != nil {
|
|
return e.InternalServerError("failed to delete function", err)
|
|
}
|
|
|
|
// Remove from system collection
|
|
records, err := p.app.FindRecordsByFilter(
|
|
collectionFunctions,
|
|
"qualifiedName = {:qname}",
|
|
"",
|
|
1,
|
|
0,
|
|
dbx.Params{"qname": qualifiedName},
|
|
)
|
|
if err == nil && len(records) > 0 {
|
|
_ = p.app.Delete(records[0])
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]string{"status": "deleted"})
|
|
}
|
|
|
|
// handleInvokeFunction handles POST /v1/functions/{name}/invoke
|
|
func (p *plugin) handleInvokeFunction(e *core.RequestEvent) error {
|
|
if e.Auth == nil {
|
|
return e.UnauthorizedError("authentication required", nil)
|
|
}
|
|
|
|
name := e.Request.PathValue("name")
|
|
tenantSlug := e.Auth.GetString("tenantSlug")
|
|
if tenantSlug == "" {
|
|
tenantSlug = "default"
|
|
}
|
|
|
|
qualifiedName := fmt.Sprintf("t-%s-%s", tenantSlug, name)
|
|
|
|
payload, err := io.ReadAll(e.Request.Body)
|
|
if err != nil {
|
|
return e.BadRequestError("failed to read request body", err)
|
|
}
|
|
|
|
async := e.Request.URL.Query().Get("async") == "true"
|
|
|
|
body, statusCode, err := p.invokeFunction(qualifiedName, payload, async)
|
|
if err != nil {
|
|
return e.InternalServerError("failed to invoke function", err)
|
|
}
|
|
|
|
if async {
|
|
return e.JSON(http.StatusAccepted, map[string]string{"status": "queued"})
|
|
}
|
|
|
|
e.Response.Header().Set("Content-Type", "application/json")
|
|
e.Response.WriteHeader(statusCode)
|
|
_, _ = e.Response.Write(body)
|
|
return nil
|
|
}
|
|
|
|
// handleGetLogs handles GET /v1/functions/{name}/logs
|
|
func (p *plugin) handleGetLogs(e *core.RequestEvent) error {
|
|
if e.Auth == nil {
|
|
return e.UnauthorizedError("authentication required", nil)
|
|
}
|
|
|
|
name := e.Request.PathValue("name")
|
|
tenantSlug := e.Auth.GetString("tenantSlug")
|
|
if tenantSlug == "" {
|
|
tenantSlug = "default"
|
|
}
|
|
|
|
qualifiedName := fmt.Sprintf("t-%s-%s", tenantSlug, name)
|
|
|
|
// OpenFaaS CE does not have a native logs endpoint.
|
|
// Return kubectl command for now; wire to K8s API in future.
|
|
return e.JSON(http.StatusOK, map[string]any{
|
|
"name": name,
|
|
"message": "Logs available via: kubectl logs -l faas_function=" + qualifiedName + " -n " + p.config.FunctionNamespace,
|
|
})
|
|
}
|