2021-08-31 09:38:40 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-09 13:47:31 +08:00
|
|
|
"api/config"
|
|
|
|
"api/initialization"
|
|
|
|
"api/middleware"
|
|
|
|
"api/utils"
|
2021-08-31 09:38:40 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2021-09-01 11:19:29 +08:00
|
|
|
"time"
|
2021-08-31 09:38:40 +08:00
|
|
|
)
|
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
func main() {
|
|
|
|
initialization.InitSystem()
|
2021-08-31 09:38:40 +08:00
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
app := gin.Default()
|
2021-08-31 09:38:40 +08:00
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
app.Use(middleware.OpenTracing())
|
2021-08-31 09:38:40 +08:00
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
ipStr := strings.Join(utils.GetIps(), ",")
|
2021-08-31 13:53:48 +08:00
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
group := app.Group(fmt.Sprintf("/%v", config.AppInfo.AppName))
|
2021-08-31 09:38:40 +08:00
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
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))
|
2021-08-31 09:38:40 +08:00
|
|
|
})
|
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
group.POST("/hello", func(context *gin.Context) {
|
|
|
|
type param struct {
|
|
|
|
User string `json:"user"`
|
|
|
|
Age int `json:"age"`
|
2021-08-31 09:38:40 +08:00
|
|
|
}
|
2021-09-09 13:47:31 +08:00
|
|
|
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))
|
2021-08-31 09:38:40 +08:00
|
|
|
})
|
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
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))
|
2021-08-31 09:38:40 +08:00
|
|
|
})
|
|
|
|
|
2021-09-09 13:47:31 +08:00
|
|
|
app.Run(fmt.Sprintf(":%v", config.AppInfo.Port))
|
2021-08-31 09:38:40 +08:00
|
|
|
}
|