Skip to main content

29 posts tagged with "golang"

View All Tags

· 7 min read
package main

import (
"net/http"

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

func CookieTool() gin.HandlerFunc {
return func(c *gin.Context) {
// Get cookie
if cookie, err := c.Cookie("label"); err == nil {
if cookie == "ok" {
c.Next()
return
}
}

// Cookie verification failed
c.JSON(http.StatusForbidden, gin.H{"error": "Forbidden with no cookie"})
c.Abort()
}
}

func main() {
route := gin.Default()

route.GET("/login", func(c *gin.Context) {
// Set cookie {"label": "ok" }, maxAge 30 seconds.
//func (c *Context) SetCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool
c.SetCookie("label", "ok", 30, "/", "localhost", false, true)
c.String(200, "Login success!")
})

route.GET("/home", CookieTool(), func(c *gin.Context) {
c.JSON(200, gin.H{"data": "Your home page"})
})

route.Run(":8080")
}

需要注意的是Cookie里的domain是严格匹配的:so 127.0.0.1 不同于 localhost, 如果domain设置的是localhost,那么你用127.0.0.1:8080/admin访问仍是Forbidden with no cookie

custom-validation

package main

import (
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)

// Booking contains binded and validated data.
type Booking struct {
CheckIn time.Time `form:"check_in" json:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
CheckOut time.Time `form:"check_out" json:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"`
}

//define tag
var bookableDate validator.Func = func(fl validator.FieldLevel) bool {
date, ok := fl.Field().Interface().(time.Time)
if ok {
today := time.Now()
if today.After(date) {
return false
}
}
return true
}

func main() {
route := gin.Default()

//register
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("bookabledate", bookableDate)
}

route.POST("/bookable", getBookable)
route.Run(":8080")
}

func getBookable(c *gin.Context) {
var b Booking
if err := c.ShouldBind(&b); err == nil {
c.JSON(http.StatusOK, gin.H{"message": "Booking dates are valid!"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}

这时你可能会一脸懵逼,time解析失败 -- 原因是我们需要传入UTC time

favicon

package main

import (
"net/http"

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

func main() {
app := gin.Default()

// serve static favicon file from a location relative to main.go directory
//app.StaticFile("/favicon.ico", "./.assets/favicon.ico")
app.StaticFile("/favicon.ico", "./favicon.ico")

app.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "Hello favicon.")
})

app.Run(":8080")
}

file binding

package main

import (
"fmt"
"mime/multipart"
"net/http"
"path/filepath"

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

type BindFile struct {
Name string `form:"name" binding:"required"`
Email string `form:"email" binding:"required"`
File *multipart.FileHeader `form:"file" binding:"required"`
}

func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.Static("/", "./public")
router.POST("/upload", func(c *gin.Context) {
var bindFile BindFile

// Bind file
if err := c.ShouldBind(&bindFile); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("err: %s", err.Error()))
return
}

// Save uploaded file
file := bindFile.File
fmt.Println("file name:", file.Filename)
fmt.Println("file size:", file.Size) //kb
dst := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, dst); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}

c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, bindFile.Name, bindFile.Email))
})
router.Run(":8080")
}

将index.html放入public目录

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>File binding</title>
</head>
<body>
<h1>Bind file with fields</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
File: <input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
</body>

然后浏览器访问 http://127.0.0.1:8080/

submit之后:

可以看到 pdf已成功上传

再来看看文件大小对不对?

OK

我们再用postman测试一下

都是没问题的

graceful shutdown

a, notify with context

// build go1.16

package main

import (
"context"
"log"
"net/http"
"os/signal"
"syscall"
"time"

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

func main() {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

router := gin.Default()
router.GET("/", func(c *gin.Context) {
time.Sleep(10 * time.Second)
c.String(http.StatusOK, "Welcome Gin Server")
})

srv := &http.Server{
Addr: ":8080",
Handler: router,
}

// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

// Listen for the interrupt signal.
<-ctx.Done()

// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
log.Println("shutting down gracefully, press Ctrl+C again to force")

// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err)
}

log.Println("Server exiting")
}

b, notify without context

//go:build go1.8
// +build go1.8

package main

import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

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

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

srv := &http.Server{
Addr: ":8080",
Handler: router,
}

// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")

// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err)
}

log.Println("Server exiting")
}

group routes

package routes

import (
"github.com/gin-gonic/gin"
"net/http"
)

var router = gin.Default()

// Run will start the server
func Run() {
getRoutes()
router.Run(":5000")
}

// getRoutes will create our routes of our entire application
// this way every group of routes can be defined in their own file
// so this one won't be so messy
func getRoutes() {
v1 := router.Group("/v1")
addUserRoutes(v1)
addPingRoutes(v1)

v2 := router.Group("/v2")
addPingRoutes(v2)
}

func addUserRoutes(rg *gin.RouterGroup) {
users := rg.Group("/users")

users.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, "users")
})
users.GET("/comments", func(c *gin.Context) {
c.JSON(http.StatusOK, "users comments")
})
users.GET("/pictures", func(c *gin.Context) {
c.JSON(http.StatusOK, "users pictures")
})
}

func addPingRoutes(rg *gin.RouterGroup) {
ping := rg.Group("/ping")

ping.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, "pong")
})
}

mutiple service

package main

import (
"log"
"net/http"
"time"

"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
)

var g errgroup.Group

func router01() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.GET("/", func(c *gin.Context) {
c.JSON(
http.StatusOK,
gin.H{
"code": http.StatusOK,
"error": "Welcome server 01",
},
)
})

