diff --git a/src/modules/common/common.go b/src/modules/common/common.go index 7a054bc2..e85c96ad 100644 --- a/src/modules/common/common.go +++ b/src/modules/common/common.go @@ -22,6 +22,16 @@ func Setup(router *gin.Engine) { controller.NewIndex.Handler, ) + // 路由服务器时间 + router.GET("/time", + middleware.RateLimit(middleware.LimitOption{ + Time: 60, + Count: 10, + Type: middleware.LIMIT_IP, + }), + controller.NewTimestamp.Handler, + ) + // 通用请求 commonGroup := router.Group("/common") { diff --git a/src/modules/common/controller/timestamp.go b/src/modules/common/controller/timestamp.go new file mode 100644 index 00000000..77db2507 --- /dev/null +++ b/src/modules/common/controller/timestamp.go @@ -0,0 +1,46 @@ +package controller + +import ( + "time" + + "be.ems/src/framework/resp" + + "github.com/gin-gonic/gin" +) + +// 实例化控制层 TimestampController 结构体 +var NewTimestamp = &TimestampController{} + +// 服务器时间 +// +// PATH /time +type TimestampController struct{} + +// Handler 路由 +// +// GET / +// +// @Tags common +// @Accept json +// @Produce json +// @Success 200 {object} object "Response Results" +// @Summary Server Time +// @Description Server Time +// @Router / [get] +func (s TimestampController) Handler(c *gin.Context) { + now := time.Now() + // 获取当前时间戳 + timestamp := now.UnixMilli() + // 获取时区 + timezone := now.Format("-0700") + // 获取时区名称 + timezoneName := now.Format("MST") + // 获取 RFC3339 格式的时间 + rfc3339 := now.Format(time.RFC3339) + c.JSON(200, resp.OkData(map[string]any{ + "timestamp": timestamp, + "timezone": timezone, + "timezoneName": timezoneName, + "rfc3339": rfc3339, + })) +}