项目初始化

This commit is contained in:
2026-01-13 17:00:49 +08:00
commit d2352318f4
27 changed files with 4343 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
package admin
import (
"FileRelay/internal/auth"
"FileRelay/internal/bootstrap"
"FileRelay/internal/model"
"net/http"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
)
type AuthHandler struct{}
func NewAuthHandler() *AuthHandler {
return &AuthHandler{}
}
type LoginRequest struct {
Password string `json:"password" binding:"required" example:"admin"`
}
type LoginResponse struct {
Token string `json:"token"`
}
// Login 管理员登录
// @Summary 管理员登录
// @Description 通过密码换取 JWT Token
// @Tags Admin
// @Accept json
// @Produce json
// @Param request body LoginRequest true "登录请求"
// @Success 200 {object} model.Response{data=LoginResponse}
// @Failure 401 {object} model.Response
// @Router /admin/login [post]
func (h *AuthHandler) Login(c *gin.Context) {
var req LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "Invalid request"))
return
}
var admin model.Admin
if err := bootstrap.DB.First(&admin).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Admin not found"))
return
}
if err := bcrypt.CompareHashAndPassword([]byte(admin.PasswordHash), []byte(req.Password)); err != nil {
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Incorrect password"))
return
}
token, err := auth.GenerateToken(admin.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to generate token"))
return
}
// 更新登录时间
now := time.Now()
bootstrap.DB.Model(&admin).Update("last_login", &now)
c.JSON(http.StatusOK, model.SuccessResponse(LoginResponse{
Token: token,
}))
}

173
internal/api/admin/batch.go Normal file
View File

@@ -0,0 +1,173 @@
package admin
import (
"FileRelay/internal/bootstrap"
"FileRelay/internal/model"
"FileRelay/internal/service"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
type BatchHandler struct {
batchService *service.BatchService
}
func NewBatchHandler() *BatchHandler {
return &BatchHandler{
batchService: service.NewBatchService(),
}
}
type ListBatchesResponse struct {
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
Data []model.FileBatch `json:"data"`
}
type UpdateBatchRequest struct {
Remark string `json:"remark"`
ExpireType string `json:"expire_type"`
ExpireAt *time.Time `json:"expire_at"`
MaxDownloads int `json:"max_downloads"`
Status string `json:"status"`
}
// ListBatches 获取批次列表
// @Summary 获取批次列表
// @Description 分页查询所有文件批次,支持按状态过滤和取件码模糊搜索
// @Tags Admin
// @Security AdminAuth
// @Param page query int false "页码 (默认 1)"
// @Param page_size query int false "每页数量 (默认 20)"
// @Param status query string false "状态 (active/expired/deleted)"
// @Param pickup_code query string false "取件码 (模糊搜索)"
// @Produce json
// @Success 200 {object} model.Response{data=ListBatchesResponse}
// @Failure 401 {object} model.Response
// @Router /admin/batches [get]
func (h *BatchHandler) ListBatches(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
status := c.Query("status")
pickupCode := c.Query("pickup_code")
query := bootstrap.DB.Model(&model.FileBatch{})
if status != "" {
query = query.Where("status = ?", status)
}
if pickupCode != "" {
query = query.Where("pickup_code LIKE ?", "%"+pickupCode+"%")
}
var total int64
query.Count(&total)
var batches []model.FileBatch
err := query.Offset((page - 1) * pageSize).Limit(pageSize).Order("created_at DESC").Find(&batches).Error
if err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(ListBatchesResponse{
Total: total,
Page: page,
PageSize: pageSize,
Data: batches,
}))
}
// GetBatch 获取批次详情
// @Summary 获取批次详情
// @Description 根据批次 ID 获取批次信息及关联的文件列表
// @Tags Admin
// @Security AdminAuth
// @Param batch_id path int true "批次 ID"
// @Produce json
// @Success 200 {object} model.Response{data=model.FileBatch}
// @Failure 404 {object} model.Response
// @Router /admin/batch/{batch_id} [get]
func (h *BatchHandler) GetBatch(c *gin.Context) {
id := c.Param("batch_id")
var batch model.FileBatch
if err := bootstrap.DB.Preload("FileItems").First(&batch, id).Error; err != nil {
c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found"))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(batch))
}
// UpdateBatch 修改批次信息
// @Summary 修改批次信息
// @Description 允许修改备注、过期策略、最大下载次数、状态等
// @Tags Admin
// @Security AdminAuth
// @Accept json
// @Produce json
// @Param batch_id path int true "批次 ID"
// @Param request body UpdateBatchRequest true "修改内容"
// @Success 200 {object} model.Response{data=model.FileBatch}
// @Failure 400 {object} model.Response
// @Router /admin/batch/{batch_id} [put]
func (h *BatchHandler) UpdateBatch(c *gin.Context) {
id := c.Param("batch_id")
var batch model.FileBatch
if err := bootstrap.DB.First(&batch, id).Error; err != nil {
c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found"))
return
}
var input UpdateBatchRequest
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, err.Error()))
return
}
updates := make(map[string]interface{})
updates["remark"] = input.Remark
updates["expire_type"] = input.ExpireType
updates["expire_at"] = input.ExpireAt
updates["max_downloads"] = input.MaxDownloads
updates["status"] = input.Status
if err := bootstrap.DB.Model(&batch).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(batch))
}
// DeleteBatch 删除批次
// @Summary 删除批次
// @Description 标记批次为已删除,并物理删除关联的存储文件
// @Tags Admin
// @Security AdminAuth
// @Param batch_id path int true "批次 ID"
// @Produce json
// @Success 200 {object} model.Response
// @Failure 500 {object} model.Response
// @Router /admin/batch/{batch_id} [delete]
func (h *BatchHandler) DeleteBatch(c *gin.Context) {
idStr := c.Param("batch_id")
id, _ := strconv.ParseUint(idStr, 10, 32)
if err := h.batchService.DeleteBatch(c.Request.Context(), uint(id)); err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(map[string]interface{}{}))
}