return e
}

func router02() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.GET("/", func(c *gin.Context) {
c.JSON(
http.StatusOK,
gin.H{
"code": http.StatusOK,
"error": "Welcome server 02",
},
)
})

return e
}

func main() {
server01 := &http.Server{
Addr: ":8080",
Handler: router01(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}

server02 := &http.Server{
Addr: ":8081",
Handler: router02(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}

g.Go(func() error {
return server01.ListenAndServe()
})

g.Go(func() error {
return server02.ListenAndServe()
})

if err := g.Wait(); err != nil {
log.Fatal(err)
}
}

· 3 min read

简介

viper是go语言配置文件的一个管理库,github star 23.2K

viper有以下特性:

  • 设置默认值
  • 从json, toml,yaml,envfile 和 java properties格式的配置文件读取配置信息
  • 实时监控和重新读区配置文件
  • 从远程配置系统(etcd或consul)读取并监控配置变化
  • 从命令行参数读取配置
  • 从buffer读区配置
  • 设置显式值

get started

下面来看看viper的简单用法, 以config.yml为例

#install
go get github.com/spf13/viper

目录结构:

package main

import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"log"
"os"
"path"
)

type Config struct {
Use string `mapstructure:"use"`
Cfg []Server `mapstructure:"cfg"`
}

type Server struct {
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Likes []string `mapstructure:"likes"`
}

func main() {
pth := path.Join(os.Getenv("GOPATH"), "src/github.com/scott-x/test")

// name of config file (without extension)
viper.SetConfigName("config")
// REQUIRED if the config file does not have the extension in the name
viper.SetConfigType("yaml")
// optionally look for config in the working directory
viper.AddConfigPath(".")
// path to look for the config file in
viper.AddConfigPath(pth)

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}

var config Config
if err = viper.Unmarshal(&config); err != nil {
panic("Unmarshal failed:" + err.Error())
}
log.Println(config)

go func() {
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
viper.WatchConfig()
}()
}

运行:

在不修改源码的情况下实现线上线下环境分离

思路:

  • 1,线下创建一个环境变量,如果能够读到就为debug模式,否则为生产模式
  • 2,利用flag 命令行指定环境 eg: xx -env debug

下面展示环境变量的用法,eg:

export TEST_ENV=1

代码结构:

package main

import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"log"
"os"
"path"
)

type Config struct {
Mysql `mapstructure:"mysql"`
}

