0
点赞
收藏
分享

微信扫一扫

GoLang 基于1.13.1Gin项目搭建

大雁f 2022-11-21 阅读 23


1.下载

wget https://dl.google.com/go/go1.13.1.linux-amd64.tar.gz

2.安装

tar -xvf go1.13.1.linux-amd64.tar.gz

mv go /usr/local/go

3.设置环境变量

编辑:.bash_profile

GOPATH=/usr/local/go
PATH=$PATH:$HOME/bin:$GOPATH/bin

export GO111MODULE=on
export GOPROXY=https://goproxy.io

参考:​​https://goproxy.io/​​

编写代码

package main

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

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

// Agent ...
type Agent struct {
AgentID string `json:"agent_id"`
QueuedAt string `json:"queued_at"`
QueuedBy string `json:"queued_by"`
}

// Details ...
type Details struct {
EventID string `json:"event_id"`
Endpoint string
Metric string
Content string
Priority int
Status string
}

// Test1 ...
type Test1 struct {
Agent Agent
Details Details
Description string
EventType string `json:"event_type"`
ServiceKey string `json:"service_key"`
}

// Test2 test2
type Test2 struct {
Data []*Test1
}

func main() {
r := gin.New()

// Global middleware
// Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release.
// By default gin.DefaultWriter = os.Stdout
r.Use(gin.Logger())

r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})

r.POST("/v1/test", func(c *gin.Context) {
var test Test1

if err := c.BindJSON(&test); err == nil {
log.Println("========================start io=====================")
time.Sleep(time.Duration(1) * time.Second)
log.Println("=========================end io=======================")
c.JSON(http.StatusOK, test)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}

})
r.POST("/v2/test", func(c *gin.Context) {
var test Test2

if err := c.BindJSON(&test); err == nil {
log.Println("========================start io=====================")
time.Sleep(time.Duration(1) * time.Second)
log.Println("=========================end io=======================")
c.JSON(http.StatusOK, test)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
})
r.POST("/v3/test", func(c *gin.Context) {
var test Test2

if err := c.BindJSON(&test); err == nil {
log.Println("========================start io=====================")
time.Sleep(time.Duration(1) * time.Second)
log.Println("=========================end io=======================")
c.JSON(http.StatusOK, test)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}

})
r.POST("/v4/test", func(c *gin.Context) {
var test Test2

if err := c.BindJSON(&test); err == nil {
log.Println("========================start io=====================")
time.Sleep(time.Duration(1) * time.Second)
log.Println("=========================end io=======================")
c.JSON(http.StatusOK, test)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}

})
r.Run() // listen and serve on 0.0.0.0:8080
}

 

举报

相关推荐

0 条评论