package main import ( "log" "net/http" "time" "github.com/gin-gonic/gin" "github.com/gin-contrib/cors" ) func main() { // 创建 Gin 引擎 r := gin.Default() // 配置 CORS r.Use(cors.New(cors.Config{ AllowOrigins: []string{"http://localhost:3000", "http://localhost:3002", "http://localhost:3003"}, AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Type", "Authorization"}, AllowCredentials: true, MaxAge: 12 * time.Hour, })) // 健康检查 r.GET("/health", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "status": "ok", "timestamp": time.Now().Unix(), "version": "1.0.0", }) }) // 模拟登录接口 r.POST("/api/v1/auth/login", func(c *gin.Context) { var req struct { Username string `json:"username"` Password string `json:"password"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // 简单验证 if req.Username == "admin" && req.Password == "admin123" { c.JSON(http.StatusOK, gin.H{ "user": gin.H{ "id": "1", "username": "admin", "email": "admin@example.com", "role": "admin", "isActive": true, }, "access_token": "mock-jwt-token", "expires_in": 86400, }) } else { c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"}) } }) // 模拟照片列表接口 r.GET("/api/v1/photos", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "data": []gin.H{ { "id": "1", "title": "Sample Photo 1", "description": "This is a sample photo", "url": "https://picsum.photos/800/600?random=1", "status": "published", "createdAt": time.Now().Format(time.RFC3339), }, { "id": "2", "title": "Sample Photo 2", "description": "This is another sample photo", "url": "https://picsum.photos/800/600?random=2", "status": "published", "createdAt": time.Now().Format(time.RFC3339), }, }, "total": 2, "page": 1, "limit": 10, "totalPages": 1, }) }) // 模拟仪表板统计接口 r.GET("/api/v1/dashboard/stats", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "photos": gin.H{ "total": 156, "thisMonth": 23, "today": 5, }, "categories": gin.H{ "total": 12, "active": 10, }, "tags": gin.H{ "total": 45, }, "users": gin.H{ "total": 8, "active": 7, }, }) }) // 模拟分类列表接口 r.GET("/api/v1/categories", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "data": []gin.H{ { "id": "1", "name": "风景", "slug": "landscape", "description": "自然风景摄影", "photoCount": 25, "isActive": true, }, { "id": "2", "name": "人像", "slug": "portrait", "description": "人物肖像摄影", "photoCount": 18, "isActive": true, }, }, "total": 2, }) }) // 模拟标签列表接口 r.GET("/api/v1/tags", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "data": []gin.H{ { "id": "1", "name": "日落", "slug": "sunset", "photoCount": 12, }, { "id": "2", "name": "城市", "slug": "city", "photoCount": 8, }, }, "total": 2, }) }) // 模拟用户列表接口 r.GET("/api/v1/users", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "data": []gin.H{ { "id": "1", "username": "admin", "email": "admin@example.com", "role": "admin", "isActive": true, }, { "id": "2", "username": "editor", "email": "editor@example.com", "role": "editor", "isActive": true, }, }, "total": 2, }) }) // 启动服务器 log.Println("🚀 Simple backend server starting on :8080") log.Fatal(r.Run(":8080")) }