type Mysql struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"prot"`
Username string `mapstructure:"username"`
}

func main() {
pth := path.Join(os.Getenv("GOPATH"), "src/github.com/scott-x/test/env")
var configFileName, env string
if myenv := os.Getenv("TEST_ENV"); len(myenv) > 0 {
env = "debug"
} else {
env = "pro"
}
configFileName = fmt.Sprintf("config-%s.yml", env)
log.Println("configFileName:", configFileName)

// name of config file (without extension)
viper.SetConfigName(configFileName)
// REQUIRED if the config file does not have the extension in the name
viper.SetConfigType("yaml")
// optionally look for config in the working directory
viper.AddConfigPath(".")
// path to look for the config file in
viper.AddConfigPath(pth)

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}

var config Config
if err = viper.Unmarshal(&config); err != nil {
panic("Unmarshal failed:" + err.Error())
}
log.Println(config)

go func() {
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
viper.WatchConfig()
}()
}

不设置环境变量运行:

设置环境变量后运行:

· 24 min read

gin目前已有69.3K star

一个简单的http demo

package main

import (
"github.com/gin-gonic/gin"
"log"
)

func main() {
//gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.GET("/", index)
log.Println("server is running at http://127.0.0.1:3333")
router.Run(":3333")
}

func index(c *gin.Context) {
//db operation
c.JSON(200, gin.H{
"code": "2000",
"msg": "ok",
"likes": []string{"apple", "pear", "orange"},
})
}

gin重要struct

想要了解gin,必须掌握 gin.Context

type Context struct {
Request *http.Request
Writer ResponseWriter

Params Params

// Keys is a key/value pair exclusively for the context of each request.
Keys map[string]interface{}

// Errors is a list of errors attached to all the
// handlers/middlewares who used this context.
Errors errorMsgs

// Accepted defines a list of manually accepted formats
// for content negotiation.
Accepted []string
// contains filtered or unexported fields
}

type ResponseWriter interface {
http.ResponseWriter
http.Hijacker
http.Flusher
http.CloseNotifier

// Status returns the HTTP response status code of the current request.
Status() int

// Size returns the number of bytes
// already written into the response http body.
// See Written()
Size() int

// WriteString writes the string into the response body.
WriteString(string) (int, error)

// Written returns true if the response body was already written.
Written() bool

// WriteHeaderNow forces to write the http header (status code + headers).
WriteHeaderNow()

// Pusher get the http.Pusher for server push
Pusher() http.Pusher
}

net/http: regarding Cookie

http.Request

type Cookie struct {
Name string //eg:"x-my-token"
Value string //jwt token

Path string // optional eg:"/"
Domain string // optional eg:"localhost"
Expires time.Time // optional
RawExpires string // for reading cookies only

// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int
Secure bool //需要设置成false 否则postman拿不到预期的response
HttpOnly bool //需设置为true
SameSite SameSite
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
func SetCookie(c *gin.Context) {
cookie := http.Cookie{
Name: "x-token",
Value: "jwt_token_value",
Path: "/",
Domain: "localhost",
Expires: time.Now().Add(time.Minute * time.Duration(5)), //5 min 后过期
RawExpires: "",
MaxAge: 0,
Secure: false, //如果这里设置为true 则postman永远拿不到cookie
HttpOnly: true,
SameSite: 0,
Raw: "",
Unparsed: nil,
}
http.SetCookie(c.Writer, &cookie)
c.JSON(200, gin.H{
"code": 2000,
"msg": "success",
})
}

获取参数

package main

import (
"github.com/gin-gonic/gin"
"log"
)

func main() {
route := gin.Default()

apiGroup := route.Group("/api")
{
//http://127.0.0.1:9999/api/test1
apiGroup.GET("/test1", Test1)
//http://127.0.0.1:9999/api/test2
apiGroup.GET("/test2", Test2)
//http://127.0.0.1:9999/api/test3
apiGroup.GET("/test3/:name/:company", Test3)
}

log.Fatal(route.Run(":9999"))
}

func Test1(c *gin.Context) {
//get username & password
u := struct {
Username string `json:"username"`
Password string `json:"password"`
}{}
err := c.ShouldBindJSON(&u)
if err != nil {
c.JSONP(200, gin.H{
"code": 2001,
"msg": "bad request",
})
return
}

c.JSONP(200, gin.H{
"code": 2000,
"msg": "success",
"data": u,
})
}

func Test2(c *gin.Context) {
//func (c *Context) Query(key string) (value string
name := c.Query("name")
age := c.Query("age")
//建议使用DefaultQuery() 如果用户没输入还可设定默认值
//注意Query()返回值都是string
//name := C.DefaultQuery("name","Scott")
//age := C.DefaultQuery("age","18")
c.JSON(200, gin.H{
"code": 2000,
"name": name,
"age": age,
})
}

func Test3(c *gin.Context) {
//func (c *Context) Param(key string) string
name := c.Param("name")
company := c.Param("company")
c.JSON(200, gin.H{
"code": 2000,
"name": name,
"company": company,
})
}

如果前端是通过json传递数据的 那么需要调用 c.ShouldBindJSON()

如果是通过papram url传递 则调用c.Query()

如果是通过变量的方式传递 则调用c.Param()

middleware

gin的middleware很简单:定义一个function 返回 gin.HandlerFunc 即可

package main

import (
"github.com/gin-gonic/gin"
"log"
"net/http"
)

func main() {
route := gin.Default()

apiGroup := route.Group("/api")
apiGroup.Use(MiddlewareDemo())
{
apiGroup.GET("/test1", Test1) //http://127.0.0.1:9999/api/test1
apiGroup.GET("/test2", Test2) //http://127.0.0.1:9999/api/test2
}

log.Fatal(route.Run(":9999"))
}

func Test1(c *gin.Context) {
c.JSONP(http.StatusOK, gin.H{
"code": 2000,
"msg": "success",
"data": "test1",
})
}

func Test2(c *gin.Context) {
c.JSONP(http.StatusOK, gin.H{
"code": 2000,
"msg": "success",
"data": "test2",
})
}

// gin的middleware很简单:定义一个function 返回 gin.HandlerFunc 即可
func MiddlewareDemo() gin.HandlerFunc {
return func(c *gin.Context) {
log.Println("middleware called")
//拦截用c.Abort()
c.Next()
}
}

middleware执行顺序:

package main

import (
"fmt"
"github.com/gin-gonic/gin"
)

func main() {
router := gin.Default()

router.Use(globalMiddleware())

router.GET("/rest/n/api/*some", mid1(), mid2(), handler)

router.Run()
}

func globalMiddleware() gin.HandlerFunc {
fmt.Println("globalMiddleware")
//return前可以初始化 按注册顺序 只会执行一次
return func(c *gin.Context) {
fmt.Println("1")
c.Next()
fmt.Println("2")
}
}

func handler(c *gin.Context) {
fmt.Println("exec handler.")
}

func mid1() gin.HandlerFunc {
fmt.Println("mid1")
//return前可以初始化 按注册顺序 只会执行一次
return func(c *gin.Context) {

fmt.Println("3")
c.Next()
fmt.Println("4")
}
}

func mid2() gin.HandlerFunc {
fmt.Println("mid2")
//return前可以初始化 按注册顺序 只会执行一次
return func(c *gin.Context) {
fmt.Println("5")
c.Next()
fmt.Println("6")
}
}

启动服务:

请求:http://127.0.0.1:8080/rest/n/api/1

再次请求:http://127.0.0.1:8080/rest/n/api/1

画图表示:

参考官网: How to build one effective middleware?

常见middleware:

更多用法

Abort

package main

import (
"github.com/gin-gonic/gin"
"log"
"net/http"
)

func main() {
route := gin.Default()

apiGroup := route.Group("/api").Use(AuthMiddleware())
{
apiGroup.GET("/test", Test)
}

log.Fatal(route.Run(":9999"))
}

func Test(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{
"code": 2000,
"msg": "test",
})
}
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
isLogin := false
if isLogin {
c.Next()
} else {
c.JSON(http.StatusUnauthorized, gin.H{
"code": 401,
"msg": "未登陆",
})
c.Abort() //Abort阻止了后续逻辑的执行 如果换成return是不行的 ,return并不能hold住后面的逻辑
}
}
}

Abort的原理是让Engine的index越界

render html

gin没有自己去实现一套模版的语法,它用的go语言标准库的一套模板html/template

web框架从请求到返回的全过程

  • router.LoadHTMLFiles(file string)
  • router.LoadHTMLGlob("templates/*")
  • router.LoadHTMLGlob("templates/**/*")

以上与文件相关的的API不能同时出现,只能选一个, 完整demo

exit gracefully

package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"os"
"os/signal"
"syscall"
)

func main() {
//优雅退出: 当我们关闭程序的时候应该做的后续处理,
//比如我们做爬虫的时候,数据爬下来了,这时一个ctrl+c/kill程序关闭了 数据没有处理
//微服务:启动之前 或者启动之后做一件事: 将当前的服务的ip地址和端口号注册到注册中心
//我们当前的服务停止了以后并没有告知注册中心,这时如果前端正好请求了这个ip的服务 就会报错

router := gin.Default()

router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"msg": "pong",
})
})

//主协程退出 子协程也跟着退出
go func() {
router.Run(":8088")
}()

// 如果想要接收信号,kill可以,但kill -9 强杀命令 golang是没机会收到信号的
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) //处理ctrl+c 和 kill
<-quit

//处理后续的逻辑
fmt.Println("关闭server中。。。")
fmt.Println("注销服务。。。")
}

表单验证

表单验证是web框架的一项重要功能

在gin中,若要将请求主体绑定到结构体中,需使用模型绑定,目前支持JSON, XML,YAML和标准表单值(foo=bar&boo=baz)的绑定

gin使用的是第三方库go-playground/validator来验证参数, GitHub Start 13.3K, documentation

使用方法:需要在绑定的字段上设置tag,比如,绑定格式为json,需这样设置json:"fieldname"

//postman/YAPI 
//如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
//否则只能通过raw/JSON的方式发送
type LoginForm struct {
//多个条件用逗号隔开 不用加空格
User string `form:"user" json:"user" binding:"required,min=3,max=5"`
Password string `form:"user" json:"password" binding:"required"`
}

此外,gin还提供了2套绑定方法

  • Must Bind
    • Methods: Bind, BindJSON, BindXML, BindQuery, BindYAML
    • Behavior: 这些方法底层使用MustBindWith, 如果存在绑定错误,请求将被以下指令中止c.AbordWithError(400,err).SetType(ErrorTypeBind), 响应状态码会被设置为400,请求头Content-Type被设置为text/plain;charset=utf-8. 注意如果你试图在此之后设置响应代码,将会发出[Gin-debug] [Warning] Headers were already written. Wanted to override status code 400 with 422, 如果你希望更好的控制行为,请使用ShouldBind的相关方法
  • Should bind - 实际开发中我们只要留意ShouldBind, ShouldBindJSON这2个方法就好了
    • Methods: ShouldBind, ShouldBindJSON, ShouldBindXML, ShouldBindQuery, ShouldBindYAML
    • Behavior: 这些方法底层使用ShouldBindWith,如果存在绑定错误,则返回错误,开发人员可以正确的处理请求和错误

当我们使用绑定方法时,gin会根据Content-Type推断出使用哪种绑定器,如果你确定你绑定的是什么,可以使用MustBindWith或者BindingWith

你还可以给字段指定特定规则的修饰符,如果一个字段用binding:"required"修饰,并且在绑定改字段的值为空,那么将会返回一个错误

package main

import (
"github.com/gin-gonic/gin"
"log"
"net/http"
)

// postman/YAPI
// 如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
// 否则只能通过raw/JSON的方式发送,一般我们配置json的tag就可以了
type LoginForm struct {
//多个条件用逗号隔开 不用加空格
User string `form:"user" json:"user" binding:"required,min=3,max=5"`
Password string `form:"password" json:"password" binding:"required"`
}

type SignUpParam struct {
Name string `json:"name" binding:"required"`
Age uint8 `json:"age" binding:"gte=1,lte=18"`
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
RePassword string `json:"re_password" binding:"required,eqfield=Password"`
}

func main() {
router := gin.Default()
router.POST("/loginJSON", func(c *gin.Context) {
var loginForm LoginForm
if err := c.ShouldBind(&loginForm); err != nil {
log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"user": loginForm.User,
"password": loginForm.Password,
"msg": "登录成功",
})
})

router.POST("/signup", func(c *gin.Context) {
var signup SignUpParam
if err := c.ShouldBind(&signup); err != nil {
log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"msg": "注册成功",
})
})
router.Run(":8088")
}

接下来,我们用postman来发送请求

测试登陆接口

  1. 不加任何参数

  1. 只传入user,不满足长度要求

  1. 只传入user,满足长度要求

  1. 传入user和password

测试注册接口

输入的字段只满足name

输入的字段满足所有要求

注意

可以看到gin会把验证的所有错误抛出来, 同时错误信息不太友好, 其实validator是支持多语言的,接下来我们将错误信息翻译成中文

将错误信息翻译成中文

在看下面这个demo之前,请先读懂validator给的example

package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"log"
"net/http"
)

// postman/YAPI
//如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
//否则只能通过body/raw/JSON的方式发送
type SignUpForm struct {
//多个条件用逗号隔开 不用加空格
Name string `json:"name" binding:"required,min=3"`
Age uint8 `json:"age" binding:"gte=1,lte=130"`
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
//跨字段
RePassword string `json:"re_password" binding:"required,eqfield=Password"`
}

var trans ut.Translator //国际化翻译器

func InitTrans(locale string) (err error) {
//修改gin框架中的validator引擎属性,实现定制
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
zhT := zh.New() //中文翻译器
enT := en.New() //英文翻译器
//第一个参数是备用的语言环境 后面的参数是应该被支持的语言环境
uni := ut.New(enT, zhT, enT)
trans, ok = uni.GetTranslator(locale) //初始化全局翻译器
if !ok {
return fmt.Errorf("uni.GetTranslator(%s)", locale)
}

switch locale {
case "en":
en_translations.RegisterDefaultTranslations(v, trans)
case "zh":
zh_translations.RegisterDefaultTranslations(v, trans)
default:
en_translations.RegisterDefaultTranslations(v, trans)
}
return
}
return
}

func main() {
if err := InitTrans("zh"); err != nil {
fmt.Println("初始化翻译器错误")
return
}
router := gin.Default()
router.POST("/signpForm", func(c *gin.Context) {
var signupForm SignUpForm
if err := c.ShouldBind(&signupForm); err != nil {
//
errs, ok := err.(validator.ValidationErrors)
//如果非验证错误
if !ok {
c.JSON(http.StatusOK, gin.H{
"msg": errs.Error(),
})
return
}
//如果是验证错误
c.JSON(http.StatusBadRequest, gin.H{
"error": errs.Translate(trans),
})
return
log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"user": signupForm.Name,
"age": signupForm.Age,
"email": signupForm.Email,
"password": signupForm.Password,
"msg": "登录成功",
})
})
router.Run(":8088")
}

接着我们用postman测试一下接口

可以看到错误信息提示已经人性化很多了,但是字段名还是golang的风格,不符合json的模式

将错误信息里的字段名大写改成小写

package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"log"
"net/http"
"reflect"
"strings"
)

// postman/YAPI
// 如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
// 否则只能通过body/raw/JSON的方式发送
type SignUpForm struct {
//多个条件用逗号隔开 不用加空格
Name string `json:"name,aa" binding:"required,min=3"`
Age uint8 `json:"age" binding:"gte=1,lte=130"`
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
//跨字段
RePassword string `json:"re_password" binding:"required,eqfield=Password"`
}

var trans ut.Translator //国际化翻译器

func InitTrans(locale string) (err error) {
//修改gin框架中的validator引擎属性,实现定制
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
//注册一个获取json的tag的自定义方法
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
fmt.Println("fld.Tag.Get(json):", fld.Tag.Get("json"))
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
fmt.Println("json tag name:", name)
//兼容 - : golang中json tag为 - 会被忽略
if name == "-" { //RePassword string `json:"-"`
return ""
}
return name
})
zhT := zh.New() //中文翻译器
enT := en.New() //英文翻译器
//第一个参数是备用的语言环境 后面的参数是应该被支持的语言环境
uni := ut.New(enT, zhT, enT)
trans, ok = uni.GetTranslator(locale)
if !ok {
return fmt.Errorf("uni.GetTranslator(%s)", locale)
}

switch locale {
case "en":
en_translations.RegisterDefaultTranslations(v, trans)
case "zh":
zh_translations.RegisterDefaultTranslations(v, trans)
default:
en_translations.RegisterDefaultTranslations(v, trans)
}
return
}
return
}

func main() {
if err := InitTrans("zh"); err != nil {
fmt.Println("初始化翻译器错误")
return
}
router := gin.Default()
router.POST("/signpForm", func(c *gin.Context) {
var signupForm SignUpForm
if err := c.ShouldBind(&signupForm); err != nil {
//
errs, ok := err.(validator.ValidationErrors)
if !ok {
c.JSON(http.StatusOK, gin.H{
"msg": errs.Error(),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
//errs.Translate(trans)的本质就是map[string]string
"error": errs.Translate(trans),
})
return
log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"user": signupForm.Name,
"age": signupForm.Age,
"email": signupForm.Email,
"password": signupForm.Password,
"msg": "登录成功",
})
})
router.Run(":8088")
}

再次发送请求

可以看到 Email 已变成了email, 字段名已成功以json tag为主

最终改造

上面的实例还是不太友好,我们要将 "SignUpForm.email" 改成 "email"

errs.Translate(trans)的本质就是map[string]string,我们修改一下key就ok了

package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"log"
"net/http"
"reflect"
"strings"
)

// postman/YAPI
// 如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
// 否则只能通过body/raw/JSON的方式发送
type SignUpForm struct {
//多个条件用逗号隔开 不用加空格
Name string `json:"name,aa" binding:"required,min=3"`
Age uint8 `json:"age" binding:"gte=1,lte=130"`
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
//跨字段
RePassword string `json:"re_password" binding:"required,eqfield=Password"`
}

var trans ut.Translator //国际化翻译器

func InitTrans(locale string) (err error) {
//修改gin框架中的validator引擎属性,实现定制
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
//注册一个获取json的tag的自定义方法
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
//fmt.Println("fld.Tag.Get(json):", fld.Tag.Get("json"))
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
//fmt.Println("json tag name:", name)
//兼容 - : golang中json tag为 - 会被忽略
if name == "-" { //RePassword string `json:"-"`
return ""
}
return name
})
zhT := zh.New() //中文翻译器
enT := en.New() //英文翻译器
//第一个参数是备用的语言环境 后面的参数是应该被支持的语言环境
uni := ut.New(enT, zhT, enT)
trans, ok = uni.GetTranslator(locale)
if !ok {
return fmt.Errorf("uni.GetTranslator(%s)", locale)
}

switch locale {
case "en":
en_translations.RegisterDefaultTranslations(v, trans)
case "zh":
zh_translations.RegisterDefaultTranslations(v, trans)
default:
en_translations.RegisterDefaultTranslations(v, trans)
}
return
}
return
}

// eg: "SignUpForm.email"` 改成 `"email"`
func fixStructKey(fileds map[string]string) map[string]string {
rsp := make(map[string]string)
for field, err := range fileds {
rsp[field[strings.LastIndex(field, ".")+1:]] = err
}
return rsp
}

