# 一、 项目介绍
go 的 web 脚手架项目。
技术栈 goframe 2.x,使用 sqlite 文件数据库。
包含功能有
- 登录,token 刷新,用户的增删改查
## 1.1 登录
登录采用 JWT 方式,登录成功生成 jwt 格式 token,其他接口会效验 token 是否有效(从 jwt 的层面,不额外查表),jwt 是分布式有效的,所以 token 不用存储,服务是无状态的。可以通过 manifest/config/config.yaml 中的配置修改 token 的存活时长。
# 二、 快速开始
## 2.1 运行
```
go run main.go
```
## 2.2 验证
本地执行
```
curl --location 'http://127.0.0.1:8000/api/user/login' \
--header 'Content-Type: application/json' \
--data '{"username":"admin","password":"admin"}'
```
获得返回
```
{"code":0,"message":"","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNjg2MjE0MTgzfQ.WZV1YvwzokEgDL1urWYoUPpfWSsq3TisO3f6zutmjf4","role":""}}
```
# 二、 代码结构
## 2.1 项目框架
### 2.1.1 总体项目结构
> 项目结构分为 4个部分,api, hack, internal, manifest, 是 goframe 标准结构。
简单的介绍一下这几部分的核心功能:
- api: 定义接口的 url,输入输出字段
- hack: gf 工具配置,可以通过数据库生成通用 dao,service 代码
- internal: 业务实现代码
- manifest: 交付清单,包含程序编译、部署、运行、配置的文件。
> goframe 框架结构
| fff | fff |
| --- | --- |
| ffff| dddd |
| 目录\文件名称 | 说明 | 描述 |
| --- | --- | --- |
| api | 对外接口 | 对外提供服务的输入/输出数据结构定义。考虑到版本管理需要,往往以api/v1...存在。 |
| hack | 工具脚本 | 存放项目开发工具、脚本等内容。例如,CLI工具的配置,各种shell/bat脚本等文件。|
| internal | 内部逻辑 | 业务逻辑存放目录。通过Golang internal特性对外部隐藏可见性。|
| - cmd | 入口指令 | 命令行管理目录。可以管理维护多个命令行。|
| - consts | 常量定义 | 项目所有常量定义。|
| - controller | 接口处理 | 接收/解析用户输入参数的入口/接口层。|
| - dao | 数据访问 | 数据访问对象,这是一层抽象对象,用于和底层数据库交互,仅包含最基础的 CURD 方法|
| - logic | 业务封装 | 业务逻辑封装管理,特定的业务逻辑实现和封装。往往是项目中最复杂的部分。|
| - model | 结构模型 | 数据结构管理模块,管理数据实体对象,以及输入与输出数据结构定义。|
| - do | 领域对象 | 用于dao数据操作中业务模型与实例模型转换,由工具维护,用户不能修改。|
| - entity | 数据模型 | 数据模型是模型与数据集合的一对一关系,由工具维护,用户不能修改。|
| - service | 业务接口 | 用于业务模块解耦的接口定义层。具体的接口实现在logic中进行注入。|
| manifest | 交付清单 | 包含程序编译、部署、运行、配置的文件。常见内容如下:|
| - config | 配置管理 | 配置文件存放目录。|
| - docker | 镜像文件 | Docker镜像相关依赖文件,脚本文件等等。|
| - deploy | 部署文件 | 部署相关的文件。默认提供了Kubernetes集群化部署的Yaml模板,通过kustomize管理。|
| resource | 静态资源 | 静态资源文件。这些文件往往可以通过 资源打包/镜像编译 的形式注入到发布文件中。|
| go.mod | 依赖管理 | 使用Go Module包管理的依赖描述文件。|
| main.go | 入口文件 | 程序入口文件。|
## 2.2 业务封装
### 2.2.1 登录、鉴权
- internal/middleware/middleware.go: 提供统一鉴权处理
- internal/controller/login.go: 提供登录
- internal/controller/user.go: 提供 token 续签
# 三、 FAQ
## 1. 初始化项目失败
错误信息
```
go run main.go
# github.com/gogf/gf/contrib/drivers/sqlite/v2
../../../pkg/mod/github.com/gogf/gf/contrib/drivers/sqlite/[email protected]/sqlite.go:81:12: config.Extra undefined (type *gdb.ConfigNode has no field or method Extra)
../../../pkg/mod/github.com/gogf/gf/contrib/drivers/sqlite/[email protected]/sqlite.go:86:40: config.Extra undefined (type *gdb.ConfigNode has no field or method Extra)
../../../pkg/mod/github.com/gogf/gf/contrib/drivers/sqlite/[email protected]/sqlite.go:150:22: undefined: gutil.GetOrDefaultStr
```
原因:gdb 版本太老,无法识别 Extra 字段。您可以尝试升级到最新版本的 gdb 包,或者尝试使用其他合适的配置来连接 SQLite 数据库,以解决此问题。
解决方案,在 go.sum 中将 github.com/gogf/gf/contrib/drivers/sqlite/v2 升级到 v2.4.0,重新 `go mod tidy`
## 2. 生成 dao 失败
错误信息
```
gf gen dao -r user
error format "internal/dao/.go" go files: internal/dao/.go:22:3: expected 'IDENT', found '=' (and 1 more errors)
generated: internal/dao/.go
generated: internal/dao/internal/.go
error format "internal/model/do/.go" go files: internal/model/do/.go:13:7: expected 'IDENT', found 'struct' (and 1 more errors)
generated: internal/model/do/.go
error format "internal/model/entity/.go" go files: internal/model/entity/.go:12:7: expected 'IDENT', found 'struct' (and 1 more errors)
generated: internal/model/entity/.go
```
原因,命令错误,下面是正确命令,-r 是 removePrefix 的意思,把 user remove,表名就为空了
```
gf gen dao
```
## 3. go test 单元测试失败
将 _test.go 放在 controller 下,进入目录执行 go test 显示数据库驱动 sqlite 找不到。单元测试中并没有数据库连接,怀疑编译了 controller 文件。
就算 controller 增加 sqlite 驱动也不能启动。
## 4. 如何出 window 包
```
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o login.exe main.go
打包配置文件和数据库文件
zip login.zip login.exe manifest/config/config.yaml manifest/db/user.db
```
## 5. dao 根据传入的查询条件动态增加对应的查询项
通过动态增加 where 并重新赋值的方式。
```
model := dao.Pile.Ctx(ctx).LeftJoin("station", "station.id = pile.station_id")
if query.StationId != 0 {
model = model.Where("pile.station_id = ?", query.StationId)
}
if query.Code != "" {
model = model.Where("pile.code like ?", fmt.Sprintf("%%%s%%", query.Code))
}
count, err = model.Count()
```
## 6. gf 如何进行查询翻页
# 四、 附录: GoFrame
Project Makefile Commands:
- `make cli`: Install or Update to the latest GoFrame CLI tool.
- `make dao`: Generate go files for `Entity/DAO/DO` according to the configuration file from `hack` folder.
- `make service`: Parse `logic` folder to generate interface go files into `service` folder.
- `make image TAG=xxx`: Run `docker build` to build image according `manifest/docker`.
- `make image.push TAG=xxx`: Run `docker build` and `docker push` to build and push image according `manifest/docker`.
- `make deploy TAG=xxx`: Run `kustomize build` to build and deploy deployment to kubernetes server group according `manifest/deploy`.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
土拨鼠开源充电系统是一套包含鸿蒙、微信小程序、云平台充电设备管理系统。 鸿蒙App使用HarmonyOS 4.0开发,小程序使用uniapp开发;功能涉及:登录、注册、查找充电站和充电站、在线充电、订单查询、个人中心等 云平台使用Vue3+vite+ts+Element Plus开发,多租户、权限、审计、充电站/充电桩管理、订单管理、价格规则、充值等 后台使用 Go 语言 goframe 框架土拨鼠开源充电系统是一套包含鸿蒙App、微信小程序、云平台和后台的充电设备管理系统。鸿蒙App使用Api9+Stage模式+ArkTS+ArkUI开发,包含登录、注册、充电站列表、充电桩信息、在线充电、订单查询、个人中心等多个功能。小程序使用 Uniapp 开发,为电动汽车用户提供充电服务,包括登录、注册、查找充电站、充电桩信息、在线充电、订单查询、个人中心等多个功能。云平台技术涉及vue3+vite+ts+Element Plus 多租户、权限、审计、充电站/充电桩管理、订单管理,价格规则,充值等。后台使用 Go 语言开发,使用 goframe2 框架和 sqlite 文件数据
资源推荐
资源详情
资源评论
收起资源包目录
土拨鼠开源充电系统是一套包含鸿蒙微信小程序云平台充电设备管理系统 (923个子文件)
hvigorw.bat 2KB
.eslintrc.cjs 2KB
.stylelintrc.cjs 2KB
commitlint.config.cjs 547B
uniicons.css 8KB
style.css 1KB
user.db 92KB
Dockerfile 647B
.eslintignore 17B
ChargeOrder.ets 4KB
ChargeList.ets 3KB
Charging.ets 3KB
UserPage.ets 3KB
Login.ets 3KB
Register.ets 2KB
TestAbility.ets 2KB
PileList.ets 2KB
BreakpointSystem.ets 2KB
Index.ets 2KB
ChargeModel.ets 2KB
UserModel.ets 2KB
Ability.test.ets 2KB
ChargeIndex.ets 2KB
PricePop.ets 2KB
BreakpointConstants.ets 1024B
Index.ets 835B
HeaderComponent.ets 580B
ScanIndex.ets 527B
UserHeader.ets 484B
ChargeType.ets 364B
BreanpointType.ets 330B
UserIndex.ets 295B
OrderType.ets 286B
PriceType.ets 195B
PileType.ets 190B
List.test.ets 97B
Tenant.ets 88B
ResponseData.ets 74B
loding.gif 989KB
.gitattributes 22B
.gitignore 253B
.gitignore 197B
.gitignore 177B
.gitignore 119B
.gitignore 55B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
.gitkeep 0B
charge_order.go 8KB
wx_charge.go 7KB
wx_charge_test.go 6KB
wx_login.go 6KB
wx_charge.go 6KB
user.go 6KB
cmd.go 4KB
custom_driver.go 4KB
tenant.go 4KB
file.go 4KB
parse_test.go 4KB
role_test.go 3KB
station_test.go 3KB
models.go 3KB
charge_price_test.go 3KB
charge_order.go 3KB
pile.go 3KB
audit_log.go 3KB
tenant.go 3KB
role_permission.go 3KB
tenant.go 3KB
pay_record.go 3KB
charge_price.go 3KB
tenant_test.go 3KB
wx_user.go 3KB
station.go 3KB
pile_test.go 3KB
user_test.go 3KB
user_wallet.go 3KB
user.go 3KB
pile.go 3KB
role.go 3KB
auth_middleware.go 2KB
parse.go 2KB
wx_login.go 2KB
role.go 2KB
common_test.go 2KB
common_test.go 2KB
common_test.go 2KB
common_test.go 2KB
charge_price.go 2KB
共 923 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
传奇开心果编程
- 粉丝: 8593
- 资源: 335
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Music-Player +PlayerActivity+ rockplayer+ SeeJoPlayer 播放器JAVA源码
- vscode-1.46.0.tar源码文件
- 最近很火植物大战僵尸杂交版2.08苹果+安卓+PC+防闪退工具V2+修改工具+高清工具+通关存档整合包更新
- 超级好用的截图工具PixPin,可录制Gif图
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
- node-v18.20.2-linux-arm64
- 222222222222
- 16张相机标定图片,可复现本文畸变矫正
- dbeaver-ce-23.3.1-x86_64-setup.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功