feat: 实现后端和管理后台基础架构
## 后端架构 (Go + Gin + GORM) - ✅ 完整的分层架构 (API/Service/Repository) - ✅ PostgreSQL数据库设计和迁移脚本 - ✅ JWT认证系统和权限控制 - ✅ 用户、照片、分类、标签等核心模型 - ✅ 中间件系统 (认证、CORS、日志) - ✅ 配置管理和环境变量支持 - ✅ 结构化日志和错误处理 - ✅ Makefile构建和部署脚本 ## 管理后台架构 (React + TypeScript) - ✅ Vite + React 18 + TypeScript现代化架构 - ✅ 路由系统和状态管理 (Zustand + TanStack Query) - ✅ 基于Radix UI的组件库基础 - ✅ 认证流程和权限控制 - ✅ 响应式设计和主题系统 ## 数据库设计 - ✅ 用户表 (角色权限、认证信息) - ✅ 照片表 (元数据、EXIF、状态管理) - ✅ 分类表 (层级结构、封面图片) - ✅ 标签表 (使用统计、标签云) - ✅ 关联表 (照片-标签多对多) ## 技术特点 - 🚀 高性能: Gin框架 + GORM ORM - 🔐 安全: JWT认证 + 密码加密 + 权限控制 - 📊 监控: 结构化日志 + 健康检查 - 🎨 现代化: React 18 + TypeScript + Vite - 📱 响应式: Tailwind CSS + Radix UI 参考文档: docs/development/saved-docs/
This commit is contained in:
76
backend/pkg/logger/logger.go
Normal file
76
backend/pkg/logger/logger.go
Normal file
@ -0,0 +1,76 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"photography-backend/internal/config"
|
||||
)
|
||||
|
||||
// InitLogger 初始化日志记录器
|
||||
func InitLogger(cfg *config.LoggerConfig) (*zap.Logger, error) {
|
||||
// 设置日志级别
|
||||
var level zapcore.Level
|
||||
switch cfg.Level {
|
||||
case "debug":
|
||||
level = zapcore.DebugLevel
|
||||
case "info":
|
||||
level = zapcore.InfoLevel
|
||||
case "warn":
|
||||
level = zapcore.WarnLevel
|
||||
case "error":
|
||||
level = zapcore.ErrorLevel
|
||||
default:
|
||||
level = zapcore.InfoLevel
|
||||
}
|
||||
|
||||
// 创建编码器配置
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
encoderConfig.TimeKey = "timestamp"
|
||||
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
|
||||
// 创建编码器
|
||||
var encoder zapcore.Encoder
|
||||
if cfg.Format == "json" {
|
||||
encoder = zapcore.NewJSONEncoder(encoderConfig)
|
||||
} else {
|
||||
encoder = zapcore.NewConsoleEncoder(encoderConfig)
|
||||
}
|
||||
|
||||
// 创建写入器
|
||||
var writers []zapcore.WriteSyncer
|
||||
|
||||
// 控制台输出
|
||||
writers = append(writers, zapcore.AddSync(os.Stdout))
|
||||
|
||||
// 文件输出
|
||||
if cfg.Output == "file" && cfg.Filename != "" {
|
||||
// 确保日志目录存在
|
||||
if err := os.MkdirAll("logs", 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileWriter := &lumberjack.Logger{
|
||||
Filename: cfg.Filename,
|
||||
MaxSize: cfg.MaxSize,
|
||||
MaxAge: cfg.MaxAge,
|
||||
MaxBackups: 10,
|
||||
LocalTime: true,
|
||||
Compress: cfg.Compress,
|
||||
}
|
||||
writers = append(writers, zapcore.AddSync(fileWriter))
|
||||
}
|
||||
|
||||
// 合并写入器
|
||||
writer := zapcore.NewMultiWriteSyncer(writers...)
|
||||
|
||||
// 创建核心
|
||||
core := zapcore.NewCore(encoder, writer, level)
|
||||
|
||||
// 创建日志记录器
|
||||
logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
165
backend/pkg/response/response.go
Normal file
165
backend/pkg/response/response.go
Normal file
@ -0,0 +1,165 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Response 统一响应结构
|
||||
type Response struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// Meta 元数据
|
||||
type Meta struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
}
|
||||
|
||||
// PaginatedResponse 分页响应
|
||||
type PaginatedResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
Pagination *Pagination `json:"pagination"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// Pagination 分页信息
|
||||
type Pagination struct {
|
||||
Page int `json:"page"`
|
||||
Limit int `json:"limit"`
|
||||
Total int64 `json:"total"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
HasNext bool `json:"has_next"`
|
||||
HasPrev bool `json:"has_prev"`
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func Success(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
func Error(code int, message string) *Response {
|
||||
return &Response{
|
||||
Success: false,
|
||||
Code: code,
|
||||
Message: message,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Created 创建成功响应
|
||||
func Created(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusCreated,
|
||||
Message: "Created successfully",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Updated 更新成功响应
|
||||
func Updated(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Updated successfully",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Deleted 删除成功响应
|
||||
func Deleted() *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Deleted successfully",
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Paginated 分页响应
|
||||
func Paginated(data interface{}, page, limit int, total int64) *PaginatedResponse {
|
||||
totalPages := int((total + int64(limit) - 1) / int64(limit))
|
||||
|
||||
return &PaginatedResponse{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
Pagination: &Pagination{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Total: total,
|
||||
TotalPages: totalPages,
|
||||
HasNext: page < totalPages,
|
||||
HasPrev: page > 1,
|
||||
},
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// BadRequest 400错误
|
||||
func BadRequest(message string) *Response {
|
||||
return Error(http.StatusBadRequest, message)
|
||||
}
|
||||
|
||||
// Unauthorized 401错误
|
||||
func Unauthorized(message string) *Response {
|
||||
return Error(http.StatusUnauthorized, message)
|
||||
}
|
||||
|
||||
// Forbidden 403错误
|
||||
func Forbidden(message string) *Response {
|
||||
return Error(http.StatusForbidden, message)
|
||||
}
|
||||
|
||||
// NotFound 404错误
|
||||
func NotFound(message string) *Response {
|
||||
return Error(http.StatusNotFound, message)
|
||||
}
|
||||
|
||||
// InternalServerError 500错误
|
||||
func InternalServerError(message string) *Response {
|
||||
return Error(http.StatusInternalServerError, message)
|
||||
}
|
||||
|
||||
// ValidationError 验证错误
|
||||
func ValidationError(errors map[string]string) *Response {
|
||||
return &Response{
|
||||
Success: false,
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Message: "Validation failed",
|
||||
Data: errors,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user