func main() {
if err := InitTrans("zh"); err != nil {
fmt.Println("初始化翻译器错误")
return
}
router := gin.Default()
router.POST("/signpForm", func(c *gin.Context) {
var signupForm SignUpForm
if err := c.ShouldBind(&signupForm); err != nil {
//
errs, ok := err.(validator.ValidationErrors)
if !ok {
c.JSON(http.StatusOK, gin.H{
"msg": errs.Error(),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
//errs.Translate(trans)的本质就是map[string]string
"error": fixStructKey(errs.Translate(trans)),
})
return
log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"user": signupForm.Name,
"age": signupForm.Age,
"email": signupForm.Email,
"password": signupForm.Password,
"msg": "登录成功",
})
})
router.Run(":8088")
}

再次发送请求

OK!

自定义验证器

业务要求变化多样,官方的validator不可能满足我们的所有要求,因此validator也暴露出了定制自己的验证器的方法

  1. 定义验证器
//返回值是否通过验证
func ValidateXXX(fl validator.FieldLevel) bool{
//获取字段值
mobile := fl.Field().String()
//剩下的交给自己
return true
}
  1. 注册验证器
//注册验证器
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
_ = v.RegisterValidation("mobile", ValidateXXX)
}
  1. 接着我们就可以像下面这样添加自定义规则了
