|
|
@@ -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) {
|