You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: apilog.go
    path of file: ./repos/xxim-server/app/gateway/internal/middleware
    
    the code of the file until where you have to start completion: package middleware

import (
	"bytes"
	"github.com/cherish-chat/xxim-server/app/gateway/internal/svc"
	"github.com/cherish-chat/xxim-server/common/utils"
	"github.com/gin-gonic/gin"
	"github.com/zeromicro/go-zero/core/logx"
	"io"
	"os"
	"regexp"
	"strings"
	"time"
)

func ApiLog(svcCtx *svc.ServiceContext) gin.HandlerFunc {
	if len(svcCtx.Config.Http.ApiLog.Apis) == 0 {
		return func(c *gin.Context) {
			c.Next()
		}
	} else {
		return func(c *gin.Context) {
			matched := false
			for _, api := range svcCtx.Config.Http.ApiLog.Apis {
				// 格式: GET r'^/api/v1/user/.*' 表示所有以 /ap
    
func ApiLog(svcCtx *svc.ServiceContext) gin.HandlerFunc {
	if len(svcCtx.Config.Http.ApiLog.Apis) == 0 {
		return func(c *gin.Context) {
			c.Next()
		}
	} else {
		return func(c *gin.Context) {
			matched := false
			for _, api := range svcCtx.Config.Http.ApiLog.Apis {
				// 格式: GET r'^/api/v1/user/.*' 表示所有以 /api/v1/user/ 开头的 GET 请求都会被记录
				// 取出 Method 和 PathRegex
				apiSplit := strings.Split(api, " ")
				if len(apiSplit) != 2 {
					logx.Errorf("config.Http.ApiLog.Apis is invalid: %s", api)
					os.Exit(1)
				}
				method := strings.ToUpper(apiSplit[0])
				// 取出 Path
				pathRegex := apiSplit[1]
				// 判断是否匹配
				if method == c.Request.Method && pathRegexMatch(pathRegex, c.Request.URL.Path) {
					// 匹配，记录日志
					matched = true
					break
				}
			}
			if matched {
				// 记录日志
				var (
					bodyBuf  []byte
					reqTime  = time.Now()
					clientIp = utils.Http.GetClientIP(c.Request)
				)
				blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
				c.Writer = blw
				body := c.Request.Body
				defer body.Close()
				bodyBuf, _ = io.ReadAll(body)
				c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBuf))
				defer func() {
					c := c.Copy()
					respTime := time.Now()
					go func() {
						respBody := blw.body.Bytes()
						resp := string(respBody)
						logx.Debugf("clientIp: %s, method: %s, path: %s, requestTime: %s, responseTime: %s, request: %s, response: %s",
							clientIp, c.Request.Method, c.Request.URL.Path, reqTime.Format("2006-01-02 15:04:05.000"), respTime.Format("2006-01-02 15:04:05.000"), string(bodyBuf), resp,
						)
						// TODO: 推送到消息队列?
					}()
				}()
				c.Next()
			}
		}
	}
}