Go-React做一个todolist(服务端)【一】项目初始化

2024-03-31 04:52

本文主要是介绍Go-React做一个todolist(服务端)【一】项目初始化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

后端仓库地址

地址

项目依赖

# gin
go get -u github.com/gin-gonic/gin
# viper日志
go get -u github.com/spf13/viper
# 数据库和gorm
go get -u gorm.io/driver/mysql
go get -u gorm.io/gorm
# uuid
go get -u github.com/google/uuid
# token
go get -u github.com/golang-jwt/jwt/v5
# 邮箱
go get github.com/jordan-wright/email
# swagger
go get -u github.com/swaggo/swag/cmd/swag
go install github.com/swaggo/swag/cmd/swag@latest
go get -u github.com/swaggo/files
go get -u github.com/swaggo/gin-swagger# base64验证码
go get -u github.com/mojocn/base64Captcha
# gokit 工具集合
go get github.com/songzhibin97/gkit

项目结构搭建

先执行 go mod init ToDoList
在这里插入图片描述

初始化模块

在initialize/index.go中

package initializeimport ("ToDoList/global""fmt"
)func Works() {// 读取配置文件global.GVA_VIPER = Viper()// 初始化缓存组件Cache.InitCache()// 初始化数据库并注册表global.GVA_DB = GormMysql.InitGormMysql()GormMysql.TableInit()// 启动服务global.GVA_SERVER = GinEngine.InitEngine()if global.GVA_SERVER != nil {// 注册中间件GinEngine.InitMiddleware()// 注册路由GinEngine.InitRouter()// 运行服务global.GVA_SERVER.Run(fmt.Sprintf(":%s", global.GVA_CONFIG.App.Port))}
}

在这里插入图片描述

gin初始化

在initialize/gin.go中

