github.com/weplanx/server@v0.2.6-0.20240318110640-f7e75155779a/xapi/emqx/controller.go (about)

     1  package emqx
     2  
     3  import (
     4  	"context"
     5  	"github.com/bytedance/gopkg/util/logger"
     6  	"github.com/cloudwego/hertz/pkg/app"
     7  	"github.com/cloudwego/hertz/pkg/common/utils"
     8  )
     9  
    10  type Controller struct {
    11  	EmqxService *Service
    12  }
    13  
    14  type AuthDto struct {
    15  	Identity string `json:"identity" vd:"required"`
    16  	Token    string `json:"token" vd:"required"`
    17  }
    18  
    19  func (x *Controller) Auth(ctx context.Context, c *app.RequestContext) {
    20  	var dto AuthDto
    21  	if err := c.BindAndValidate(&dto); err != nil {
    22  		c.Error(err)
    23  		return
    24  	}
    25  
    26  	if err := x.EmqxService.Auth(ctx, dto); err != nil {
    27  		c.Error(err)
    28  		return
    29  	}
    30  
    31  	c.Status(204)
    32  }
    33  
    34  type AclDto struct {
    35  	Identity string `json:"identity" vd:"mongodb"`
    36  	Topic    string `json:"topic" vd:"required"`
    37  }
    38  
    39  func (x *Controller) Acl(ctx context.Context, c *app.RequestContext) {
    40  	var dto AclDto
    41  	if err := c.BindAndValidate(&dto); err != nil {
    42  		c.Error(err)
    43  		return
    44  	}
    45  
    46  	if err := x.EmqxService.Acl(ctx, dto); err != nil {
    47  		logger.CtxErrorf(ctx, err.Error())
    48  		c.JSON(200, utils.H{"result": "deny"})
    49  		return
    50  	}
    51  
    52  	c.Status(204)
    53  }
    54  
    55  type BridgeDto struct {
    56  	Client  string `json:"client" vd:"required"`
    57  	Topic   string `json:"topic" vd:"required"`
    58  	Payload M      `json:"payload" vd:"required,gt=0"`
    59  }
    60  
    61  func (x *Controller) Bridge(ctx context.Context, c *app.RequestContext) {
    62  	var dto BridgeDto
    63  	if err := c.BindAndValidate(&dto); err != nil {
    64  		c.Error(err)
    65  		return
    66  	}
    67  
    68  	if err := x.EmqxService.Bridge(ctx, dto); err != nil {
    69  		c.Error(err)
    70  		return
    71  	}
    72  
    73  	c.Status(204)
    74  }