狗浪gin做webserver

wy.mikuq.com到期了大球猫叫了一宿,主要需要两个功能
1、返回客户端IP
2、返回时间

package main

import (
    "net/http"
    "time"

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

func doIP(c *gin.Context) {
    c.String(http.StatusOK, c.ClientIP())
}

func doTime(c *gin.Context) {
    c.String(http.StatusOK, time.Now().Format("2006-01-02 15:04:05"))
}

func main() {
    r := gin.Default()
    r.GET("/api/ip", doIP)
    r.GET("/api/time", doTime)
    r.Run(":9001")
}

编译后竟然有10M

go mod init main
go run wy.go
set GOOS=linux
go build wy.go

修改nginx

location /api/ip/ {
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://127.0.0.1:9001/api/ip;
}

location /api/time/ {
  proxy_set_header X-Forward-For $remote_addr;
  proxy_set_header X-real-ip $remote_addr;
  proxy_pass http://127.0.0.1:9001/api/time;
}

猫的小技巧,结果9837K->7008K

go build -ldflags "-s -w" 

参考资料:
https://www.jianshu.com/p/3227289306e0
https://www.jianshu.com/p/98965b3ff638

标签: none

添加新评论