# vfaith **Repository Path**: git_public/vfaith ## Basic Information - **Project Name**: vfaith - **Description**: 一个适合Go初学者的轻量级CRUD框架 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-09-25 - **Last Updated**: 2024-11-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 框架使用步骤 - cmd初始化项目 1. 新建文件夹:ProjectName ``` md {{ProjectName}} cd {{ProjectName}} ``` 2. 初始化项目:go mod init projectname ``` go mod init {{ProjectName}} ``` 3. 安装框架: ``` go get gitee.com/git_public/vfaith@master ``` 4. 新建程序入口:创建【main.go】文件 ``` package main import ( "embed" "gitee.com/git_public/vfaith/assemble" ) //go:embed public/* var httpRegister embed.FS // @title MyApp项目 // @version 1.0 // @description MyApp Go Project. // @host localhost:8080 // @BasePath / func main() { // 初始化项目服务 assemble.InitServer(&httpRegister) } ``` 5. 在目录【manifest/config】下创建配置文件【env_dev.yaml】 ``` md manifest\config ``` ``` DB: DB_USERNAME: DB_PASSWORD: DB_HOST: DB_PORT: DB_NAME: ``` 6. 执行mod整理命令: ``` go mod tidy ``` 7. 启动项目: ``` go run main.go ``` ### 引入Swagger 1. 生成swag文档: ``` swag init --parseDependency ``` 2. 在main.go文件中引入swag ``` _ "gitee.com/git_public/vfaith/toolkit/swag/action" //引入Swag模块 _ "xxx/docs" //引入Swag本地路径 ``` 3. 自动引入相关包,执行命令 ``` go mod tidy ``` 4. 访问swag文档【二选一】 ``` http://localhost:8080/swagger/index.html ``` ``` http://localhost:8080/knife/doc.html ``` ### 引入登录模块 1. 创建资源文件夹 ``` md resources\static ``` 2. 创建路由配置文件夹 ``` md manifest\router ``` 3. 创建路由配置文件 ``` dashboard_router.go ``` 4. 在main.go文件中引入路由模块 ``` _ "{{ProjectName}}/manifest/router" //引入路由配置模块 ``` 5. 引入路由配置内容 ``` package router import ( "log" "net/http" "os" ) func init() { mux := http.NewServeMux() mux.Handle("/dashboard/", http.StripPrefix("/dashboard/", http.FileServer(http.Dir("resources/static/dashboard")))) // 设置自定义路由器 http.Handle("/dashboard/", &DashboardRouter{mux: mux}) } type DashboardRouter struct { mux *http.ServeMux } func (cm *DashboardRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Printf("muxHandle:【%s】", r.URL.Path) // 获取请求的路径 path := r.URL.Path // 如果请求的是根目录,使用 indexHandler if path == "/" { // 这里应该给一个默认的服务启动页面 http.Redirect(w, r, "/dashboard/index", http.StatusMovedPermanently) } // 检查路径是否是一个目录 if stat, err := os.Stat("./static" + path); err == nil && stat.IsDir() { // 如果是目录,返回 403 Forbidden //http.Error(w, "403 Forbidden.文件目录不允许访问", http.StatusForbidden) //return } // 处理其他路径 cm.mux.ServeHTTP(w, r) } ``` 6. 引入登录api接口 https://gitee.com/git_public/vfaith/wikis/Login%E9%A1%B5%E9%9D%A2 7. 自定义登陆页面并访问 ### 引入代码生成 1. 在main.go文件中引入generate ``` _ "gitee.com/git_public/vfaith/toolkit/generate/action" //引入代码自动生成模块 ``` 2. 代码自动生成: 手动输入 ``` http://localhost:8080/gen/form ``` URL赋值: ``` http://localhost:8080/gen/form?genBasePath=&pageTitle=&moduleName=mortal&fileName=&className=&tableName= ``` ### 生成代码 1. 访问代码生成页面,输入相关参数,生成代码 2. 初次生成的模块,需要在main.go中做引入 ``` _ "{{ProjectName}}/public/dashboard/api/{{ModuleName}}/controller" //引入mortal模块 ``` xxx替换为代码生成时的moduleName ### 其他参考 1. 如果swag报错: ``` # myappt/docs docs\docs.go:30:2: unknown field LeftDelim in struct literal of type "github.com/swaggo/swag".Spec docs\docs.go:31:2: unknown field RightDelim in struct literal of type "github.com/swaggo/swag".Spec ``` 执行swag更新命令: ``` go get -u github.com/swaggo/swag ``` 2. 框架更新命令 ``` go get -u gitee.com/git_public/vfaith@master ``` 如果无反应,可以先执行一下【go mod tidy】命令 ### 自定义修改框架 1. 将代码clone到本地 2. 在go.mod文件中,引入待开发项目 ``` require gitee.com/git_public/vfaith v0.0.0 ``` 3. 在go.mod文件中,使用replace将源码引入待开发项目 ``` replace gitee.com/git_public/vfaith => ../vfaith ```