golang-nacos-api-demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
1.2 KiB

package main
import (
"api/config"
"api/initialization"
"api/middleware"
"api/utils"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"time"
)
func main() {
initialization.InitSystem()
app := gin.Default()
app.Use(middleware.OpenTracing())
ipStr := strings.Join(utils.GetIps(), ",")
group := app.Group(fmt.Sprintf("/%v", config.AppInfo.AppName))
group.GET("/hello", func(context *gin.Context) {
name := context.Query("name")
context.String(http.StatusBadGateway, fmt.Sprintf("[GET]-[%v]-[latest]\n%v\n%v", config.AppInfo.AppName, ipStr, name))
})
group.POST("/hello", func(context *gin.Context) {
type param struct {
User string `json:"user"`
Age int `json:"age"`
}
p := param{}
err := context.ShouldBindJSON(&p)
if err != nil {
context.String(http.StatusInternalServerError, "服务异常")
return
}
context.String(http.StatusOK, fmt.Sprintf("[GET]-[%v]-[latest]\n%v\n%v", config.AppInfo.AppName, ipStr, p.User))
})
group.GET("/ip", func(context *gin.Context) {
time.Sleep(1 * time.Second)
context.String(http.StatusOK, fmt.Sprintf("[%v]\n%v", config.AppInfo.AppName, ipStr))
})
app.Run(fmt.Sprintf(":%v", config.AppInfo.Port))
}