123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package main
- import (
- "../serverImpl"
- "fmt"
- "git.wanpinghui.com/WPH/go_common/wph"
- "github.com/gin-gonic/gin"
- "net/http"
- )
- // Istio测试用,HTTP后端服务
- func main() {
- router := gin.New()
- // 添加 GIN 请求处理中间件
- //router.Use(
- // gzip.Gzip(gzip.DefaultCompression),
- // mw.Favicon("./static/favicon.ico"), // 请求图标时返回favicon.ico
- // mw.AddCrossOriginHeaders(), // 添加跨域头
- // mw.HandleOptionsMethod(), // Options和Head请求处理
- //)
- // 设置404、405响应
- router.NoMethod(func(c *gin.Context) {
- resp := wph.E(http.StatusMethodNotAllowed, "Method Not Allowed", nil)
- resp.HttpStatus = http.StatusMethodNotAllowed
- panic(resp)
- })
- router.NoRoute(func(c *gin.Context) {
- resp := wph.E(http.StatusMethodNotAllowed, "Endpoint Not Found", nil)
- resp.HttpStatus = http.StatusMethodNotAllowed
- panic(resp)
- })
- // 注册路由
- router.GET("/backend", backend)
- // 开始监听HTTP请求
- httpServer := &http.Server{Addr: ":8080", Handler: router}
- go func(httpServer *http.Server) {
- println("Start http server")
- if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- println("http server failed to listen and serve", err.Error())
- }
- }(httpServer)
- serverImpl.SignalHandler(httpServer)
- }
- func backend(ctxt *gin.Context) {
- input, _ := ctxt.GetQuery("in")
- var ipStr string
- ip, err := wph.PrivateIPv4()
- if nil == err && nil != ip {
- ipStr = ip.String()
- }
- ctxt.JSON(http.StatusOK, wph.R(fmt.Sprintf("%s (from %s:8080)", input, ipStr)))
- }
|