hi,
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
....
})
router.Run(":8080") // data services
routerAdmin := gin.Default()
routerAdmin.GET("/", func(c *gin.Context) {
.....
})
routerAdmin.Run(":8090") // admin and monitor services
}
but , only one port ( 8080 port ) working
yet, i know there some others solution to split different URL rout for two propose.
i just want to know single program can service tow port or not.
some one help for this??
thanks a lot.
gin.Run() is blocking so you have to call them in separate goroutines if you want to accomplish that.
@tsingson @slimmy is right.
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
....
})
go router.Run(":8080") // data services
routerAdmin := gin.Default()
routerAdmin.GET("/", func(c *gin.Context) {
.....
})
routerAdmin.Run(":8090") // admin and monitor services
}
@slimmy @manucorporat thanks a lot!!
_you can do it like this:_
go func() { router1.Run("127.0.0.1:9000") }()
router2.Run("127.0.0.1:8000")
Is there support for multiple sites/vhosts?
See the PR #1119 and the following example:
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
)
var (
g errgroup.Group
)
func router01() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.GET("/", func(c *gin.Context) {
c.JSON(
http.StatusOK,
gin.H{
"code": http.StatusOK,
"error": "Welcome server 01",
},
)
})
return e
}
func router02() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.GET("/", func(c *gin.Context) {
c.JSON(
http.StatusOK,
gin.H{
"code": http.StatusOK,
"error": "Welcome server 02",
},
)
})
return e
}
func main() {
server01 := &http.Server{
Addr: ":8080",
Handler: router01(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
server02 := &http.Server{
Addr: ":8081",
Handler: router02(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
g.Go(func() error {
return server01.ListenAndServe()
})
g.Go(func() error {
return server02.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
Hi, Run multiple service using Gin can use https ? I want create a http and https multiple service
Hi! How to use graceful shutdown with multiple servers?
Most helpful comment
See the PR #1119 and the following example: