GoWindGoWind
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
  • Introduction

    • GoWind CMS Product Introduction
    • GoWind CMS Installation Guide
  • Backend

    • CMS Backend Architecture Overview
    • CMS Backend Modules Overview
    • CMS Protobuf API Definitions
    • CMS Configuration and Deployment Guide
    • Backend Extension Mechanisms Guide
  • Frontend

    • CMS Frontend Architecture
    • CMS Frontend Modules Overview
  • Getting Started

    • Adding a Content Type, Full Stack in Practice
    • Multi-Client API Client Code Generation Tutorial
  • Core Tutorials

    • Content Multilingual Translation Tutorial
    • Content Publishing Workflow Tutorial
    • Section Editor in Practice
    • Headless API Multi-Client Integration in Practice
    • Frontend Application Development in Practice
  • Intermediate Tutorials

    • Multi-Site Management in Practice
    • Media Asset Management in Practice
    • Comment System Tutorial
    • Event Bus Architecture in Practice
    • Lua Script Extensions in Practice
  • Advanced Topics

    • Permission System in Practice
    • Dual-Client Login Security in Practice
    • Encryption and Security Toolkit Tutorial
    • Full-Text Search in Practice
    • Task Scheduling in Practice
    • Real-Time Message Push (SSE) in Practice
    • Performance Monitoring in Practice
    • Dictionary Management System Tutorial
  • Full-Stack Guides

    • Full-Stack Integration in Practice
    • Three-Service Deployment Tutorial

Backend Extension Mechanisms Guide

GoWind CMS provides multiple backend extension approaches, allowing custom business logic without modifying core code. This document introduces all extension mechanisms and their applicable scenarios.

1. Extension Approaches Overview

2. Extension Approaches Compared

ApproachDevelopment CostRuntime FlexibilityPerformanceApplicable Scenarios
New business moduleHighNo (requires compilation)BestBrand-new feature modules
Lua scriptLowYesMediumLightweight logic extensions
Event subscriptionLowYesHighSide-effect handling
Custom APIMediumYesMediumAggregate endpoints/adapters
Scheduled tasksLowYesHighPeriodic tasks
MiddlewareMediumNoHighGlobal request processing

3. Adding a Business Module

For the complete module addition workflow, see Full-Stack Practice: Adding a Content Type. Core steps:

4. Lua Script Extension

For details, see Lua Script Extension in Practice.

4.1 Quick Example

-- Register a custom API
api.register("GET", "/app/v1/posts/popular", function(ctx)
    local posts = db.query("SELECT * FROM posts WHERE status = 1 ORDER BY view_count DESC LIMIT 10")
    return json.encode({ items = posts })
end)

-- Event subscription
eventbus.subscribe("post.published", function(event)
    log.info("Post published: " .. event.PostId)
end)

5. Event Bus Extension

For details, see Event Bus Architecture.

5.1 Go Subscription

// Register a subscriber
bus.Subscribe("post.published", func(ctx context.Context, event interface{}) {
    e := event.(*eventbus.PostPublishedEvent)
    // Custom handling logic
})

5.2 Lua Subscription

eventbus.subscribe("post.published", function(event)
    -- Lua handling logic
end)

6. Custom Middleware

6.1 Middleware Example

// pkg/middleware/tenant_context.go
func TenantContext() middleware.Middleware {
    return func(handler middleware.Handler) middleware.Handler {
        return func(ctx context.Context, req interface{}) (interface{}, error) {
            // Extract the tenant ID from the JWT
            tenantId := ctx.Value(middleware.TenantIDKey{}).(uint32)

            // Inject it into the context
            ctx = context.WithValue(ctx, entgo.TenantKey{}, tenantId)

            return handler(ctx, req)
        }
    }
}

6.2 Registering Middleware

// app/admin/service/internal/server/rest_server.go
srv.Use(
    middleware.Logging(),
    middleware.Audit(),
    middleware.Auth(),           // JWT authentication
    middleware.TenantContext(),  // Tenant context
    middleware.Authz(authorizer),// Permission verification
)

7. Scheduled Task Extension

7.1 Asynq Scheduled Tasks

// app/core/service/internal/server/asynq_server.go
func RegisterScheduledTasks(scheduler *asynq.Scheduler) {
    scheduler.Register("0 3 * * *", asynq.NewTask(
        task.TypeCleanupExpiredData,
        jsonMustMarshal(task.CleanupExpiredDataPayload{
            EntityType: "posts",
            OlderThan:  time.Now().AddDate(0, 0, -30).Unix(),
        }),
    ))
}

7.2 Lua Scheduled Tasks

cron.register("0 2 * * *", function()
    -- Daily data statistics
    local stats = db.query("SELECT ...")
    redis.set("daily:stats", json.encode(stats))
end)

8. Extension Best Practices

8.1 Choosing an Extension Approach

8.2 Extension Security

RuleDescription
Lua sandboxRestricts file system/network access
Event isolationSubscriber panics do not affect the main flow
Permission checksExtension APIs require authentication

9. Related Documentation

  • Full-Stack Practice: Adding a Content Type
  • Lua Script Extension in Practice
  • Event Bus Architecture
  • GoWind Admin Backend Extension Mechanisms
Edit this page
Last Updated:: 11/3/26, 2:14 PM
Contributors: Bobo
Prev
CMS Configuration and Deployment Guide