- Go
1.24.x(project usesgo 1.24.13) - MySQL and Redis
makeand standard Go toolchain (gofmt,go test)
git clone https://github.com/seakee/dudu-admin-api.git
cd dudu-admin-api
go mod download
cp bin/configs/local.json.default bin/configs/local.json
# edit bin/configs/local.json
make build
make runbin/data/sql/{dialect}/init.sql already contains full schema and admin seed data. Use scripts/init-project.sh as the single recommended entry:
# Run inside repository: initialize the current repository interactively
./scripts/init-project.sh
# Remote download and run: generate the default ./dudu-admin-api directory
curl -fsSL https://raw.githubusercontent.com/seakee/dudu-admin-api/main/scripts/init-project.sh -o init-project.sh
bash init-project.sh
# Remote download and generate a custom project
bash init-project.sh --project-name my-api --module-name github.com/acme/my-api
# Non-interactive mode: generate and initialize a new project
bash init-project.sh --non-interactive --yes \
--project-name my-api \
--module-name github.com/acme/my-api \
--dialect postgres \
--db-host 127.0.0.1 --db-port 5432 \
--db-name my-api --db-user my-api --db-password 'your-password'
# Non-interactive mode: initialize an existing repository
bash init-project.sh --non-interactive --yes \
--project-dir ./dudu-admin-api --skip-clone \
--dialect postgres \
--db-host 127.0.0.1 --db-port 5432 \
--db-name dudu-admin-api --db-user dudu-admin-api --db-password 'your-password'If --module-name is not a remote repository path and you are not running inside the template repository, pass --repo-url explicitly.
The script generates a minimal runnable config file (bin/configs/{RUN_ENV}.json) including:
system.namesystem.route_prefixsystem.run_modesystem.http_portsystem.default_langsystem.jwt_secretsystem.admin.jwt_secret
Notes:
- The effective route prefix is configured by
system.route_prefix. - If the frontend project uses
dudu-admin, keepVITE_API_ROUTE_PREFIXaligned with the effective route prefix to avoid drift between API docs, dev proxy settings, and server routes. - When
--admin-passwordis provided, the script storesbcrypt(md5(plain_password))and clears the preset TOTP state foruser_id=1. - Before executing
init.sql, the script rewrites seeded RBAC permission paths to the effective prefix. - If
--configwrites to a custom path, start the service withAPP_CONFIG_PATH=/path/to/config.json.
PostgreSQL example:
bash init-project.sh \
--non-interactive --yes \
--project-dir ./dudu-admin-api \
--repo-url https://github.com/seakee/dudu-admin-api.git \
--repo-ref main \
--env local \
--dialect postgres \
--name dudu-admin-api \
--route-prefix dudu-admin-api \
--run-mode release \
--http-port :8080 \
--default-lang zh-CN \
--db-host 127.0.0.1 \
--db-port 5432 \
--db-name dudu-admin-api \
--db-user dudu-admin-api \
--db-password 'CHANGE_ME_DB_PASSWORD' \
--db-ssl-mode disable \
--db-timezone Asia/Shanghai \
--redis-host 127.0.0.1:6379 \
--redis-auth '' \
--redis-db 0 \
--admin-email admin@example.com \
--admin-phone 13800000000 \
--admin-username admin \
--admin-password 'CHANGE_ME_ADMIN_PASSWORD'MySQL example:
bash init-project.sh \
--non-interactive --yes \
--project-dir ./dudu-admin-api \
--repo-url https://github.com/seakee/dudu-admin-api.git \
--repo-ref main \
--env local \
--dialect mysql \
--name dudu-admin-api \
--route-prefix dudu-admin-api \
--run-mode release \
--http-port :8080 \
--default-lang zh-CN \
--db-host 127.0.0.1 \
--db-port 3306 \
--db-name dudu-admin-api \
--db-user dudu-admin-api \
--db-password 'CHANGE_ME_DB_PASSWORD' \
--redis-host 127.0.0.1:6379 \
--redis-auth '' \
--redis-db 0 \
--admin-email admin@example.com \
--admin-phone 13800000000 \
--admin-username admin \
--admin-password 'CHANGE_ME_ADMIN_PASSWORD'If repository already exists in target path, append:
--project-dir ./dudu-admin-api --skip-cloneNote: init.sql resets related tables (includes DROP TABLE). Do not run directly in production.
If the PostgreSQL application database is already provisioned but the managed environment does not expose the postgres maintenance database, the script checks target-database connectivity first. If you explicitly do not want the script to attempt database creation, append --no-create-db.
bin/configs/local.jsonbin/configs/dev.jsonbin/configs/prod.json
RUN_ENV: selectsbin/configs/{RUN_ENV}.json, defaultlocalAPP_CONFIG_PATH: explicit config file path, higher priority thanRUN_ENVAPP_NAME: overridessystem.nameat runtime
main.go -> bootstrap.NewApp -> App.Start -> bootstrap/http.go -> app/http/router
Model -> Repository -> Service -> Controller
- Controller must not call repository directly.
- Service should accept
context.Contextand should not handle Gin request/response details. - Repository should focus on persistence, not business orchestration.
Prefer model-layer helpers first (such as Where, First, List, Create, Updates, Count, FindWithPagination, BatchInsert).
Direct db.WithContext(ctx)... in repository is only for:
- transactions (
Transaction(...)) - queries not covered by model helpers (for example
Select,Pluck,Joins)
The effective route prefix is configured by system.route_prefix; the default value is dudu-admin-api.
/{apiPrefix}/external/.../{apiPrefix}/internal/.../{apiPrefix}/internal/admin/.../{apiPrefix}/internal/service/...
When adding endpoints, define:
- auth middleware (
CheckAppAuthand/orCheckAdminAuth) - operation logging (
SaveOperationRecord) requirement - sensitive payload redaction requirement
- Define or update model
- Implement repository with model helpers
- Implement service business logic
- Add controller handlers
- Register routes under the correct module path
- Add tests
- Update bilingual docs and error-code/i18n mapping when user-facing behavior changes
Generator entry: command/codegen/handler.go
# Generate from one SQL file name
go run ./command/codegen/handler.go -name auth_app
# Generate one PostgreSQL SQL file
go run ./command/codegen/handler.go -dialect=postgres -sql bin/data/sql/postgres -name oauth_app
# Generate from all SQL files
go run ./command/codegen/handler.goDefault SQL directory:
bin/data/sql
After generation, complete manual integration:
- service layer wiring
- controller/router wiring
- tests
- docs and error-code updates when needed
Minimum verification before finalizing changes:
gofmt -w .
go test ./...Recommended for medium/large changes:
go test -race ./...make fmt
make test
make build
make runShell script alternative:
./scripts/make.sh all
./scripts/make.sh run