github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/http.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cdc
    15  
    16  import (
    17  	"net/http"
    18  	"net/http/pprof"
    19  
    20  	"github.com/gin-gonic/gin"
    21  	"github.com/pingcap/failpoint"
    22  	"github.com/pingcap/tiflow/cdc/api/owner"
    23  	"github.com/pingcap/tiflow/cdc/api/status"
    24  	v1 "github.com/pingcap/tiflow/cdc/api/v1"
    25  	v2 "github.com/pingcap/tiflow/cdc/api/v2"
    26  	"github.com/pingcap/tiflow/cdc/capture"
    27  	_ "github.com/pingcap/tiflow/docs/swagger" // use for OpenAPI online docs
    28  	"github.com/pingcap/tiflow/pkg/config"
    29  	"github.com/pingcap/tiflow/pkg/util"
    30  	"github.com/prometheus/client_golang/prometheus"
    31  	"github.com/prometheus/client_golang/prometheus/promhttp"
    32  	swaggerFiles "github.com/swaggo/files"
    33  	ginSwagger "github.com/swaggo/gin-swagger"
    34  )
    35  
    36  // RegisterRoutes create a router for OpenAPI
    37  func RegisterRoutes(
    38  	router *gin.Engine,
    39  	capture capture.Capture,
    40  	registry prometheus.Gatherer,
    41  ) {
    42  	// online docs
    43  	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
    44  
    45  	// Open API V1
    46  	v1.RegisterOpenAPIRoutes(router, v1.NewOpenAPI(capture))
    47  	// Open API V2
    48  	v2.RegisterOpenAPIV2Routes(router, v2.NewOpenAPIV2(capture))
    49  
    50  	// Owner API
    51  	owner.RegisterOwnerAPIRoutes(router, capture)
    52  
    53  	// Status API
    54  	status.RegisterStatusAPIRoutes(router, capture)
    55  
    56  	// Log API
    57  	router.POST("/admin/log", gin.WrapF(owner.HandleAdminLogLevel))
    58  	router.GET("/config", func(c *gin.Context) {
    59  		c.JSON(http.StatusOK, config.GetGlobalServerConfig())
    60  	})
    61  
    62  	// pprof debug API
    63  	pprofGroup := router.Group("/debug/pprof/")
    64  	pprofGroup.GET("", gin.WrapF(pprof.Index))
    65  	pprofGroup.GET("/:any", gin.WrapF(pprof.Index))
    66  	pprofGroup.GET("/cmdline", gin.WrapF(pprof.Cmdline))
    67  	pprofGroup.GET("/profile", gin.WrapF(pprof.Profile))
    68  	pprofGroup.GET("/symbol", gin.WrapF(pprof.Symbol))
    69  	pprofGroup.GET("/trace", gin.WrapF(pprof.Trace))
    70  	pprofGroup.GET("/threadcreate", gin.WrapF(pprof.Handler("threadcreate").ServeHTTP))
    71  
    72  	// Failpoint API
    73  	if util.FailpointBuild {
    74  		// `http.StripPrefix` is needed because `failpoint.HttpHandler` assumes that it handles the prefix `/`.
    75  		router.Any("/debug/fail/*any", gin.WrapH(http.StripPrefix("/debug/fail", &failpoint.HttpHandler{})))
    76  	}
    77  
    78  	// Promtheus metrics API
    79  	prometheus.DefaultGatherer = registry
    80  	router.Any("/metrics", gin.WrapH(promhttp.Handler()))
    81  }