-
Notifications
You must be signed in to change notification settings - Fork 527
/
route.go
89 lines (81 loc) · 3.01 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* 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 internal
import (
"fmt"
"path/filepath"
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/apisix/manager-api/internal/conf"
"github.com/apisix/manager-api/internal/filter"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/handler/authentication"
"github.com/apisix/manager-api/internal/handler/consumer"
"github.com/apisix/manager-api/internal/handler/global_rule"
"github.com/apisix/manager-api/internal/handler/healthz"
"github.com/apisix/manager-api/internal/handler/label"
"github.com/apisix/manager-api/internal/handler/plugin"
"github.com/apisix/manager-api/internal/handler/route"
"github.com/apisix/manager-api/internal/handler/route_online_debug"
"github.com/apisix/manager-api/internal/handler/server_info"
"github.com/apisix/manager-api/internal/handler/service"
"github.com/apisix/manager-api/internal/handler/ssl"
"github.com/apisix/manager-api/internal/handler/upstream"
"github.com/apisix/manager-api/internal/log"
)
func SetUpRouter() *gin.Engine {
if conf.ENV == conf.EnvLOCAL || conf.ENV == conf.EnvDEV {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
logger := log.GetLogger(log.AccessLog)
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("session", store))
r.Use(filter.CORS(), filter.RequestId(), filter.RequestLogHandler(logger), filter.SchemaCheck(), filter.Authentication(), filter.RecoverHandler())
r.Use(static.Serve("/", static.LocalFile(filepath.Join(conf.WorkDir, conf.WebDir), false)))
r.NoRoute(func(c *gin.Context) {
c.File(fmt.Sprintf("%s/index.html", filepath.Join(conf.WorkDir, conf.WebDir)))
})
factories := []handler.RegisterFactory{
route.NewHandler,
ssl.NewHandler,
consumer.NewHandler,
upstream.NewHandler,
service.NewHandler,
plugin.NewHandler,
healthz.NewHandler,
authentication.NewHandler,
global_rule.NewHandler,
route_online_debug.NewHandler,
server_info.NewHandler,
label.NewHandler,
}
for i := range factories {
h, err := factories[i]()
if err != nil {
panic(err)
}
h.ApplyRoute(r)
}
pprof.Register(r)
return r
}