kra/internal/server/http.go

31 lines
716 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}