feat(core): 优化路径处理和配置端口并添加项目规范文档
- 将所有文件路径转换为 URL 友好格式,统一使用正斜杠,确保跨平台兼容 - 修改默认服务器监听端口为 :8989,提升默认配置适用性 - 数据库初始化时创建数据库文件夹,避免路径不存在导致错误 - 新增 AGENTS.md 文档,详细规范项目开发流程、代码风格、架构设计和安全性能指导 - 修正头像路径显示逻辑,确保头像 URL 正确展示 - 增加应用启动和开发的标准操作指南,提升团队协作效率
This commit is contained in:
@@ -31,7 +31,7 @@ type AppConfig struct {
|
||||
|
||||
var AppConfigInstance = &Config{
|
||||
Server: ServerConfig{
|
||||
Port: ":8080",
|
||||
Port: ":8989",
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
|
||||
@@ -87,6 +87,8 @@ func (c *DiscussionController) CreatePost(ctx *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
// 将路径转换为 URL 友好的格式
|
||||
imagePath = filepath.ToSlash(imagePath)
|
||||
}
|
||||
|
||||
if err := c.discussionSvc.CreatePost(userID, title, content, imagePath); err != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -40,6 +41,14 @@ func (c *HomeController) Index(ctx *gin.Context) {
|
||||
|
||||
posts, _ := c.discussionSvc.GetApprovedPosts(30)
|
||||
|
||||
// 处理头像路径,确保使用正斜杠并添加前导斜杠
|
||||
for i := range posts {
|
||||
if posts[i].Avatar != "" {
|
||||
avatarPath := filepath.ToSlash(posts[i].Avatar)
|
||||
posts[i].Avatar = "/" + avatarPath
|
||||
}
|
||||
}
|
||||
|
||||
var postCount, userCount, onlineCount int64
|
||||
postCount, _ = c.discussionRepo.CountByStatus("approved")
|
||||
userCount, onlineCount, _ = c.userSvc.GetUserStats()
|
||||
@@ -93,7 +102,9 @@ func (c *HomeController) ShowPost(ctx *gin.Context) {
|
||||
|
||||
avatar := ""
|
||||
if detail.Post.User.Avatar != "" {
|
||||
avatar = "/" + detail.Post.User.Avatar
|
||||
// 确保路径使用正斜杠,并添加前导斜杠
|
||||
avatarPath := filepath.ToSlash(detail.Post.User.Avatar)
|
||||
avatar = "/" + avatarPath
|
||||
}
|
||||
|
||||
ctx.HTML(http.StatusOK, "post.html", gin.H{
|
||||
|
||||
@@ -47,7 +47,9 @@ func (c *UserController) ShowProfile(ctx *gin.Context) {
|
||||
|
||||
avatar := ""
|
||||
if profile.User.Avatar != "" {
|
||||
avatar = "/" + profile.User.Avatar
|
||||
// 确保路径使用正斜杠,并添加前导斜杠
|
||||
avatarPath := filepath.ToSlash(profile.User.Avatar)
|
||||
avatar = "/" + avatarPath
|
||||
}
|
||||
|
||||
ctx.HTML(http.StatusOK, "profile.html", gin.H{
|
||||
@@ -93,6 +95,8 @@ func (c *UserController) UploadAvatar(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.userSvc.UpdateAvatar(userID, imagePath)
|
||||
// 将路径转换为 URL 友好的格式(使用正斜杠)
|
||||
avatarPath := filepath.ToSlash(imagePath)
|
||||
c.userSvc.UpdateAvatar(userID, avatarPath)
|
||||
ctx.Redirect(http.StatusFound, "/profile?success=头像更新成功")
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"time"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"lv8girl/internal/models"
|
||||
|
||||
@@ -13,6 +14,12 @@ import (
|
||||
var DB *gorm.DB
|
||||
|
||||
func Init(databasePath string) error {
|
||||
// 确保数据库文件所在目录存在
|
||||
dir := filepath.Dir(databasePath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var err error
|
||||
DB, err = gorm.Open(sqlite.Open(databasePath), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"lv8girl/internal/models"
|
||||
"lv8girl/internal/repositories"
|
||||
)
|
||||
@@ -68,7 +70,9 @@ func (s *MessageService) GetConversations(userID uint) ([]ConversationView, erro
|
||||
|
||||
avatar := ""
|
||||
if otherUser.Avatar != "" {
|
||||
avatar = "/" + otherUser.Avatar
|
||||
// 确保路径使用正斜杠,并添加前导斜杠
|
||||
avatarPath := filepath.ToSlash(otherUser.Avatar)
|
||||
avatar = "/" + avatarPath
|
||||
}
|
||||
|
||||
conversations = append(conversations, ConversationView{
|
||||
|
||||
Reference in New Issue
Block a user