type LoginForm struct {
//多个条件用逗号隔开 不能加空格
Mobile string `json:"mobile" form:"mobile" binding:"required,mobile"`
Password string `json:"password" form:"password" binding:"required"`
}

来看看完整的demo:

package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"net/http"
"reflect"
"regexp"
"strings"
)

// postman/YAPI
// 如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
// 否则只能通过body/raw/JSON的方式发送
type LoginForm struct {
//多个条件用逗号隔开 不用加空格
Mobile string `json:"mobile" form:"mobile" binding:"required,mobile"`
Password string `json:"password" form:"password" binding:"required"`
}

var trans ut.Translator //国际化翻译器

// 返回值是否通过验证
func validateMobile(fl validator.FieldLevel) bool {
//获取字段值
mobile := fl.Field().String()
//剩下的交给自己
mobileRe := regexp.MustCompile(`^1([38][0-9]|14[579]|5[^4]|16[6]|7[1-35-8]|9[189])\d{8}$`)
if res := mobileRe.FindString(mobile); len(res) > 0 {
return true
}
return false
}

func InitTrans(locale string) (err error) {
//修改gin框架中的validator引擎属性,实现定制
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
//注册一个获取json的tag的自定义方法
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
//fmt.Println("fld.Tag.Get(json):", fld.Tag.Get("json"))
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
//fmt.Println("json tag name:", name)
//兼容 - : golang中json tag为 - 会被忽略
if name == "-" { //RePassword string `json:"-"`
return ""
}
return name
})
zhT := zh.New() //中文翻译器
enT := en.New() //英文翻译器
//第一个参数是备用的语言环境 后面的参数是应该被支持的语言环境
uni := ut.New(enT, zhT, enT)
trans, ok = uni.GetTranslator(locale)
if !ok {
return fmt.Errorf("uni.GetTranslator(%s)", locale)
}

