forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
66 lines (59 loc) · 2 KB
/
router.go
File metadata and controls
66 lines (59 loc) · 2 KB
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
// Copyright 2022 TiKV Project Authors.
//
// Licensed 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 apiv2
import (
"context"
"net/http"
"sync"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/apiv2/handlers"
"github.com/tikv/pd/server/apiv2/middlewares"
)
var once sync.Once
var group = server.APIServiceGroup{
Name: "core",
IsCore: true,
Version: "v2",
PathPrefix: apiV2Prefix,
}
const apiV2Prefix = "/pd/api/v2/"
// NewV2Handler creates a HTTP handler for API.
// @title Placement Driver Core API
// @version 2.0
// @description This is placement driver.
// @contact.name Placement Driver Support
// @contact.url https://github.com/tikv/pd/issues
// @contact.email info@pingcap.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /pd/api/v2
func NewV2Handler(_ context.Context, svr *server.Server) (http.Handler, server.APIServiceGroup, error) {
once.Do(func() {
// See https://github.com/pingcap/tidb-dashboard/blob/f8ecb64e3d63f4ed91c3dca7a04362418ade01d8/pkg/apiserver/apiserver.go#L84
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
router := gin.New()
router.Use(func(c *gin.Context) {
c.Set("server", svr)
c.Next()
})
router.Use(middlewares.Redirector())
root := router.Group(apiV2Prefix)
handlers.RegisterKeyspace(root)
return router, group, nil
}