本项目采用了一系列 Golang 中比较流行的组件,可以以本项目为基础快速搭建 Restful Web API 。
本项目已经整合了许多开发 API 所必要的组件:
- Gin: 轻量级 Web 框架,自称路由速度是 Golang 最快的
- GORM: ORM 工具。本项目需要配合Mysql使用
- Gin-Session: Gin 框架提供的 Session 操作工具
- Go-Redis: Golang Redis 客户端
- godotenv: 开发环境下的环境变量工具,方便使用环境变量
- Gin-Cors: Gin 框架提供的跨域中间件
- 自行实现了国际化 i18n 的一些基本功能
- 本项目是使用基于 Cookie 实现的 Session 来保存登录状态的,如果需要可以自行修改为 token 验证
本项目已经预先实现了一些常用的代码方便参考和复用:
- 创建了用户模型
- 实现了
/api/v1/user/register
用户注册接口 - 实现了
/api/v1/user/login
用户登录接口 - 实现了
/api/v1/user/me
用户资料接口(需要登录后获取 Session) - 实现了
/api/v1/user/logout
用户登出接口(需要登录后获取 Session)
本项目已经预先创建了一系列文件夹划分出下列模块:
api
文件夹就是MVC框架的 Controller,负责协调各部件完成任务model
文件夹负责存储数据库模型和数据库操作相关的代码service
负责处理比较复杂的业务,把业务代码模型化可以有效提高业务代码的质量(比如用户注册,充值,下单等)serializer
储存通用的 JSON 模型,把model
得到的数据库模型转换成 API 需要的 JSON 对象cache
负责 Redis 缓存相关的代码auth
权限控制文件夹util
一些通用的小工具conf
放一些静态存放的配置文件,其中locales
内放置翻译相关的配置文件
项目在启动的时候依赖以下环境变量,但是在也可以在项目根目录创建 .env
文件设置环境变量便于使用(建议开发环境使用)
MYSQL_DSN="db_user:db_password@/db_name?charset=utf8&parseTime=True&loc=Local" # Mysql连接地址
REDIS_ADDR="127.0.0.1:6379" # Redis端口和地址
REDIS_PW="" # Redis连接密码
REDIS_DB="" # Redis库从0到10
SESSION_SECRE="" # Seesion密钥,必须设置而且不要泄露
GIN_MODE="debug"
本项目使用 go mod
管理依赖。
export GOPROXY=https://goproxy.io
go mod tidy
go mod vendor
go run main.go
项目运行后启动在3000端口(可以修改,参考gin文档)