# script-admin **Repository Path**: chanterchen/script-admin ## Basic Information - **Project Name**: script-admin - **Description**: 多脚本引擎管理后台(script-admin) 基于 Go + Iris 的后台管理系统,内置 **JS / Lua / SQL / Tengo** 四种脚本运行时, 支持脚本在线管理、脚本自定义 API、可视化代码生成器(Web + 移动端双产物) - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-10 - **Last Updated**: 2026-11-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Multi-Script-Engine Admin (script-admin) > A Go + Iris based admin backend with built-in **JS / Lua / SQL / Tengo** script runtimes, > supporting online script management, script-as-API, and a visual code generator (Web + mobile), > plus system / script AI assistants, an operations manual, and an MCP control plane, > with a Vue3 + Element-Plus web UI and a uni-app mobile app. - Backend: Go 1.26.8+ / [Iris](https://www.iris-go.com/) v12 - Frontend: `frontend/`, Vue3 + Element-Plus + Vite (RuoYi-Vue flavored) - Mobile: `mobile/`, uni-app (H5 / WeChat Mini-Program) - License: [Apache License 2.0](./LICENSE) --- ## Features - **Multi script runtimes**: JS (Goja), Lua (Gopher-Lua), SQL templates, and Tengo — each with an isolated per-request VM pool. - **Script-as-API**: Expose scripts as HTTP endpoints (incl. PATCH) with configurable params, auth, and row-level guards. - **Code generator**: Generate list / form / detail / tree-table code from DB tables, for both **Web** and **uni-app mobile**, with syntax-highlighted preview. - **System management**: Users, roles, menus, depts, posts, dictionaries, scheduled jobs, notices, and other standard admin modules. - **AI assistants**: a system assistant (read-only queries + manual/API retrieval, permission `system:ai:chat`) and a script assistant (script lookup, validation, confirm-before-execute writes), served over SSE with server-side session persistence. - **Operations manual**: reads `docs/manual/*.md` at runtime (`GET /system/manual`), rendered at the frontend `/manual` page and indexed by the AI assistant. - **MCP control plane**: `POST /system/script/mcp` (HTTP JSON-RPC) and `cmd/mcp-server` (stdio), sharing one set of script tool definitions with the AI assistant. - **Observability**: Data-access-layer monitoring (slow query / P95 / P99 sampling), structured logs, Swagger / Knife4j docs. - **Security boundaries**: Script file-dir allowlist (GFile), HTTP target allowlist (GHttp, SSRF protection), JWT, Swagger BasicAuth. --- ## Tech Stack | Layer | Tech | |-------|------| | Backend | Go 1.26, Iris v12, GORM, Zap, go-redis | | Scripts | goja (JS) / gopher-lua (Lua) / Tengo / SQL templates | | Frontend | Vue 3, Element-Plus, Pinia, Vite, Axios | | Mobile | uni-app 3.x, Pinia, Vue 3 | | Storage | MySQL / PostgreSQL / MSSQL / SQLite (MySQL 8 default) + Redis | | AI | OpenAI-compatible model service (SSE streaming), session persistence, MCP (JSON-RPC) | | Docs | Swagger (`docs/swagger.json` / `.yaml`) + Knife4j UI (`knife4g/`) | | Deploy | Docker multi-stage build + docker-compose | --- ## Directory Layout ``` script-admin/ ├── main.go # backend entry (Iris bootstrap, graceful shutdown) ├── conf/ # config loading ├── controllers/ # HTTP handlers ├── ai/ # AI chat, model client, tool registry (system_* read-only tools) ├── mcp/ # MCP adapter (tool definitions / JSON-RPC / stdio·HTTP transports) ├── cmd/ # standalone commands: mcp-server / migrate / pending-gc / addbuildmenu ├── service/ # business logic ├── model/ # data models ├── datasource/ # datasource / ORM row guard ├── vm/ # script runtimes (js/lua/tengo) ├── route/ # route registration ├── middleware/ # middlewares (auth/CORS/log...) ├── cache/ # cache layer (local / redis source) ├── scriptauth/ scriptctl/ scriptgate/ scriptaudit/ # script control plane (auth / plan / approval / audit) ├── script/ # script samples & stdlib (js/lua/sql/tengo) │ ├── json/init/ # system seed data (menus/dicts/users...) ├── tmpl/ # code-gen templates (web/mobile) ├── gen/ # code-gen related ├── sql/ # schema / SQL execution ├── init/ # engine init scripts ├── docs/ # Swagger artifacts + manual (docs/manual) + design/env docs ├── knife4g/ # Knife4j doc frontend ├── frontend/ # Vue3 + Element-Plus frontend ├── mobile/ # uni-app mobile ├── config.example.json5 # config template (secret placeholders) ├── Dockerfile # backend multi-stage build ├── docker-compose.yml # frontend+backend orchestration └── makefile # common build / test targets ``` --- ## Requirements | Dep | Version | Note | |-----|---------|------| | Go | 1.26.8+ | see `go.mod` | | Node.js | 18+ | frontend / mobile build | | MySQL | 8.0+ | default datasource (PG/MSSQL/SQLite also supported) | | Redis | 6+ | cache (or switch to local cache) | > MySQL and Redis are **external resources**: `docker-compose.yml` does not create them. > Provide your own instances and fill them into the config. --- ## Quick Start ### 1. Backend ```bash # prepare config (template has placeholders; edit as needed) cp config.example.json5 config.json5 # replace JWT / DB / Redis / Swagger secrets in config.json5 (see Security) # build make build # output bin/script-admin # or directly go build -o bin/script-admin . # run (config.json5 must be in the working directory) ./bin/script-admin # or dev mode go run . ``` Backend listens on `127.0.0.1:8000` by default. On first start it runs schema migration and imports `script/json/init` seed data — ensure the database exists and the account has DDL rights. ### 2. Frontend ```bash cd frontend npm install npm run dev # Vite dev server (default http://localhost:5175) # production build npm run build:prod # output frontend/dist/ ``` The frontend proxies `/dev-api` to the backend at `127.0.0.1:8000` via Vite. ### 3. Mobile (uni-app) ```bash cd mobile npm install npm run dev:h5 # H5 debug npm run dev:mp-weixin # WeChat Mini-Program (open mobile/dist/build/mp-weixin in DevTools) # production build npm run build:h5 npm run build:mp-weixin ``` --- ## Configuration Config is **JSON5** (`config.example.json5` is the template; `config.json5` is the private local config and is already git-ignored). Key fields: | Config | Description | |--------|-------------| | `common.host` / `common.port` | listen address / port (default `127.0.0.1:8000`) | | `common.env` | `dev` / `test` / `prod`; in prod, debug endpoints like `/login/simple` are disabled | | `common.gfile` | extra script file-dir allowlist (off by default; only each script dir allowed) | | `common.ghttp` | script GHttp target URL allowlist (SSRF protection; recommended in prod) | | `common.cors` | CORS config; **tighten `origins` in prod, never use `*`** | | `common.jwt.key` | JWT signing secret; **must be a strong random value** | | `swagger.basic_auth` | Swagger password; **change the default** | | `db[]` | DB connections (mysql/mssql/postgres/sqlite) | | `cache` | cache type `redis` / `local` | | `license.vendor` / `license.contact` | vendor name and contact shown in license-expired / missing-SN messages (replace before delivery) | | `ai` | OpenAI-compatible model service: `base_url` / `model` / `api_key` (inject via `SCRIPT_ADMIN_AI_API_KEY`) / timeout / output cap / quota gate; assistants show an empty state when unset | | `js` / `lua` / `sql` / `tengo` | engine toggle, VM pool size, script path, init scripts | Full field reference with comments: `config.example.json5`. --- ## Database Initialization 1. Create a database (e.g. `script`), charset `utf8mb4`. 2. Fill the DB account in `config.json5` (least-privilege app account scoped to `script.*` recommended). 3. Start the backend; it auto-runs: - schema migration (`sql/` package); - system seed import (`script/json/init/*.json5`: menus, dicts, roles, users, posts, depts, jobs, notices). 4. Default admin account is in the seed data (`sys_user.json5`); change the initial password before use. --- ## Docker Deployment ```bash # build & start (backend + frontend containers; MySQL/Redis are external) docker compose build docker compose up -d # inject secrets at runtime (recommended over editing files): docker compose up -d \ -e SCRIPT_ADMIN_JWT_KEY=*** \ -e SCRIPT_ADMIN_DB_PASSWORD=*** \ -e SCRIPT_ADMIN_REDIS_PASSWORD=*** \ -e SCRIPT_ADMIN_SWAGGER_USER=*** \ -e SCRIPT_ADMIN_SWAGGER_PASS=*** ``` - Frontend container exposes `${WEB_PORT:-80}` as the public entry (put it behind CLB / WAF); - Backend container does not publish a host port — accessed only via the frontend Nginx reverse proxy (port allowlist); - Uploads (`profile/`) and logs (`logs/`) are persisted via volumes. > Note: `go.mod` declares Go 1.26.8 while `Dockerfile` still uses `golang:1.25-alpine`; bump the base image to 1.26+ before building. --- ## Script Engines | Engine | Config | Note | |--------|--------|------| | JavaScript | `js` | Goja-based, on by default, configurable VM pool | | Lua | `lua` | Gopher-Lua, configurable call-stack / registry size | | SQL | `sql` | SQL template scripts | | Tengo | `tengo` | Tengo scripting language | Scripts live under `script/{js,lua,sql,tengo}/src`; stdlib (gcache/gorm/ghttp, etc.) and docs are in each `docs/` subdir. Script-as-API is declared via `api.json` / `model.json`, loaded from the `conf.api` path. --- ## AI Assistants and Operations Manual The system ships two mutually isolated AI assistants, both streamed over SSE with server-side session persistence: | Entry | Frontend | Backend | Capability | |-------|----------|---------|------------| | System assistant | `/tool/system-assistant` | `POST /system/ai/chat` (permission `system:ai:chat`) | Read-only queries over users / roles / menus / dictionaries / scheduled jobs, plus manual and API retrieval | | Script assistant | script console sidebar | `POST /system/script/ai/chat` | Script listing / version / validation; writes become confirm-before-execute cards | - Session APIs: `GET/DELETE /system/ai/sessions`, `GET/DELETE /system/script/ai/sessions` (isolated per JWT user); - The model comes from the `ai` config block (OpenAI-compatible service); assistants show an empty state instead of an error when unset; - If the upstream service does not emit tokens incrementally, the frontend typewriter (`frontend/src/utils/typewriter.js`) spreads the full text frame by frame so it still reads as streaming. Operations manual: - Docs live in `docs/manual/*.md` (`system-manual.md` for system operations, `engine-guide.md` for engines); - The backend reads them at runtime and serves `GET /system/manual` (JWT-protected); the frontend `/manual` page renders it, with an entry on the home page; - `ai/tools/system_doc.go` indexes `docs/manual/*.md` so the assistant can retrieve the manual before answering. --- ## MCP Control Plane The script control plane is also exposed as MCP (Model Context Protocol), with a single set of tool definitions (`mcp/tools.go`): - HTTP: `POST /system/script/mcp` (JSON-RPC `tools/call`, JWT / access token auth); - stdio: `SCRIPT_ADMIN_MCP_STDIO=1 SCRIPT_ADMIN_MCP_CLIENT_ID=... SCRIPT_ADMIN_MCP_CLIENT_SECRET=... go run ./cmd/mcp-server` (run from the repo root; shares `config.json5` / DB / Redis with the main service, which must have initialized first); - Authorization goes through `scriptauth.Authorize`, and every tool call is audited (`trigger_type=mcp`). --- ## Code Generator Under "System Tools → Code Generation", generates from table schema: - **Web**: list / form / detail / tree-table Vue components; - **Mobile**: uni-app list / form / detail / tree-table, with syntax-highlighted preview and native dict / range-filter support. The target end (`web` / `mobile` / `both`) is persisted in the table config's `options.target`; preview and download support temporary switching. --- ## API Docs - Swagger: `docs/swagger.json` / `docs/swagger.yaml` (BasicAuth protected, see `swagger` config); - Knife4j-style doc frontend: `knife4g/`. Regenerate Swagger: `make swag`. --- ## Testing ```bash make test # backend unit tests (with race detector) make covercheck # coverage gate (scripts/coverage_gate.py + coverage_baseline.txt) make ci # vet + test + coverage gate make ci-full # ci + frontend build check make frontend # frontend build check make check # vet + test + frontend ``` --- ## Security Notes > ⚠️ Always deploy with the `config.example.json5` template + a private `config.json5`. - Set `common.env` to `prod` in production; - Tighten `common.cors.origins` to real frontend domains, never `*`; - Enable `common.ghttp` allowlist to prevent SSRF from untrusted script inputs; - For sensitive tables, enable `conf.api.row_guard_tables` row-level guards on script ORM access; - Never commit `config.json5`; keep secret-bearing local config on disk only. --- ## Commercial Use Released under the **Apache License 2.0** — free to use, modify, redistribute, and use commercially, provided you retain copyright notices and the `LICENSE` file, and state modifications in derivatives. --- ## License [Apache License 2.0](./LICENSE)