Zero-Admin Documentation

repository·master·Indexed 21 days ago

https://github.com/feihua/zero-admin

A full-stack e-commerce system built using the go-zero framework, designed for Docker-based deployment. It includes a backend management system, a React-based PC management dashboard (zero-admin-ui), a React-based web storefront (zero-pc-web), and a Flutter-based mobile application (flutter_mall). The system utilizes goctl for API and RPC code generation and includes a custom generate-code tool for scaffolding modules based on database schemas.

Tokens
3.6K
Snippets
13
Records
15
Agent score
76%

What's inside Zero-Admin

  1. Overview of Zero-Admin components

    master

    Zero-Admin is an e-commerce system built on the go-zero framework. It is designed for containerized deployment via Docker and consists of several distinct interfaces:

    • Front-end Mall System: The consumer-facing shop including product search, shopping cart, order processing, and member center.
    • Back-end Management System: The administrative interface for managing products, orders, members, promotions, and system permissions.
    • zero-admin-ui: A React-based PC management dashboard.
    • flutter_mall: A Flutter-based mobile application.
    • zero-pc-web: A React-based web version of the e-commerce storefront.
  2. Generate API code

    master

    Use goctl api go to generate backend code from .api definition files. Note: These commands must be executed from the project root directory.

    Depending on the target module, use the corresponding command:

    • Backend Management: Generates code for the admin interface.
    • Mobile App: Generates code for the front-end mobile application using the go_zero style.
    • MQ Consumer: Generates code for message queue consumers.
    • Job (Scheduled Tasks): Generates code for scheduled task jobs.
    # Backend Management
    goctl api go -api ./api/admin/doc/api/admin.api -dir ./api/admin/
    
    # Mobile App
    goctl api go -api ./api/front/doc/api/front.api -dir ./api/front/ --style go_zero
    
    # MQ Consumer
    goctl api go -api ./consumer/consumer.api -dir ./consumer --style go_zero
    
    # Job (Scheduled Tasks)
    goctl api go -api ./job/job.api -dir ./job --style go_zero
  3. Use generate-code to scaffold Zero-Admin modules

    master

    You can use the generate-code CLI to generate Go code for Zero-Admin based on an existing database schema.

    Command Syntax: generate-code golang zero --dsn "<DSN>" --tableNames <TABLE_PATTERN> --prefix <PREFIX> --rpcClient <CLIENT_NAME> --author <AUTHOR_NAME>

    Arguments:

    • --dsn: Data Source Name for the MySQL connection (e.g., root:password@tcp(127.0.0.1:3306)/dbname).
    • --tableNames: A pattern or prefix to identify which tables to process (e.g., sys_).
    • --prefix: The prefix to apply to generated files/entities.
    • --rpcClient: The name of the RPC client to generate.
    • --author: The name of the author for code comments.
    generate-code golang zero --dsn "root:123456@tcp(127.0.0.1:3306)/demo" --tableNames sys_ --prefix sys_  --rpcClient sysclient --author liufeihua
  4. Generate RPC code

    master

    Use goctl rpc protoc to generate gRPC and zRPC code from .proto files. These commands generate the necessary service implementations and client stubs for the various microservices in the system.

    Available services include:

    • sys-rpc (System)
    • ums-rpc (User Management)
    • pms-rpc (Product Management)
    • oms-rpc (Order Management)
    • sms-rpc (SMS/Messaging)
    • cms-rpc (Content Management)
    • search (Search Service)
    # sys-rpc
    goctl rpc protoc rpc/sys/sys.proto --go_out=./rpc/sys/ --go-grpc_out=./rpc/sys/ --zrpc_out=./rpc/sys/ -m
    
    # ums-rpc
    goctl rpc protoc rpc/ums/ums.proto --go_out=./rpc/ums/ --go-grpc_out=./rpc/ums/ --zrpc_out=./rpc/ums/ -m
    
    # pms-rpc
    goctl rpc protoc rpc/pms/pms.proto --go_out=./rpc/pms/ --go-grpc_out=./rpc/pms/ --zrpc_out=./rpc/pms/ -m
    
    # oms-rpc
    goctl rpc protoc rpc/oms/oms.proto --go_out=./rpc/oms/ --go-grpc_out=./rpc/oms/ --zrpc_out=./rpc/oms/ -m --style go_zero
    
    # sms-rpc
    goctl rpc protoc rpc/sms/sms.proto --go_out=./rpc/sms/ --go-grpc_out=./rpc/sms/ --zrpc_out=./rpc/sms/ -m --style go_zero
    
    # cms-rpc
    goctl rpc protoc rpc/cms/cms.proto --go_out=./rpc/cms/ --go-grpc_out=./rpc/cms/ --zrpc_out=./rpc/cms/ -m
    
    # search
    goctl rpc protoc rpc/search/search.proto --go_out=./rpc/search/ --go-grpc_out=./rpc/search/ --zrpc_out=./rpc/search/ --style go_zero
  5. Access the Zero-Admin demo environments

    master

    You can preview the system using the following credentials:

    EnvironmentURLCredentials
    Mall (Standard)http://129.204.203.29/malladmin / 123456
    Vue Versionhttp://129.204.203.29/vue/loginadmin / 123456
    Web (PC) Versionhttp://129.204.203.29/pc/admin / 123456

    Note: Some modification and deletion permissions are restricted for demo accounts.

  6. Merge multiple proto files into a single sys.proto file

    master

    Because goctl (go-zero's CLI tool) generates gRPC services from a single .proto file, managing multiple proto files can become disorganized. This utility automates the merging of multiple .proto files located in a specific directory into one unified sys.proto file.

    It works by:

    1. Reading all files in a target directory (excluding the tool itself).
    2. Stripping the first 69-70 characters from each file (to remove individual package/option headers).
    3. Concatenating the contents.
    4. Replacing the original package main with package sysclient and updating the go_package option to ./sysclient in the final output.

    Note: You must manually update the folderPath and outputFilePath variables in the source code before running it.

    // Configuration within main.go
    folderPath := "rpc/sys/proto"         // Replace with your directory path
    outputFilePath := "rpc/sys/sys.proto" // Path for the new merged file
  7. Merge multiple proto files into a single PMS proto file

    master

    Because goctl (the go-zero CLI tool) generates gRPC services from a single .proto file, managing multiple service definitions can become disorganized. This utility automates the process of merging multiple .proto files located in a specific directory into one consolidated pms.proto file.

    It works by:

    1. Reading all files in a target directory (excluding the main.go file itself).
    2. Stripping the initial header of each file (the first 70 bytes) to avoid duplicate syntax/package declarations.
    3. Concatenating the remaining content.
    4. Replacing the package main and option go_package = "./proto" declarations with package pmsclient and option go_package = "./pmsclient" respectively to ensure the merged file is valid for the pmsclient context.

    To use this, update the folderPath and outputFilePath variables in the source code before running.

    func main() {
    	folderPath := "rpc/pms/proto"         // Replace with your directory path
    	outputFilePath := "rpc/pms/pms.proto" // Path for the new merged file
    	// ... execution logic ...
    }
  8. Merge multiple proto files into a single CMS proto file

    master

    When using goctl to generate gRPC services in go-zero, the tool typically expects a single .proto file. If your service is split across multiple proto files, this utility merges them into one consolidated file.

    It works by:

    1. Reading all .proto files in a specified directory.
    2. Stripping the initial header lines (the first 69-70 bytes) from subsequent files to avoid duplicate syntax/package declarations.
    3. Concatenating the contents.
    4. Replacing the package main and option go_package = "./proto" declarations with package cmsclient and option go_package = "./cmsclient" respectively to ensure the merged file is valid for a client package.

    Note: You must manually update the folderPath and outputFilePath variables in the source code before running it.

    // Edit these variables in rpc/cms/proto/main.go before running
    folderPath := "rpc/cms/proto"         // Your source proto directory
    outputFilePath := "rpc/cms/cms.proto" // The target merged file path
  9. Merge multiple proto files into a single SMS service proto

    master

    Because goctl (from the go-zero framework) generates gRPC services from a single .proto file, managing multiple proto files can become disorganized. This utility automates the process of merging multiple .proto files within a directory into one consolidated .proto file, specifically updating the package name and go_package option to ensure the resulting file is compatible with the smsclient package.

    To use this tool, you must modify the folderPath and outputFilePath variables in the source code to match your project structure.

    func main() {
    	folderPath := "rpc/sms/proto"         // Replace with your directory path
    	outputFilePath := "rpc/sms/sms.proto" // Path for the new merged file
    	// ... execution logic
    }