31 lines
716 B
Go
31 lines
716 B
Go
package server
|
||
|
||
import (
|
||
"kra/internal/conf"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
khttp "github.com/go-kratos/kratos/v2/transport/http"
|
||
)
|
||
|
||
// NewHTTPServer 创建HTTP服务器,将gin路由挂载到kratos http server
|
||
func NewHTTPServer(c *conf.Server, ginRouter *gin.Engine) *khttp.Server {
|
||
var opts = []khttp.ServerOption{}
|
||
|
||
if c.Http.Network != "" {
|
||
opts = append(opts, khttp.Network(c.Http.Network))
|
||
}
|
||
if c.Http.Addr != "" {
|
||
opts = append(opts, khttp.Address(c.Http.Addr))
|
||
}
|
||
if c.Http.Timeout != nil {
|
||
opts = append(opts, khttp.Timeout(c.Http.Timeout.AsDuration()))
|
||
}
|
||
|
||
srv := khttp.NewServer(opts...)
|
||
|
||
// 关键:将gin路由挂载到kratos http server
|
||
srv.HandlePrefix("/", ginRouter)
|
||
|
||
return srv
|
||
}
|