All Projects → sqrtcat → easy-gin

sqrtcat / easy-gin

Licence: MIT license
一套基于 Gin 框架的 MVC 脚手架,使用 govendor 包管理

Programming Languages

go
31211 projects - #10 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to easy-gin

djburger
Framework for safe and maintainable web-projects.
Stars: ✭ 75 (+1.35%)
Mutual labels:  mvc, restful-api
Ppgo api demo gin
API接口应用Demo 基于Gin
Stars: ✭ 90 (+21.62%)
Mutual labels:  gin, restful-api
golang api
This is a source code of my golang restful api series on youtube.
Stars: ✭ 36 (-51.35%)
Mutual labels:  gin, restful-api
Desenvolvimento-Android-do-absoluto-zero-para-iniciantes
Visite meu site e conheça todos os meus cursos 100% on-line.
Stars: ✭ 33 (-55.41%)
Mutual labels:  mvc, restful-api
Proteus
Lean, mean, and incredibly fast JVM framework for web and microservice development.
Stars: ✭ 178 (+140.54%)
Mutual labels:  mvc, restful-api
Hunt Framework
A Web framework for D Programming Language. Full-stack high-performance.
Stars: ✭ 256 (+245.95%)
Mutual labels:  mvc, restful-api
relax
Relax is a set of tools for modeling, documenting and testing RESTFul APIs
Stars: ✭ 12 (-83.78%)
Mutual labels:  mvc, restful-api
gonrails
Rails like mvc backend application with golang .
Stars: ✭ 37 (-50%)
Mutual labels:  mvc, gin
gin-api-boilerplate
A Go RESTful API server with gin and docker
Stars: ✭ 16 (-78.38%)
Mutual labels:  gin, restful-api
vsphere-automation-sdk-.net
[DEPRECATED] Please see README. C# samples, language bindings, and API reference documentation for vSphere, VMC, and NSX-T using the VMware REST API
Stars: ✭ 67 (-9.46%)
Mutual labels:  restful-api
DNTCommon.Web.Core
DNTCommon.Web.Core provides common scenarios' solutions for ASP.NET Core 3.x applications.
Stars: ✭ 117 (+58.11%)
Mutual labels:  mvc
hornbill
基于uri的 node mvc框架,适用于前后端分离服务
Stars: ✭ 22 (-70.27%)
Mutual labels:  mvc
DEEPaaS
A REST API to serve machine learning and deep learning models
Stars: ✭ 26 (-64.86%)
Mutual labels:  restful-api
ExtApp
ExtApp是一个基于三层架构,使用NHibernate、API Controller和ExtJs创建的,用于简化政府和企业应用开发的Web应用程序框架。
Stars: ✭ 14 (-81.08%)
Mutual labels:  mvc
lumen-api-starter
A starter project to develop API with Lumen 8.*
Stars: ✭ 42 (-43.24%)
Mutual labels:  restful-api
seal
django-base-templates 主要为 django 开发DEMO, 支持 非前后端分离 和 前后端分离模式 。
Stars: ✭ 118 (+59.46%)
Mutual labels:  restful-api
framework
A PHP framework for rapidly building web applications.
Stars: ✭ 48 (-35.14%)
Mutual labels:  mvc
express-routes-versioning
Node.js module provides versioning for expressjs routes/api
Stars: ✭ 57 (-22.97%)
Mutual labels:  restful-api
estore
Electronic Store Application - A web based application developed using PHP and Driven by MySQL Database
Stars: ✭ 48 (-35.14%)
Mutual labels:  mvc
app container
a little little framework 一个简单的SPA框架, 按照自己对SPA的交互理解制作~
Stars: ✭ 19 (-74.32%)
Mutual labels:  mvc

easy-gin

一套基于 Gin 框架的 MVC 脚手架

  • 封装了 Gin Web 服务配置、路由配置、数据库/连接池配置、视图配置,方便快速体验及构建 Go Web 工程
  • 自带一套用于体验及演示的 Restful Api 代码示例

安装步骤

安装govendor包管理工具

# go get -u -v github.com/kardianos/govendor

拉取源码

# cd $GOPATH/src && git clone [email protected]:sqrtcat/easy-gin.git && cd easy-gin

使用 govendor 安装依赖包

# govendor sync

服务配置

package configs

// 服务配置 防止变量污染故用函数组织
func GetServerConfig() (serverConfig map[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
}

数据库及连接池配置

因框架启动时会创建连接池服务,故需配置好数据库后运行

package configs

// 数据库配置
func GetDbConfig() 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" // 连接池链接最长生命周期

	return dbConfig
}

启动服务

# go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] Loaded HTML Templates (3):
        -
        - index.html
        - index/index.html

[GIN-debug] GET    /                         --> easy-gin/controllers.IndexHome (3 handlers)
[GIN-debug] GET    /index                    --> easy-gin/controllers.IndexHome (3 handlers)
[GIN-debug] GET    /users/:id                --> easy-gin/controllers.UserGet (3 handlers)
[GIN-debug] GET    /users                    --> easy-gin/controllers.UserGetList (3 handlers)
[GIN-debug] POST   /users                    --> easy-gin/controllers.UserPost (3 handlers)
[GIN-debug] PUT    /users/:id                --> easy-gin/controllers.UserPut (3 handlers)
[GIN-debug] PATCH  /users/:id                --> easy-gin/controllers.UserPut (3 handlers)
[GIN-debug] DELETE /users/:id                --> easy-gin/controllers.UserDelete (3 handlers)
[GIN-debug] Listening and serving HTTP on 0.0.0.0:8080

访问服务

http://yourhost:8080/

快速体验

导入框架示例 Sql

CREATE DATABASE `golang` DEFAULT CHARSET uft8mb4 DEFAULT utf8mb4_general_ci;
USE `golang`;
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  `age` tinyint unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

定义控制器

controllers
controllers/IndexController.go

package controllers

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func IndexHome(ctx *gin.Context) {

	//// query string
	//queryVal1 := ctx.Query("val1")
	//queryVal2 := ctx.DefaultQuery("val2", "val2_default")
	//
	//// post form data
	//formVal3 := ctx.PostForm("val3")
	//formVal4 := ctx.DefaultPostForm("val4", "val4_default")
	//
	//// path info
	//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": "easy gin",
	})
}

定义模型

models
models/User.go

package models

import (
	"log"
)

type User struct {
	Model
	Id   int    `json:"id" form:"id" primaryKey:"true"`
	Name string `json:"name" form:"name" binding:"required"`
	Age  int    `json:"age" form:"age" binding:"required"`
}

// get one
func (model *User) UserGet(id int) (user User, err error) {
  ....
}

// get list
func (model *User) UserGetList(page int, pageSize int) (users []User, err error) {
	....
}

// create
func (model *User) UserAdd() (id int64, err error) {
	....
}

// update
func (model *User) UserUpdate(id int) (afr int64, err error) {
	....
}

// delete
func (model *User) UserDelete(id int) (afr int64, err error) {
	....
}

定义视图

views
views/index
views/index/index.html

定义路由 Restful

routes
routes/router.go

package routes

import (
	"easy-gin/controllers"
	"github.com/gin-gonic/gin"
)

func RegisterRoutes(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 插件

# go get -u github.com/kataras/rizla
# rizla main.go

注意

因 golang 的包载入机制问题,项目名如需改为其他,需修改框架内的部分包的载入路径 easy-gin to your-projectName

main.go
server/server.go
routes/router.go
server/server.go
models/*

go 1.11 后大家可以改用 gomod 模式

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].