一、项目结构示例
注:该项目是我个人项目示例,各位可以自行命令或设计目录结构。
root@ubuntu:/SuperxonWebSite#tree.├──Apps│├──commonConfigurationItem││└──PersonInChargeWarningInfo││└──...│└──humanResources│└──...├──Assets│├──ini││└──common.ini│└──mp4│└──...├──Databases│└──...├──go.mod├──go.sum├──Middlewares│└──...├──Models│└──ProductionMaterialRelation│└──...├──Pb│└──user│└──...├──Router│└──routers.go├──Services│└──kafka.go├──main.go└──Utils└──...
二、文件逐个分析
1.Apps
主体业务层,放入除数据库操作的所有的逻辑处理业务,提供给路由配置连接的函数。
示例:
func GetVideoInfoListHandler(c *gin.Context) { videoInfoList, err := FileManager.GetVideoInfoList() //这段代码就是在调取封装好的数据库操作 if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) } else { c.JSON(http.StatusOK, videoInfoList) }}
2.Assets
资源层,放入的是配置文件、外部文件和保存上传的文件,当然也可以把这三部分拆分出来。
3.Databases
数据库的初始化连接层,放入的是各类数据库连接文件。
例如:
mysql.go
packageDatabasesimport("fmt"_"github.com/go-sql-driver/mysql"_"github.com/jinzhu/gorm/dialects/mysql""github.com/jmoiron/sqlx")varSuperxonUserDb*sqlx.DBfuncInitMysql()error{varerrerrorSuperxonUserDb,err=sqlx.Open("mysql","username:password@(172.20.3.12:3306)/superxon-user?charset=utf8mb4&parseTime=true&loc=Local")iferr!=nil{fmt.Println("opensuperxon-userfailed,",err)returnerr}SuperxonUserDb.SetMaxIdleConns(5)SuperxonUserDb.SetMaxOpenConns(15)returnnil}funcCloseMysql(){_=SuperxonUserDb.Close()}
4.Middlewares
中间件层,放入的就是自定义的中间件。
例如:
package Middlewaresimport ( "context" "github.com/gin-gonic/gin" "net/http" "time")func TimeoutMiddleware(timeout time.Duration) func(c *gin.Context) { return func(c *gin.Context) { // wrap the request context with a timeout ctx, cancel := context.WithTimeout(c.Request.Context(), timeout) defer func() { // check if context timeout was reached if ctx.Err() == context.DeadlineExceeded { // write response and abort the request c.Writer.WriteHeader(http.StatusGatewayTimeout) c.Abort() } //cancel to clear resources after finished cancel() }() // replace request with context wrapped request c.Request = c.Request.WithContext(ctx) c.Next() }}
5.Models
数据库模型操作层,对数据库CRUD的处理都在这里。
示例:
packageUserRelationimport("SuperxonWebSite/Databases""errors")typeRightManagerstruct{Idint64`db:"id"`Usernamestring`db:"username"`Nicknamestring`db:"nickname"`RightItemstring`db:"right_item"`}funcFindAllRightManager()([]RightManager,error){varres=make([]RightManager,0)sqlStr:="SELECT*FROMright_manager"err:=Databases.SuperxonUserDb.Select(&res,sqlStr)iferr!=nil{returnnil,err}returnres,nil}
6.Pb
grpc微服务 *.pb.go文件的存放点。
7.Router
放入路由配置文件 routers.go
。
8.Services
放入的是其它服务组件的初始化等等。
例如kafka.go
packageServicesimport("encoding/json""errors""github.com/tal-tech/go-queue/kq""time")varEmailOfWaringInfoKqPusher*kq.PushervarEmailOfWaringInfoWithStationKqPusher*kq.PushervarEmailOfPtrChangeInfoKqPusher*kq.PusherfuncKafkaInit(){EmailOfWaringInfoKqPusher=kq.NewPusher([]string{"172.20.3.19:9092","172.20.3.19:9093","172.20.3.19:9094"},"EmailOfWarningInfo")EmailOfWaringInfoWithStationKqPusher=kq.NewPusher([]string{"172.20.3.19:9092","172.20.3.19:9093","172.20.3.19:9094"},"EmailOfWarningInfoWithStation")EmailOfPtrChangeInfoKqPusher=kq.NewPusher([]string{"172.20.3.19:9092","172.20.3.19:9093","172.20.3.19:9094"},"EmailOfPtrChangedInfo")}
9.Utils
通用工具目录,比如字符处理啊,时间处理等等。
作者:小小小丶叶子著作权归作者所有。