jzero

repository·main·Indexed 41 days ago

https://github.com/jzero-io/jzero

A framework built on top of go-zero that automates the generation of API, gateway, and RPC projects. It uses a template-driven approach to generate server and client code from API, proto, or SQL definitions, reducing boilerplate while maintaining full compatibility with the go-zero ecosystem.

Tokens
41.8K
Snippets
161
Records
203
Agent score
48%

What's inside jzero

  1. Overview of jzero-admin

    main

    jzero-admin is a backend management system designed as a best-practice template for jzero. It provides a pre-built foundation for common administrative tasks and supports extensibility through plugins.

    Key Features:

    • Core Management: Includes implementations for user management, menu management, and role management.
    • Database Support: Compatible with mysql, postgres, and sqlite.
    • Extensibility: Supports backend plugins.
    • Internationalization: Built-in i18n support.
  2. Overview of jzero features and design

    main

    jzero is a framework built on top of go-zero and go-zero/goctl. It automates the initialization of API, gateway, and RPC projects by generating both server and client code from descriptive files (api, proto, or sql).

    Key Capabilities

    • Automated Code Generation: Generates framework code based on descriptive files. It supports generating code for git-changed files, specific files, or ignoring specific files to optimize large project workflows.
    • AI-Friendly: Designed to work with AI to generate business logic following best practices.
    • Template Driven: Uses a template-based system for all code generation, allowing for custom enterprise-specific templates.
    • Flexible Configuration: Supports configuration via files, command-line arguments, and environment variables.
    • Ecosystem Compatibility: Maintains full compatibility with the go-zero ecosystem without modifying its core components.
  3. What is jzero?

    main

    jzero is a framework built on top of go-zero and go-zero/goctl. It provides a one-click initialization for api, gateway, and rpc projects.

    Key capabilities:

    • Automated Code Generation: Automatically generates both server-side and client-side framework code based on descriptive files (api, proto, or sql).
    • AI-Ready: Designed to be AI-friendly, allowing AI agents to generate business logic that follows best practices using built-in jzero-skills.
    • Flexible Configuration: Supports configuration via files, command-line arguments, and environment variables.
    • Efficient Updates: Supports generating code based on Git changes, specific files, or ignoring specific files to improve efficiency in large projects.
    • Customizable Templates: Uses a template-driven approach where all code generation is based on templates. You can use default templates (which follow best practices) or create your own custom enterprise templates.
  4. Use jzero features in GoLand

    main

    The jzero-intellij plugin provides several productivity features for working with jzero projects in GoLand:

    • File Support: Adds support for api/proto and sql files.
    • Syntax & Navigation: Provides API file highlighting and navigation.
    • Cross-File Navigation:
      • Navigate from api/proto files directly to their corresponding logic files.
      • Navigate from logic files back to their api/proto definitions.
    • Code Generation:
      • Adds an execution button at the start of lines in api/proto or sql files to trigger code generation.
      • Adds an execution button within the .jzero.yaml file to trigger code generation.
  5. Understand jzero template types

    main

    jzero uses templates to provide predefined code structures and engineering standards. There are four main types of templates:

    • Built-in template (frame): Provides core framework capabilities and supports optional features like model, cache, or redis.
    • Path template (home): A template specified by a path, typically used for project-specific needs within a directory.
    • Local template (local): A global template stored in your local machine at ~/.jzero/templates/local.
    • Remote repository template (remote+branch): Used for enterprise-specific templates hosted in remote repositories.
  6. How template rendering and variables work

    main

    The jzero new command renders both the content of .tpl files and the file/directory names themselves using Go text/template syntax.

    • File Content: .tpl files are processed. If a file ends in .tpl.tpl, only one .tpl suffix is removed and the content is not re-rendered (useful for preserving template source text).
    • Paths: Variables like {{ .APP }} or {{ .Module }} can be used in directory and file names to create dynamic structures.

    Example path rendering: internal/{{ .APP | lower }}/{{ FormatStyle .Style "service_context.go.tpl" }}

  7. What are Skills and how do they work?

    main

    Skills are modular knowledge packages designed to empower AI Agents (like Claude) with domain-specific expertise. They bridge the knowledge gap by teaching AI Agents about specific frameworks, libraries, and architectural patterns.

    In the context of jzero, the jzero-skills package provides the AI Agent with comprehensive knowledge of the jzero framework, including REST API patterns, RPC services, and database operations. This allows AI Agents to act as effective collaborators that understand your specific technology stack without requiring repetitive explanations.

  8. Organize Zrpc clients using third_party and separate config

    main

    For better project structure, it is recommended to place generated zrpcclient code under a third_party directory. To avoid configuration conflicts between the server and the client, use a separate .jzero.yaml file within the client directory.

    Example structure for third_party/matchingclient/:

    1. Create third_party/matchingclient/.jzero.yaml with:
    gen:
      zrpcclient:
        output: .
    1. Run jzero gen zrpcclient while inside the third_party/matchingclient directory.

    This results in a clean separation of the client's proto definitions, models, and typed code from your main service logic.

    # third_party/matchingclient/.jzero.yaml
    gen:
      zrpcclient:
        output: .
  9. Group generated types using go_package

    main

    To avoid a massive types.go file and to prevent naming conflicts between different API modules, use the go_package option within the info block. This groups the generated message structs into the specified Go package folder.

    info (
    	go_package: "version"
    )
  10. Configure HTTP and zRPC middleware in proto files

    main

    You can configure middleware for entire services or individual methods using specific proto options. Multiple middleware names should be separated by commas.

    Middleware Scopes

    • Service-level (Group): Use (jzero.api.http_group) to add HTTP middleware to all methods in a service, or (jzero.api.zrpc_group) for zRPC middleware.
    • Method-level: Use (jzero.api.http) or (jzero.api.zrpc) to apply middleware to a specific RPC method.

    After running jzero gen, the middleware wiring is generated in files such as internal/middleware/middleware_gen.go.

    import "jzero/api/http.proto";
    import "jzero/api/zrpc.proto";
    
    service User {
        // Adds HTTP middleware to all methods in this service
        option (jzero.api.http_group) = {
            middleware: "auth",
        };
    
        rpc CreateUser(CreateUserRequest) returns(CreateUserResponse) {
            option (google.api.http) = {
                post: "/api/v1/user/create",
                body: "*"
            };
            // Adds zRPC middleware to this specific method
            option (jzero.api.zrpc) = {
                middleware: "withValue1",
            };
        };
    
        rpc ListUser(ListUserRequest) returns(ListUserResponse) {
            option (google.api.http) = {
                get: "/api/v1/user/{username}/list",
            };
        };
    }
  11. Generated file structure for API and SQL descriptors

    main

    When running jzero gen on .api and .sql files, the following directory structure is automatically populated to maintain project standards:

    • internal/model/model.go: Contains all model registrations.
    • internal/model/{name}/*.go: The Model layer containing CRUD methods and field constants.
    • internal/handler/{name}/{name}_compact.go: The HTTP/RPC handler implementation.
    • internal/handler/routes.go: Centralized route registration.
    • internal/logic/{name}/{method}.go: The business logic implementation files (initially containing empty implementations).
    • internal/types/{name}/types.go: Type struct definitions.
  12. How the jzero CLI command structure works

    main

    The jzero CLI template uses a three-layer command structure based on the Cobra framework to organize functionality:

    1. Root Command: The entry point (e.g., mycli). It defines global configuration, global flags (like --debug or --config), and registers top-level commands.
    2. Command: Major functional modules mounted under the root (e.g., mycli config, mycli plugin, mycli server). These can have their own independent flags and arguments.
    3. Sub Command: Granular functionality nested under commands (e.g., mycli config init, mycli plugin install). This allows for modular, multi-level nesting.

    Flag Types:

    • Local Flags: Valid only for the specific command they are defined in.
    • Persistent Flags: Valid for the command they are defined in AND all its subcommands.
    • Global Flags: Valid for all commands in the tool.
    # Example hierarchy
    mycli config init      # Subcommand of 'config'
    mycli plugin install   # Subcommand of 'plugin'
    mycli server start     # Subcommand of 'server'