109
internal/api/admin/token.go Normal file
View File

@@ -0,0 +1,109 @@
package admin
import (
"FileRelay/internal/bootstrap"
"FileRelay/internal/model"
"FileRelay/internal/service"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type TokenHandler struct {
tokenService *service.TokenService
}
func NewTokenHandler() *TokenHandler {
return &TokenHandler{
tokenService: service.NewTokenService(),
}
}
type CreateTokenRequest struct {
Name string `json:"name" binding:"required" example:"Test Token"`
Scope string `json:"scope" example:"upload,pickup"`
ExpireAt *time.Time `json:"expire_at"`
}
type CreateTokenResponse struct {
Token string `json:"token"`
Data *model.APIToken `json:"data"`
}
// ListTokens 获取 API Token 列表
// @Summary 获取 API Token 列表
// @Description 获取系统中所有 API Token 的详详信息(不包含哈希)
// @Tags Admin
// @Security AdminAuth
// @Produce json
// @Success 200 {object} model.Response{data=[]model.APIToken}
// @Failure 401 {object} model.Response
// @Router /admin/api-tokens [get]
func (h *TokenHandler) ListTokens(c *gin.Context) {
var tokens []model.APIToken
if err := bootstrap.DB.Find(&tokens).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(tokens))
}
// CreateToken 创建 API Token
// @Summary 创建 API Token
// @Description 创建一个新的 API Token返回原始 Token仅显示一次
// @Tags Admin
// @Security AdminAuth
// @Accept json
// @Produce json
// @Param request body CreateTokenRequest true "Token 信息"
// @Success 201 {object} model.Response{data=CreateTokenResponse}
// @Failure 400 {object} model.Response
// @Router /admin/api-tokens [post]
func (h *TokenHandler) CreateToken(c *gin.Context) {
var input CreateTokenRequest
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, err.Error()))
return
}
rawToken, token, err := h.tokenService.CreateToken(input.Name, input.Scope, input.ExpireAt)
if err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusCreated, model.SuccessResponse(CreateTokenResponse{
Token: rawToken,
Data: token,
}))
}
func (h *TokenHandler) RevokeToken(c *gin.Context) {
id := c.Param("id")
if err := bootstrap.DB.Model(&model.APIToken{}).Where("id = ?", id).Update("revoked", true).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(map[string]interface{}{}))
}
// DeleteToken 删除 API Token
// @Summary 删除 API Token
// @Description 根据 ID 永久删除 API Token
// @Tags Admin
// @Security AdminAuth
// @Param id path int true "Token ID"
// @Produce json
// @Success 200 {object} model.Response
// @Failure 500 {object} model.Response
// @Router /admin/api-tokens/{id} [delete]
func (h *TokenHandler) DeleteToken(c *gin.Context) {
id := c.Param("id")
if err := bootstrap.DB.Delete(&model.APIToken{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
return
}
c.JSON(http.StatusOK, model.SuccessResponse(map[string]interface{}{}))
}