package initializeimport ("ToDoList/docs""ToDoList/global""ToDoList/middleware""ToDoList/router""github.com/gin-gonic/gin"swaggerFiles "github.com/swaggo/files"ginSwagger "github.com/swaggo/gin-swagger"
)type ginEngine struct{}// 初始化中间件
func (receiver ginEngine) InitMiddleware() {// cors跨域中间件global.GVA_SERVER.Use(middleware.CorsByRules())// swagger中间件docs.SwaggerInfo.BasePath = global.GVA_CONFIG.App.RouterPrefixglobal.GVA_SERVER.GET(global.GVA_CONFIG.App.RouterPrefix+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}// 初始化路由
func (receiver *ginEngine) InitRouter() {// 全局路由前缀globalRouterGroup := global.GVA_SERVER.Group(global.GVA_CONFIG.App.RouterPrefix)router.UserRouter.InitUserRouter(globalRouterGroup)
}// 初始化Gin引擎
func (receiver *ginEngine) InitEngine() *gin.Engine {r := gin.Default()return r
}var GinEngine = new(ginEngine)

gorm初始化

在initialize/gorm.go中

package initializeimport ("ToDoList/global""ToDoList/model""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""gorm.io/gorm/schema""log""os""time"
)type gormMysql struct{}func (receiver *gormMysql) TableInit() {err := global.GVA_DB.AutoMigrate(model.User{},model.Backlog{},)if err != nil {fmt.Println("注册表发生错误:", err)panic("初始化表失败")}fmt.Println("~~~The database table is successfully registered~~~")
}func (receiver *gormMysql) InitGormMysql() *gorm.DB {password := global.GVA_CONFIG.Mysql.Passwordusername := global.GVA_CONFIG.Mysql.Usernameport := global.GVA_CONFIG.Mysql.PortdbName := global.GVA_CONFIG.Mysql.Dbnamedsn := fmt.Sprintf("%s:%s@tcp(127.0.0.1:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, port, dbName)db, err := gorm.Open(mysql.New(mysql.Config{DSN:                       dsn,   // DSN data source nameDefaultStringSize:         256,   // string 类型字段的默认长度 如果该字段是字符串并作为主键会造成索引超长DisableDatetimePrecision:  true,  // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持DontSupportRenameIndex:    true,  // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引DontSupportRenameColumn:   true,  // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列SkipInitializeWithVersion: false, // 根据当前 MySQL 版本自动配置}), &gorm.Config{ //连接的配置SkipDefaultTransaction: false, // 默认false,增删改都是事务操作来保证数据一致性,能提升一点性能NamingStrategy: schema.NamingStrategy{TablePrefix:         "",    // 如果设置了会给每个表名加前缀SingularTable:       true,  // 单数表名,如果false会在表明后加sNameReplacer:        nil,   // 字符转转换器,转换字段名NoLowerCase:         false, //当设置为true时,NoLowerCase选项将禁用表名和列名的蛇形命名转换。保持表名和列名的原始大小写形式。IdentifierMaxLength: 0,     //不限制数据库标识符(如表名、列名)的最大长度。},Logger: logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), // io writerlogger.Config{SlowThreshold:             time.Second,   // Slow SQL thresholdLogLevel:                  logger.Silent, // Log levelIgnoreRecordNotFoundError: true,          // Ignore ErrRecordNotFound error for loggerParameterizedQueries:      true,          // Don't include params in the SQL logColorful:                  false,         // Disable color},), // 可以自定义日志DisableForeignKeyConstraintWhenMigrating: true, //true时,建表将不会建立物理外键,代码中我们采用逻辑外键提升数据库操作效率})if err != nil {panic(err.Error())}sqlDB, _ := db.DB()sqlDB.SetMaxIdleConns(global.GVA_CONFIG.Mysql.MaxIdleConns)sqlDB.SetMaxOpenConns(global.GVA_CONFIG.Mysql.MaxOpenConns)return db
}var GormMysql = new(gormMysql)

缓存kit初始化

在initialize/cache.go中

package initializeimport ("ToDoList/global""ToDoList/util""github.com/songzhibin97/gkit/cache/local_cache"
)type cache struct{}func (receiver *cache) InitCache() {dr, err := util.BasicUtils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)if err != nil {panic(err)}global.BlackCache = local_cache.NewCache(local_cache.SetDefaultExpire(dr),)
}var Cache = new(cache)

读取配置文件Viper初始化

package initializeimport ("ToDoList/enum""ToDoList/global""flag""fmt""github.com/fsnotify/fsnotify""github.com/gin-gonic/gin""github.com/spf13/viper"
)// Viper //
// 优先级: 命令行 > 环境变量 > 默认值
// Author [SliverHorn](https://github.com/SliverHorn)
func Viper(path ...string) *viper.Viper {var configFile stringif len(path) == 0 {flag.StringVar(&configFile, "c", "", "choose config file.")flag.Parse()if configFile == "" { // 判断命令行参数是否为空switch gin.Mode() {case gin.DebugMode:configFile = enum.ConfigDefaultFilefmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.Mode(), enum.ConfigDebugFile)case gin.ReleaseMode:configFile = enum.ConfigReleaseFilefmt.Printf("您正在使用gin模式的%s环境名称,config的路径为", gin.Mode(), enum.ConfigReleaseFile)case gin.TestMode:configFile = enum.ConfigTestFilefmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.Mode(), enum.ConfigTestFile)}} else { // 命令行参数不为空 将值赋值于configfmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%s\n", configFile)}} else { // 函数传递的可变参数的第一个值赋值于configconfigFile = path[0]fmt.Printf("您正在使用func Viper()传递的值,config的路径为%s\n", configFile)}// 初始化Viper對象v := viper.New()// 设置配置文件的路径v.SetConfigFile(configFile)// 配置文件类型v.SetConfigType("yaml")err := v.ReadInConfig()if err != nil {panic(fmt.Errorf("Fatal error config file: %s \n", err))}// 当配置文件变化调用此hookv.OnConfigChange(func(e fsnotify.Event) {fmt.Println("config file changed:", e.Name)if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {fmt.Println(err)}})// 配置文件变动会重读不必重启服务v.WatchConfig()if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {panic(err)}return v
}

模型

用户

package modelimport ("github.com/google/uuid""gorm.io/gorm"
)type User struct {gorm.ModelUsername   string    `json:"userName" gorm:"comment:用户名"`NickName   string    `json:"nickName" gorm:"comment:昵称"`Password   string    `json:"password" gorm:"comment:密码"`Identity   string    `json:"身份" gorm:"comment:用户身份"`Email      string    `json:"email" gorm:"comment:用户邮箱"`UUID       uuid.UUID `json:"uuid" gorm:"index;comment:用户UUID"`Avatar     string    `json:"avatar" gorm:"comment:用户头像;default:https://fancyfish.top/hero.jpg"`ThemeColor string    `json:"themeColor" gorm:"comment:用户主题颜色"`Enable     bool      `json:"enable" gorm:"comment:用户是否可用;default:true"`Backlog    Backlog
}func (receiver User) TableName() string {return "user"
}

待办事项

package modelimport ("gorm.io/gorm"
)type Backlog struct {gorm.ModelBacklogContent  string    `json:"backlogContent" gorm:"comment:代办事项内容"`Completed       bool      `json:"completed" gorm:"comment:是否完成;default:false"`UserId          uint      `json:"user_id"`ParentId        *uint     `json:"parent_id"`ChildrenBacklog []Backlog `gorm:"foreignkey:ParentId;"`
}func (receiver Backlog) TableName() string {return "backlog"
}

路由

package routerimport ("ToDoList/api""github.com/gin-gonic/gin"
)type userRouter struct{}func (receiver userRouter) InitUserRouter(R *gin.RouterGroup) {r := R.Group("user"){r.POST("register", api.UserApi.Register)r.POST("login", api.UserApi.Login)r.POST("change_password", api.UserApi.ChangePassword)r.PUT("set_userinfo", api.UserApi.SetSelfInfo)r.GET("get_userInfo", api.UserApi.GetUserInfo)r.POST("get_captcha", api.UserApi.GetCaptcha)}
}var UserRouter = new(userRouter)

这篇关于Go-React做一个todolist(服务端)【一】项目初始化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/863433

相关文章

Golang基于内存的键值存储缓存库go-cache

《Golang基于内存的键值存储缓存库go-cache》go-cache是一个内存中的key:valuestore/cache库,适用于单机应用程序,本文主要介绍了Golang基于内存的键值存储缓存库... 目录文档安装方法示例1示例2使用注意点优点缺点go-cache 和 Redis 缓存对比1)功能特性

Go 1.23中Timer无buffer的实现方式详解

《Go1.23中Timer无buffer的实现方式详解》在Go1.23中,Timer的实现通常是通过time包提供的time.Timer类型来实现的,本文主要介绍了Go1.23中Timer无buff... 目录Timer 的基本实现无缓冲区的实现自定义无缓冲 Timer 实现更复杂的 Timer 实现总结在

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网