switch locale {
case "en":
en_translations.RegisterDefaultTranslations(v, trans)
case "zh":
zh_translations.RegisterDefaultTranslations(v, trans)
default:
en_translations.RegisterDefaultTranslations(v, trans)
}
return
}
return
}

// eg: "SignUpForm.email"` 改成 `"email"`
func fixStructKey(fileds map[string]string) map[string]string {
rsp := make(map[string]string)
for field, err := range fileds {
rsp[field[strings.LastIndex(field, ".")+1:]] = err
}
return rsp
}

func handleValidatorError(c *gin.Context, err error) {
errs, ok := err.(validator.ValidationErrors)
if !ok {
c.JSON(http.StatusOK, gin.H{
"msg": errs.Error(),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
//errs.Translate(trans)的本质就是map[string]string
"error": fixStructKey(errs.Translate(trans)),
})
return
//log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}

func main() {
if err := InitTrans("zh"); err != nil {
fmt.Println("初始化翻译器错误")
return
}

//注册验证器
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
_ = v.RegisterValidation("mobile", validateMobile)
}

router := gin.Default()
router.POST("/signpForm", func(c *gin.Context) {
var loginForm LoginForm
if err := c.ShouldBind(&loginForm); err != nil {
handleValidatorError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"msg": "登录成功",
})
})
router.Run(":8088")
}

启动服务并向postman发起请求

  • a, 不携带任何参数, 接口返回正常

  • b, 手机号输入非法的

自定义的验证器居然漏翻译了

好在官方给出了解决方案(line 105-111)

package main

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"net/http"
"reflect"
"regexp"
"strings"
)

// postman/YAPI
// 如果想通过form-data表单的形式把数据传过来必须加 form:"user" 这个tag,
// 否则只能通过body/raw/JSON的方式发送
type LoginForm struct {
//多个条件用逗号隔开 不用加空格
Mobile string `json:"mobile" form:"mobile" binding:"required,mobile"`
Password string `json:"password" form:"password" binding:"required"`
}

var trans ut.Translator //国际化翻译器

