go 服务端分层框架设计
框架分为四层。models,controllers,repositories,services
以User为例
1、controller示例
package controllers import ( "appserver/repositories" "appserver/services" "net/http" "github.com/gin-gonic/gin" ) var UserControllerInstance *UserController type UserController struct { userService services.UserService } func init() { userRepository := repositories.NewUserRepository() userService := services.NewUserService(userRepository) UserControllerInstance = NewUserController(userService) } func NewUserController(userService services.UserService) *UserController { return &UserController{ userService: userService, } } func (uc *UserController) Login(c *gin.Context) { // 从请求中获取用户名和密码 username := c.PostForm("username") password := c.PostForm("password") // 调用用户服务进行身份验证 user, err := uc.userService.AuthenticateUser(username, password) if err != nil { // 处理身份验证失败的逻辑 c.JSON(http.StatusUnauthorized, gin.H{ "error": "Invalid credentials", }) return } user.ID = 1 // 身份验证成功,执行其他逻辑 // ... // 返回响应 c.JSON(http.StatusOK, gin.H{ "message": "Login successful", }) }
2、models示例
package models import "time" // User 表示用户模型 type User struct { ID uint `json:"id"` Username string `json:"username"` Password string `json:"-"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` } // CreateUserInput 表示创建用户的输入数据 type CreateUserInput struct { Username string `json:"username"` } // UpdateUserInput 表示更新用户的输入数据 type UpdateUserInput struct { Username string `json:"username"` Password string `json:"password"` }
3、repositories示例
package repositories import ( "database/sql" ) type UserRepository interface { IsValidUser(username, password string) bool } type userRepository struct { db *sql.DB } func NewUserRepository(db *sql.DB) UserRepository { return &userRepository{ db: db, } } func (ur *userRepository) IsValidUser(username, password string) bool { stmt, err := ur.db.Prepare("SELECT COUNT(*) FROM users WHERE username = ? AND password = ?") if err != nil { // 处理错误 return false } defer stmt.Close() var count int err = stmt.QueryRow(username, password).Scan(&count) if err != nil { // 处理查询错误 return false } return count > 0 }
4、services示例
package services import ( "appserver/repositories" ) type UserService interface { IsValidUser(username, password string) bool } type userService struct { userRepository repositories.UserRepository } func NewUserService(userRepository repositories.UserRepository) UserService { return &userService{ userRepository: userRepository, } } func (us *userService) IsValidUser(username, password string) bool { return us.userRepository.IsValidUser(username, password) }
5、请求消息处理
import( "appserver/controllers" ) // 用户登录 router.POST("/user/login", controllers.UserControllerInstance.Login)
本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
标签:
go
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)