diff --git a/api/main.go b/api/main.go index f58c65b0f4..15010ee8ea 100644 --- a/api/main.go +++ b/api/main.go @@ -22,40 +22,17 @@ import ( "time" "github.com/apisix/manager-api/conf" - "github.com/apisix/manager-api/filter" "github.com/apisix/manager-api/log" "github.com/apisix/manager-api/route" - "github.com/gin-contrib/pprof" - "github.com/gin-gonic/gin" ) var logger = log.GetLogger() -func setUpRouter() *gin.Engine { - if conf.ENV != conf.LOCAL && conf.ENV != conf.BETA { - gin.SetMode(gin.DebugMode) - } else { - gin.SetMode(gin.ReleaseMode) - } - r := gin.New() - - r.Use(filter.CORS(), filter.RequestId(), filter.RequestLogHandler(), filter.RecoverHandler()) - route.AppendHealthCheck(r) - route.AppendRoute(r) - route.AppendSsl(r) - route.AppendPlugin(r) - route.AppendUpstream(r) - - pprof.Register(r) - - return r -} - func main() { // init conf.InitializeMysql() // routes - r := setUpRouter() + r := route.SetUpRouter() addr := fmt.Sprintf(":%d", conf.ServerPort) s := &http.Server{ Addr: addr, diff --git a/api/route/base.go b/api/route/base.go new file mode 100644 index 0000000000..51dd90b8fb --- /dev/null +++ b/api/route/base.go @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package route + +import ( + "github.com/gin-contrib/pprof" + "github.com/gin-gonic/gin" + + "github.com/apisix/manager-api/conf" + "github.com/apisix/manager-api/filter" +) + +func SetUpRouter() *gin.Engine { + if conf.ENV != conf.LOCAL && conf.ENV != conf.BETA { + gin.SetMode(gin.DebugMode) + } else { + gin.SetMode(gin.ReleaseMode) + } + r := gin.New() + + r.Use(filter.CORS(), filter.RequestId(), filter.RequestLogHandler(), filter.RecoverHandler()) + + AppendHealthCheck(r) + AppendRoute(r) + AppendSsl(r) + AppendPlugin(r) + AppendUpstream(r) + AppendConsumer(r) + + pprof.Register(r) + + return r +} diff --git a/api/route/base_test.go b/api/route/base_test.go index 7135db0c78..3e3a09621a 100644 --- a/api/route/base_test.go +++ b/api/route/base_test.go @@ -18,16 +18,13 @@ package route import ( "github.com/api7/apitest" - "github.com/gin-gonic/gin" "github.com/apisix/manager-api/conf" - "github.com/apisix/manager-api/filter" ) var handler *apitest.APITest var ( - r = gin.New() uriPrefix = "/apisix/admin" ) @@ -35,18 +32,7 @@ func init() { //init mysql connect conf.InitializeMysql() - //filters - r.Use( - filter.CORS(), - filter.RequestId(), - filter.RequestLogHandler(), - filter.RecoverHandler()) + r := SetUpRouter() - handler = apitest.New(). - Handler(AppendHealthCheck(r)). - Handler(AppendRoute(r)). - Handler(AppendSsl(r)). - Handler(AppendPlugin(r)). - Handler(AppendConsumer(r)). - Handler(AppendUpstream(r)) + handler = apitest.New().Handler(r) }