Softhub软件下载站实战开发(二):项目基础框架搭建
前言 📜
上一篇文章介绍了softhub项目的主要架构和预期功能。从本篇开始将要逐步实现这些功能。本篇文章将详细介绍基础框架搭建和项目结构。
创建仓库 📦
先创建代码仓库,然后克隆到本地
git clone https://github.com/ciclebyte/softHub.git
gfast说明 📖
为了加快开发进度,本项目不从基础框架开始搭建,在成熟的后台管理框架上进行开发。
这里我们选用gfast,一款基于GoFrame
和vue-next-admin
的开箱即用的后台管理框架。
参考文档 📚
- GoFrame仓库
- GoFrame文档
- vue-next-admin仓库
- gfast仓库地址
- gfast-ui仓库地址
- gfast文档
搭建项目结构 🛠️
项目结构
我们将代码整合到我们项目中,项目结构如下
📂 softHub📁 api📁 hack📁 internal📁 library📁 manifest📁 resource📁 test📁 utility📁 web📁 admin📄 LICENSE📄 README.MD📄 go.mod📄 go.sum📄 main.go
项目结构说明
仅针对初始项目,后续会随着需求略有调整。
api目录
📂 api📁 v1📁 common📄 captcha.go📁 system📄 cache.go
仅做结构示意,存放着api的结构定义,以cache.go
为例
package systemimport ("github.com/gogf/gf/v2/frame/g"commonApi "github.com/tiger1103/gfast/v3/api/v1/common"
)type CacheRemoveReq struct {g.Meta `path:"/cache/remove" tags:"缓存管理" method:"delete" summary:"清除缓存"`commonApi.Author
}type CacheRemoveRes struct {commonApi.EmptyRes
}
hack目录
📂 hack📄 config.yaml
config.yaml
# CLI.
gfcli:gen:dao:- link: "mysql:gfast3:gfast333@tcp(127.0.0.1:3306)/gfast-v32"tables: "sys_role_dept"removePrefix: "gf_"descriptionTag: truenoModelComment: truepath: "./internal/app/system"
用于gfcli生成模板代码
internal目录
📂 internal📁 app📁 cmd📁 consts📁 router
internal
目录用于存放仅限当前项目内部使用的包,外部项目无法导入这些包。例如api定义、service等都在internal中编写
library目录
📂 library📁 libResponse📄 response.go📁 libRouter📄 router.go📁 libUtils📄 slice_tree.go📄 utils.go📁 liberr📄 err.go
library目录存放一下共通的库,例如错误处理err.go
// library\liberr\err.gopackage liberrimport ("context""github.com/gogf/gf/v2/frame/g"
)func ErrIsNil(ctx context.Context, err error, msg ...string) {if !g.IsNil(err) {if len(msg) > 0 {g.Log().Error(ctx, err.Error())panic(msg[0])} else {panic(err.Error())}}
}func ValueIsNil(value interface{}, msg string) {if g.IsNil(value) {panic(msg)}
}
manifest目录
📂 manifest📁 config📄 config.yaml.bak📁 deploy📁 kustomize📁 base📄 deployment.yaml📄 kustomization.yaml📄 service.yaml📁 overlays📁 develop📄 configmap.yaml📄 deployment.yaml📄 kustomization.yaml📁 docker📄 Dockerfile📄 docker.sh
存放一些清单文件、配置信息等
resources目录
📂 resource📁 casbin📄 rbac_model.conf📄 rbac_policy.csv📁 data📄 gfast-v32.sql📁 i18n📁 log📁 run📄 2025-06-14.log📁 server📄 2025-06-14.log📄 access-20250614.log📁 sql📄 2025-06-14.log📁 public📁 html📁 plugin📁 scripts📁 template
存放一些资源、例如数据库初始化脚本、生成的日志
test目录
顾名思义,存放测试代码
utility目录
目前为空
web目录
存放前端代码
创建数据库
创建数据库并导入建表语句resources\data\gfast-v32.sql
修改配置
修改manifest\config\config.yaml.bak
,创建manifest\config\config.yaml
修改数据库配置
# Database.
database:logger:level: "all"stdout: truePath: "resource/log/sql"default:link: "mysql:root:123456@tcp(127.0.0.1:3306)/softhub?charset=utf8mb4&parseTime=true&loc=Local"debug: truecharset: "utf8mb4" #数据库编码dryRun: false #空跑maxIdle: 10 #连接池最大闲置的连接数maxOpen: 10 #连接池最大打开的连接数maxLifetime: "30s" #(单位秒)连接对象可重复使用的时间长度
修改缓存配置
支持三种配置,按需选择
- memory 内存,重启数据丢失
- redis 需要有redis实例
- dist 磁盘缓存,速度慢点,但是本项目够用
gfToken:cacheKey: "softhub:"timeOut: 10800maxRefresh: 5400multiLogin: trueencryptKey: "49c54195e750b04e74a8429b17896586"cacheModel: "redis" #缓存模式 memory OR redis OR distdistPath: "./resource/data/distTokenDb" #使用磁盘缓存时配置数据缓存的目录excludePaths:- "/api/v1/system/login"# Redis 配置示例
redis:# 单实例配置default:address: 192.168.31.93:63790db: 1idleTimeout: "60s" #连接最大空闲时间,使用时间字符串例如30s/1m/1dmaxConnLifetime: "90s" #连接最长存活时间,使用时间字符串例如30s/1m/1dwaitTimeout: "60s" #等待连接池连接的超时时间,使用时间字符串例如30s/1m/1ddialTimeout: "30s" #TCP连接的超时时间,使用时间字符串例如30s/1m/1dreadTimeout: "30s" #TCP的Read操作超时时间,使用时间字符串例如30s/1m/1dwriteTimeout: "30s"
项目启动 🚀
启动后端
go mod tidy
go run main.go
看到如上界面即代表后端服务启动成功
项目已经集成了swagger
- http://127.0.0.1:8808/swagger/
- http://127.0.0.1:8808/api.json
按需进行访问
启动前端
cd web/admin
npm i
npm run dev
看到如上界面即代表前端启动成功
访问首页http://127.0.0.1:8888
账号
- admin
- 123456
softhub系列往期文章
- Softhub软件下载站实战开发(一):项目总览