Ver Fonte

添加接口

yjp há 1 ano atrás
pai
commit
5673b0588b
1 ficheiros alterados com 24 adições e 0 exclusões
  1. 24 0
      framework/core/api/api.go

+ 24 - 0
framework/core/api/api.go

@@ -47,6 +47,30 @@ func New(opts ...Option) *Api {
 	return api
 }
 
+func NewWithEngine(server *http.Server, engine *gin.Engine, opts ...Option) *Api {
+	options := new(Options)
+
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	if strutils.IsStringEmpty(options.port) {
+		options.port = "8080"
+	}
+
+	api := &Api{
+		options:    *options,
+		server:     server,
+		rootRouter: newRootRouter(engine),
+	}
+
+	if strutils.IsStringNotEmpty(options.urlPrefix) {
+		api.prefixRouter = newPrefixRouter(engine.Group(options.urlPrefix))
+	}
+
+	return api
+}
+
 func (api *Api) Start() error {
 	err := api.server.ListenAndServe()
 	if err != nil && !fserr.Is(err, http.ErrServerClosed) {