// 返回值是否通过验证
func validateMobile(fl validator.FieldLevel) bool {
//获取字段值
mobile := fl.Field().String()
//剩下的交给自己
mobileRe := regexp.MustCompile(`^1([38][0-9]|14[579]|5[^4]|16[6]|7[1-35-8]|9[189])\d{8}$`)
if res := mobileRe.FindString(mobile); len(res) > 0 {
return true
}
return false
}

func InitTrans(locale string) (err error) {
//修改gin框架中的validator引擎属性,实现定制
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
//注册一个获取json的tag的自定义方法
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
//fmt.Println("fld.Tag.Get(json):", fld.Tag.Get("json"))
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
//fmt.Println("json tag name:", name)
//兼容 - : golang中json tag为 - 会被忽略
if name == "-" { //RePassword string `json:"-"`
return ""
}
return name
})
zhT := zh.New() //中文翻译器
enT := en.New() //英文翻译器
//第一个参数是备用的语言环境 后面的参数是应该被支持的语言环境
uni := ut.New(enT, zhT, enT)
trans, ok = uni.GetTranslator(locale)
if !ok {
return fmt.Errorf("uni.GetTranslator(%s)", locale)
}

switch locale {
case "en":
en_translations.RegisterDefaultTranslations(v, trans)
case "zh":
zh_translations.RegisterDefaultTranslations(v, trans)
default:
en_translations.RegisterDefaultTranslations(v, trans)
}
return
}
return
}

// eg: "SignUpForm.email"` 改成 `"email"`
func fixStructKey(fileds map[string]string) map[string]string {
rsp := make(map[string]string)
for field, err := range fileds {
rsp[field[strings.LastIndex(field, ".")+1:]] = err
}
return rsp
}

func handleValidatorError(c *gin.Context, err error) {
errs, ok := err.(validator.ValidationErrors)
if !ok {
c.JSON(http.StatusOK, gin.H{
"msg": errs.Error(),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
//errs.Translate(trans)的本质就是map[string]string
"error": fixStructKey(errs.Translate(trans)),
})
return
//log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}

func main() {
if err := InitTrans("zh"); err != nil {
fmt.Println("初始化翻译器错误")
return
}

//注册验证器
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
_ = v.RegisterValidation("mobile", validateMobile)
_ = v.RegisterTranslation("mobile", trans, func(ut ut.Translator) error {
return ut.Add("required", "{0}手机号码不合法", true) // see universal-translator for details
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("required", fe.Field())
return t
})
}

router := gin.Default()
router.POST("/signpForm", func(c *gin.Context) {
var loginForm LoginForm
if err := c.ShouldBind(&loginForm); err != nil {
handleValidatorError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"msg": "登录成功",
})
})
router.Run(":8088")
}

更新代码,再次请求

Everything is normal!

· One min read
package main

import (
"fmt"
"golang.org/x/net/websocket"
"net/http"
)

type Server struct {
conns map[*websocket.Conn]bool
}

func NewServer() *Server {
return &Server{
conns: make(map[*websocket.Conn]bool),
}
}

func (s *Server) handleWS(ws *websocket.Conn) {
fmt.Println("new incoming connection from client:", ws.RemoteAddr())
s.conns[ws] = true
s.readLoop(ws)
}

func (s *Server) readLoop(ws *websocket.Conn) {
buf := make([]byte, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
fmt.Println("read error:", err)
continue
}
data := buf[:n]
fmt.Println("received data:", string(data))
ws.Write([]byte("thank you for the msg"))
}
}

func main() {
s := NewServer()
http.Handle("/ws", websocket.Handler(s.handleWS))
http.ListenAndServe(":8911", nil)
}

· One min read

有时候我们需要定期的更新配置文件,如此处的getfolders.go需要去3个服务器爬取对应的信息,而每天目录都在变,如果你不更新,就会出现搜不到单的情况

//判断文件是否过时
//pth: 文件目录
//hour: 距现在多少小时算过时
func IsFileOutOfDate(pth string, hour int) bool {
tm1, err := getFileModTime(pth)
if err != nil {
return true
}

tm2 := time.Now().Unix() //unix定义返回的是s

if tm2-tm1 > int64(hour*3600) {
return true
} else {
return false
}
}

//获取文件修改时间 返回unix时间戳
func getFileModTime(pth string) (int64, error) {
f, err := os.Open(pth)
if err != nil {
return time.Now().Unix(), err
}
defer f.Close()

fi, err := f.Stat()
if err != nil {
return time.Now().Unix(), err
}

return fi.ModTime().Unix(), nil
}

相关定义src/io/fs/fs.go

参考:https://github.com/scott-x/gutils/blob/master/fs/time.go

· One min read

之前遇到个问题,在一段代码中这样设置WriteHeader,在浏览器怎么样都不是json。

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=utf-8")

后来才知道

如果这两种修改一起做,就必须让 w.WriteHeader 在所有的 w.Header.Set 之后,因为 w.WriteHeader 后 Set Header 是无效的。

所以正确的应该是

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

· 2 min read

In Go 1.1 and newer the most simple way to do this is with a bufio.Scanner. Here is a simple example that reads lines from a file:

从Go 1.1开始,最新也是最简洁的方法就是用bufio.Scanner, 下面的例子将展示逐行读取

package main

import (
"bufio"
"fmt"
"log"
"os"
)

