首次提交
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/luxsin/app-api/internal/repository"
|
||||
"github.com/luxsin/app-api/internal/response"
|
||||
"github.com/luxsin/app-api/pkg/encode"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type BrandHandler struct {
|
||||
repo *repository.BrandRepository
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewBrandHandler(db *sql.DB, log *zap.Logger) *BrandHandler {
|
||||
return &BrandHandler{
|
||||
repo: repository.NewBrandRepository(db),
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *BrandHandler) GetBrand(c *gin.Context) {
|
||||
brandName := c.Query("brandName")
|
||||
base64Resp := encode.ParseBase64Param(c)
|
||||
|
||||
list, err := h.repo.List(c.Request.Context(), brandName)
|
||||
if err != nil {
|
||||
h.log.Error("get brand list failed", zap.Error(err))
|
||||
response.InternalError(c, "failed to get brand list")
|
||||
return
|
||||
}
|
||||
|
||||
if base64Resp {
|
||||
encoded, err := encode.EncodeJSON(list)
|
||||
if err != nil {
|
||||
h.log.Error("encode response failed", zap.Error(err))
|
||||
response.InternalError(c, "failed to encode response")
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, encoded)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, list)
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type DeviceHandler struct {
|
||||
redis *redis.Client
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewDeviceHandler(redis *redis.Client, log *zap.Logger) *DeviceHandler {
|
||||
return &DeviceHandler{
|
||||
redis: redis,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) ReportDevInfo(c *gin.Context) {
|
||||
mac := strings.TrimSpace(c.Query("mac"))
|
||||
model := strings.TrimSpace(c.Query("model"))
|
||||
ver := strings.TrimSpace(c.Query("ver"))
|
||||
xForwardedFor := c.GetHeader("X-Forwarded-For")
|
||||
|
||||
if mac == "" || model == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 400,
|
||||
"msg": "参数校验失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if ver == "" {
|
||||
ver = ""
|
||||
}
|
||||
if xForwardedFor == "" {
|
||||
xForwardedFor = ""
|
||||
}
|
||||
|
||||
h.log.Info("report dev info",
|
||||
zap.String("remote_ip", xForwardedFor),
|
||||
zap.String("time", time.Now().Format("yyyy-MM-dd HH:mm:ss")),
|
||||
)
|
||||
|
||||
deviceInfo := map[string]string{
|
||||
"mac_addr": mac,
|
||||
"model": model,
|
||||
"active_date": time.Now().Format("yyyy-MM-dd"),
|
||||
"ip_addr": xForwardedFor,
|
||||
"ver": ver,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(deviceInfo)
|
||||
if err != nil {
|
||||
h.log.Error("marshal device info failed", zap.Error(err))
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 500,
|
||||
"msg": "系统错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
if err := h.redis.HSet(ctx, "devices", mac, jsonData).Err(); err != nil {
|
||||
h.log.Error("redis hset failed", zap.Error(err))
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 500,
|
||||
"msg": "系统错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "操作成功",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/luxsin/app-api/internal/response"
|
||||
)
|
||||
|
||||
type HealthHandler struct{}
|
||||
|
||||
func NewHealthHandler() *HealthHandler {
|
||||
return &HealthHandler{}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Check(c *gin.Context) {
|
||||
response.OK(c, gin.H{
|
||||
"status": "up",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/luxsin/app-api/internal/repository"
|
||||
"github.com/luxsin/app-api/internal/response"
|
||||
"github.com/luxsin/app-api/pkg/encode"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ModelHandler struct {
|
||||
repo *repository.ModelRepository
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewModelHandler(db *sql.DB, log *zap.Logger) *ModelHandler {
|
||||
return &ModelHandler{
|
||||
repo: repository.NewModelRepository(db),
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ModelHandler) GetModel(c *gin.Context) {
|
||||
brandName := c.Query("brandName")
|
||||
modelName := c.Query("modelName")
|
||||
base64Resp := encode.ParseBase64Param(c)
|
||||
|
||||
list, err := h.repo.List(c.Request.Context(), brandName, modelName)
|
||||
if err != nil {
|
||||
h.log.Error("get model list failed", zap.Error(err))
|
||||
response.InternalError(c, "failed to get model list")
|
||||
return
|
||||
}
|
||||
|
||||
if base64Resp {
|
||||
encoded, err := encode.EncodeJSON(list)
|
||||
if err != nil {
|
||||
h.log.Error("encode response failed", zap.Error(err))
|
||||
response.InternalError(c, "failed to encode response")
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, encoded)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, list)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/luxsin/app-api/internal/response"
|
||||
"github.com/luxsin/app-api/internal/search"
|
||||
"github.com/luxsin/app-api/pkg/encode"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ModelListHandler struct {
|
||||
search *search.Client
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewModelListHandler(searchClient *search.Client, log *zap.Logger) *ModelListHandler {
|
||||
return &ModelListHandler{
|
||||
search: searchClient,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ModelListHandler) ModelList(c *gin.Context) {
|
||||
key := c.Query("key")
|
||||
base64Resp := encode.ParseBase64Param(c)
|
||||
|
||||
count := 100
|
||||
if v := c.Query("count"); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||
count = n
|
||||
}
|
||||
}
|
||||
|
||||
list, err := h.search.ModelList(c.Request.Context(), key, count)
|
||||
if err != nil {
|
||||
h.log.Error("model list search failed", zap.Error(err))
|
||||
response.InternalError(c, "failed to search models")
|
||||
return
|
||||
}
|
||||
|
||||
if base64Resp {
|
||||
encoded, err := encode.EncodeJSON(list)
|
||||
if err != nil {
|
||||
h.log.Error("encode response failed", zap.Error(err))
|
||||
response.InternalError(c, "failed to encode response")
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, encoded)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, list)
|
||||
}
|
||||
Reference in New Issue
Block a user