go-admingo-admin
  • Guide
  • Development
    • Advanced
    • Commands
  • Advanced
  • Help
  • GitHub
  • Changelog
⌘ K
Standard Practice
Standard Module Development
Backend Basics
Backend Directory Structure
Backend Config File
Starting the Backend
Frontend Basics
Frontend Directory Structure
Frontend Config File
Starting the Frontend
Development Patterns
Actions Pattern
Hand-Written Pattern
First API Endpoint
Layered Development
API Layer
Service Layer
DTO Definitions
Model Definitions
Router Registration
Multi-Environment Config
Database Table Conventions
Data Permissions
Response Format
Code Generation
Pre-Generation Setup
Generating Business Code
One-Click Menu Generation
Binding APIs to the Menu
Configuring Role Permissions
Verifying the Feature
Generating Code with an LLM
Advanced Capabilities
Runtime Core API
Authentication & Authorization
Logging
Request Tracing
Cache
Queue
File Upload
Rate Limiting
Scheduled Jobs
Air Hot Reload
Swagger Docs
Code Generation Tool
Last updated:
Open-source MIT Licensed | Copyright © 2020-present
Powered by go-admin-team

TABLE OF CONTENTS

‌
‌
‌
‌

The Code Generation Flow

Going from a database table to a working page takes six steps:

StepWhat to doWhy
1. Pre-Generation SetupSet the gen database name and frontend pathTells the generator which database to read and where to write code
2. Generating Business CodePick a table, configure field attributes, generate frontend and backend codeProduces the CRUD code
3. One-Click Menu GenerationCreate the corresponding menu entryThe generated page needs an entry point to be reachable
4. Binding APIs to the MenuRegister the new APIs in API managementThe permission system authorizes per API
5. Configuring Role PermissionsAssign the menu and API permissions to a roleOtherwise the menu won't show after login and the APIs won't work
6. Verifying the FeatureConfirm CRUD and permissions both workConfirms the whole chain is wired up

INFO

The last four steps exist because of the permission system. Both menus and APIs in go-admin are under RBAC, and generated code doesn't authorize itself — this is why people often finish generating and then find "the page won't open" or "the API says no permission".

Before generating, confirm the table matches the Database Table Conventions, especially the created_at, updated_at and deleted_at shared columns.

Editing the Config

Open config/settings.yml; the gen section controls the generator's behaviour.

bash
database:
# database type: mysql, sqlite3, postgres
driver: mysql
# connection string; the mysql default shown here includes charset=utf8&parseTime=True&loc=Local&timeout=1000ms
source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
gen:
# the database name the generator reads
dbname: dbname
# where generated frontend code goes, must point to the src folder, as a relative path
frontpath: ../go-admin-ui/src

Set the database connection info, then the generator config:

  1. gen > dbname — reads every table under this database name for generation;
  2. gen > frontpath — where generated frontend code lands, must point to the src folder as a relative path; this requires go-admin and go-admin-ui to be sibling directories.

We'll start by creating the table with a SQL script. See the table structure conventions first.

sql
CREATE TABLE `article` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`title` varchar(128) DEFAULT NULL COMMENT 'title',
`author` varchar(128) DEFAULT NULL COMMENT 'author',
`content` varchar(255) DEFAULT NULL COMMENT 'content',
`status` int(1) DEFAULT NULL COMMENT 'status',
`publish_at` timestamp NULL DEFAULT NULL COMMENT 'publish time',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`create_by` int(11) unsigned DEFAULT NULL,
`update_by` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_article_deleted_at` (`deleted_at`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='article';

Once the table exists, start the project:

bash
go run main.go server -c config/settings.yml