func main() {
file, err := os.Open("/path/to/file.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}

This is the cleanest way to read from a Reader line by line.

这是最好的方式从Reader中逐行读取

There is one caveat: Scanner will error with lines longer than 65536 characters. If you know your line length is greater than 64K, use the Buffer() method to increase the scanner’s capacity:

警告:如果单行超过 65536 个字符,那么Scanner就会出问题。 如果你单行超过 65536 个字符, 你应该重写Reader.Read()

参考: https://stackoverflow.com/questions/8757389/reading-a-file-line-by-line-in-go#16615559#753

当单行字节超过65535个字节(也就是64k)的时候,请使用Buffer()方法来增加scanner的容量

scanner := bufio.NewScanner(file)

const maxCapacity = longLineLen // your required line length
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)

for scanner.Scan() {}

· One min read
package main

import (
"io"
"log"
"net/http"

"github.com/thinkeridea/go-extend/exnet"
)

func main() {

http.HandleFunc("/", ipHandler)
log.Println("server is running at: http://127.0.0.1:10086")
//block
http.ListenAndServe(":10086", nil)
}

func ipHandler(w http.ResponseWriter, r *http.Request) {
ip := exnet.ClientPublicIP(r)
if ip == "" {
ip = exnet.ClientIP(r)
}
// log.Println(getMyIPV6())
io.WriteString(w, ip)
}

· 7 min read

用户请求到达提供服务的服务器中间有很多的环节,导致服务获取用户真实的 ip 非常困难,大多数的框架及工具库都会封装各种获取用户真实 ip 的方法,在 exnet 包中也封装了各种 ip 相关的操作,其中就包含获取客户端 ip 的方法,比较实用的方法如下:

  • func ClientIP(r *http.Request) string ClientIP最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。
  • func ClientPublicIP(r *http.Request) string ClientPublicIP 尽最大努力实现获取客户端公网 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。
  • func HasLocalIP(ip net.IP) bool HasLocalIP 检测 IP 地址是否是内网地址
  • func HasLocalIPddr(ip string) bool HasLocalIPddr 检测 IP 地址字符串是否是内网地址
  • func RemoteIP(r *http.Request) string RemoteIP 通过 RemoteAddr 获取 IP 地址, 只是一个快速解析方法。

获取用户真实ip地址

ClientIP 方法 与 ClientPublicIP 方法的实现类似,只是一个按照 http 协议约定获取客户端 ip, 一个按照约定格式查找到公网 ip。

在网络与服务架构、业务逻辑复杂的环境中,按照 http 协议约定的方式,并非总能获取到真实的 ip,在我们的业务中用户流量经由三方多层级转发(都是三方自己实现的http client) ,难免会出现一些纰漏,这时越往后的服务获取用户真实 ip 越加困难,你甚至不知道自己获取的 ip 是否是真实的。

但是我们的客户经由三方转发而来的流量,那么客户极大多数甚至排除测试之外都是公网用户,结合使用 ClientPublicIP 和 ClientIP 方法总能更好的获取用户的真实 ip

用上面的方法总能有效的获取用户真实的 ip 地址,下面分析下两个方法的具体实现。

ClientIP 首先读取 X-Forwarded-For header 中用 , 分隔的第一个ip地址,如果这个地址不存在,就会从 X-Real-Ip header 中获取,如果还是不存在,说明流量并非是由反向代理转发而来,而是客户端直接请求服务,这时通过 http.Request.RemoteAddr 字段截取除去端口号的 ip 地址。

这个方法很简单,就是按照 http 约定的格式获取,其中 X-Forwarded-For 和 X-Real-Ip header 由反向代理填充,例如 nginx 或 haproxy。

ClientPublicIP 很简单,和 ClientIP 方法的读取顺序一样,只是试图中 X-Forwarded-For 列表中找到一个公网ip,如果没有检查 X-Real-Ip 是否是一个公网 ip,其次检查 http.Request.RemoteAddr 是否是公网ip,如果没有找到公网 ip 这返回一个空字符串。

这个方法可以让我们有机会优先获取到用户的公网 ip,往往公网 ip 对我们来说更有价值。

检查ip对否是内网地址

exnet 中还提供了检查 ip 地址是否是内网地址,这在有些情况下非常有用,比如:服务中有些接口只能内网访问,也就是只允许管理员访问(例如动态设定日志级别、查看服务 pprof 信息);我们想隐藏后端服务,只暴露给用户负载均衡(反向代理),用户无法直接访问我们的服务,这些方法及其有用,下面看看具体实现。

我的服务提供了动态设置日志级别,以便服务出现问题,可以第一时间查看调试日志分析具体原因,但是这个接口很危险,不应该暴露给公网,所以会用路由中间件检查请求是否来自公网,来自公网则返回 404。

该方法认为如下地址段都是内网地址:

两个检查方法实现差异仅接受参数类型不一致,检查过程都是逐个对比内网 ip 段是否包含该ip地址,如果不包含则判断该地址是否是回环地址。

获取反向代理ip

如何判断改地址来自反向代理服务器呢,不同的反向代理实现都有些差异,4 层反向代理甚至可以提供用户的真实 ip(http.Request.RemoteAddr 是用户的ip,而不是反向代理的), 而隐藏自己的ip,这里说一下常见的方法。

往往 http.Request.RemoteAddr 保存最后一个连接服务的客户端 ip,我们获取反向代理的ip地址,最简单有效的方法就是通过 http.Request.RemoteAddr 获取, exnet 中提供了 RemoteIP 的快捷方法,实现如下:

这是一个非常方便的脚手架,它仅仅切分 http.Request.RemoteAddr 的 ip 和端口,并返回有效的ip地址,但却可以简化我们的编写业务代码。