1 Star 6 Fork 4

o0无忧亦无怖 / gin-restful

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

gin http restful项目生成

快速入门

go get  github.com/go-libraries/gin-restful

//export GO111MODULE=on && export GOMOD={{pwd}}\go.mod && go install createGinProject.go

$GOBIN/gin-restful  -package=项目名(包名) -path=项目路径 -dsn="username:password@tcp(host:port)/database"

生成项目基本结构

-config
  config.ini
-controllers
  http处理回调
-logs
  日志文件目录
-models
  模型文件
-services
  主要对控制器的复杂业务二次封装
-routers
  路由文件
go.mod
main.go
Readme.md

参数详解

Usage of createMangoProject:
  -dsn string
        connection info names dsn
  -h    this help
  -help
        this help
  -package string
        package name use all project
  -path string
        project build in this path
  -port string
        port

二次开发详解

控制器

  1. 可以在services中书写新的控制器
type UserSaveService struct {
	base.Controller
	Account *UserSaveRequest
}
func (u *UserSaveService) Decode() base.IError {
    // 解析 输入字段 如下
	u.Account = &UserSaveRequest{&models.UserAccount{}, ""}
	if bt, err := u.Ctx.GetRawData(); err == nil {
		if err := json.Unmarshal(bt, u.Account); err != nil {
			return base.NewError(err)
		}
	} else {
		return base.NewError(err)
	}

	return nil
}

func (u *UserSaveService) Process() base.IError {
    //todo:执行业务过程
	return nil
}
  1. 可以在controllers中注入执行方法
func SaveUser(c *gin.Context) {
	p := &base.Controller{}
	p.ServiceFun = func(u *base.DefaultService) base.IError {
		u.Data = "hello world"
		return nil
	}
	base.RunService(p, c)
}

路由

路由可以开发二次中间件功能

package routers

import (
	"github.com/gin-gonic/gin"
	"time"
	"{{package}}/base"
)

type Route struct {
	Name        string
	Method      string
	Path        string
	HandlerFunc gin.HandlerFunc
}

func calTime(fn func(c *gin.Context)) func(c *gin.Context) {
	return func(c *gin.Context) {
		start := time.Now()
		fn(c)
		base.Log.Printf("Done in %v (%s %s)\n", time.Since(start), c.Request.Method, c.Request.URL.Path)
	}
}

func init() {

	//Router.GET("/", func(c *gin.Context) {
	//	time.Sleep(5 * time.Second)
	//	c.String(http.StatusOK, "Welcome Gin Server")
	//})

	for _, route := range getUserRoutes() {
		handle := calTime(route.HandlerFunc)
		base.Gin.Handle(route.Method, route.Path, handle)
	}

	//todo: add other Routes
}

模型

默认使用gorm作为数据驱动,如果初始化--dsn项目不为空,会自动将该db下表生成模型并提供基础方法

外部库详见 外部库-模型生成器

文档

文档使用swagger进行配置,可以一键生成

详见

MIT License Copyright (c) 2020 go-libraries Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于 gin 的 restful api 开发框架 展开 收起
Go 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/Mars_Lee/gin-restful.git
git@gitee.com:Mars_Lee/gin-restful.git
Mars_Lee
gin-restful
gin-restful
master

搜索帮助