sqrtcat/easy-gin: 一套基于Gin 框架的MVC 脚手架 - GitHub

文章推薦指數: 80 %
投票人數:10人

Contribute to sqrtcat/easy-gin development by creating an account on GitHub. ... index.html - index/index.html [GIN-debug] GET / --> easy-gin/controllers. Skiptocontent {{message}} sqrtcat / easy-gin Public Notifications Star 67 Fork 19 一套基于Gin框架的MVC脚手架,使用govendor包管理 MITLicense 67 stars 19 forks Star Notifications Code Issues 0 Pullrequests 0 Actions Projects 0 Wiki Security Insights More Code Issues Pullrequests Actions Projects Wiki Security Insights master Branches Tags Couldnotloadbranches Nothingtoshow Loading {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default Loading 1 branch 1 tag Code Loading Latestcommit   Gitstats 9 commits Files Permalink Failedtoloadlatestcommitinformation. Type Name Latestcommitmessage Committime configs     controllers     drivers     models     routes     server     vendor     views/index     .gitignore     LICENSE     README.md     main.go     Viewcode easy-gin 安装步骤 安装govendor包管理工具 拉取源码 使用govendor安装依赖包 服务配置 数据库及连接池配置 启动服务 访问服务 快速体验 导入框架示例Sql 定义控制器 定义模型 定义视图 定义路由Restful 热更新开发模式借用rizla插件 注意 README.md easy-gin 一套基于Gin框架的MVC脚手架 封装了GinWeb服务配置、路由配置、数据库/连接池配置、视图配置,方便快速体验及构建GoWeb工程 自带一套用于体验及演示的RestfulApi代码示例 安装步骤 安装govendor包管理工具 #goget-u-vgithub.com/kardianos/govendor 拉取源码 #cd$GOPATH/src&&[email protected]:sqrtcat/easy-gin.git&&cdeasy-gin 使用govendor安装依赖包 #govendorsync 服务配置 packageconfigs //服务配置防止变量污染故用函数组织 funcGetServerConfig()(serverConfigmap[string]string){ serverConfig=make(map[string]string) serverConfig["HOST"]="0.0.0.0"//监听地址 serverConfig["PORT"]="8080"//监听端口 serverConfig["VIEWS_PATTERN"]="easy-gin/views/*/*"//视图模板路径pattern serverConfig["ENV"]="debug"//环境模式release/debug/test return } 数据库及连接池配置 因框架启动时会创建连接池服务,故需配置好数据库后运行 packageconfigs //数据库配置 funcGetDbConfig()map[string]string{ //初始化数据库配置map dbConfig:=make(map[string]string) dbConfig["DB_HOST"]="127.0.0.1"//主机 dbConfig["DB_PORT"]="3306"//端口 dbConfig["DB_NAME"]="golang"//数据库 dbConfig["DB_USER"]="root"//用户名 dbConfig["DB_PWD"]=""//密码 dbConfig["DB_CHARSET"]="utf8" dbConfig["DB_MAX_OPEN_CONNS"]="20"//连接池最大连接数 dbConfig["DB_MAX_IDLE_CONNS"]="10"//连接池最大空闲数 dbConfig["DB_MAX_LIFETIME_CONNS"]="7200"//连接池链接最长生命周期 returndbConfig } 启动服务 #gorunmain.go [GIN-debug][WARNING]CreatinganEngineinstancewiththeLoggerandRecoverymiddlewarealreadyattached. [GIN-debug][WARNING]Runningin"debug"mode.Switchto"release"modeinproduction. -usingenv:exportGIN_MODE=release -usingcode:gin.SetMode(gin.ReleaseMode) [GIN-debug]LoadedHTMLTemplates(3): - -index.html -index/index.html [GIN-debug]GET/-->easy-gin/controllers.IndexHome(3handlers) [GIN-debug]GET/index-->easy-gin/controllers.IndexHome(3handlers) [GIN-debug]GET/users/:id-->easy-gin/controllers.UserGet(3handlers) [GIN-debug]GET/users-->easy-gin/controllers.UserGetList(3handlers) [GIN-debug]POST/users-->easy-gin/controllers.UserPost(3handlers) [GIN-debug]PUT/users/:id-->easy-gin/controllers.UserPut(3handlers) [GIN-debug]PATCH/users/:id-->easy-gin/controllers.UserPut(3handlers) [GIN-debug]DELETE/users/:id-->easy-gin/controllers.UserDelete(3handlers) [GIN-debug]ListeningandservingHTTPon0.0.0.0:8080 访问服务 http://yourhost:8080/ 快速体验 导入框架示例Sql CREATEDATABASE`golang`DEFAULTCHARSETuft8mb4DEFAULTutf8mb4_general_ci; USE`golang`; CREATETABLE`users`( `id`int(10)unsignedNOTNULLAUTO_INCREMENT, `name`varchar(25)NOTNULL, `age`tinyintunsignedNOTNULLDEFAULT0, PRIMARYKEY(`id`) )ENGINE=InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=utf8; 定义控制器 controllers controllers/IndexController.go packagecontrollers import( "net/http" "github.com/gin-gonic/gin" ) funcIndexHome(ctx*gin.Context){ ////querystring //queryVal1:=ctx.Query("val1") //queryVal2:=ctx.DefaultQuery("val2","val2_default") // ////postformdata //formVal3:=ctx.PostForm("val3") //formVal4:=ctx.DefaultPostForm("val4","val4_default") // ////pathinfo //pathVal5:=ctx.Param("val5") //ctx.String(http.StatusOK,"hello%s%s%s%s%s",queryVal1,queryVal2,formVal3,formVal4,pathVal5) ctx.HTML(http.StatusOK,"index/index.html",gin.H{ "msg":"easygin", }) } 定义模型 models models/User.go packagemodels import( "log" ) typeUserstruct{ Model Idint`json:"id"form:"id"primaryKey:"true"` Namestring`json:"name"form:"name"binding:"required"` Ageint`json:"age"form:"age"binding:"required"` } //getone func(model*User)UserGet(idint)(userUser,errerror){ .... } //getlist func(model*User)UserGetList(pageint,pageSizeint)(users[]User,errerror){ .... } //create func(model*User)UserAdd()(idint64,errerror){ .... } //update func(model*User)UserUpdate(idint)(afrint64,errerror){ .... } //delete func(model*User)UserDelete(idint)(afrint64,errerror){ .... } 定义视图 views views/index views/index/index.html 定义路由Restful routes routes/router.go packageroutes import( "easy-gin/controllers" "github.com/gin-gonic/gin" ) funcRegisterRoutes(router*gin.Engine){ router.GET("/",controllers.IndexHome) router.GET("/index",controllers.IndexHome) router.GET("/users/:id",controllers.UserGet) router.GET("/users",controllers.UserGetList) router.POST("/users",controllers.UserPost) router.PUT("/users/:id",controllers.UserPut) router.PATCH("/users/:id",controllers.UserPut) router.DELETE("/users/:id",controllers.UserDelete) } 热更新开发模式借用rizla插件 #goget-ugithub.com/kataras/rizla #rizlamain.go 注意 因golang的包载入机制问题,项目名如需改为其他,需修改框架内的部分包的载入路径easy-gintoyour-projectName main.go server/server.go routes/router.go server/server.go models/* go1.11后大家可以改用gomod模式 About 一套基于Gin框架的MVC脚手架,使用govendor包管理 Topics golang mvc gin restful-api autoreload database-pool Resources Readme License MITLicense Releases 1 tags Packages0 Nopackagespublished Languages Go 94.9% HTML 5.1% Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?