github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/api/v2/api.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 v2
    15  
    16  import (
    17  	"github.com/gin-gonic/gin"
    18  	"github.com/pingcap/tiflow/cdc/api"
    19  	"github.com/pingcap/tiflow/cdc/api/middleware"
    20  	"github.com/pingcap/tiflow/cdc/capture"
    21  	"github.com/pingcap/tiflow/cdc/model"
    22  )
    23  
    24  // OpenAPIV2 provides CDC v2 APIs
    25  type OpenAPIV2 struct {
    26  	capture capture.Capture
    27  	helpers APIV2Helpers
    28  }
    29  
    30  // NewOpenAPIV2 creates a new OpenAPIV2.
    31  func NewOpenAPIV2(c capture.Capture) OpenAPIV2 {
    32  	return OpenAPIV2{c, APIV2HelpersImpl{}}
    33  }
    34  
    35  // NewOpenAPIV2ForTest creates a new OpenAPIV2.
    36  func NewOpenAPIV2ForTest(c capture.Capture, h APIV2Helpers) OpenAPIV2 {
    37  	return OpenAPIV2{c, h}
    38  }
    39  
    40  // RegisterOpenAPIV2Routes registers routes for OpenAPI
    41  func RegisterOpenAPIV2Routes(router *gin.Engine, api OpenAPIV2) {
    42  	v2 := router.Group("/api/v2")
    43  
    44  	v2.Use(middleware.CheckServerReadyMiddleware(api.capture))
    45  	v2.Use(middleware.LogMiddleware())
    46  	v2.Use(middleware.ErrorHandleMiddleware())
    47  
    48  	v2.GET("health", api.health)
    49  	v2.GET("status", api.serverStatus)
    50  	v2.POST("log", api.setLogLevel)
    51  
    52  	controllerMiddleware := middleware.ForwardToControllerMiddleware(api.capture)
    53  	changefeedOwnerMiddleware := middleware.
    54  		ForwardToChangefeedOwnerMiddleware(api.capture, getChangefeedFromRequest)
    55  	authenticateMiddleware := middleware.AuthenticateMiddleware(api.capture)
    56  
    57  	// changefeed apis
    58  	changefeedGroup := v2.Group("/changefeeds")
    59  	changefeedGroup.GET("/:changefeed_id", changefeedOwnerMiddleware, api.getChangeFeed)
    60  	changefeedGroup.POST("", controllerMiddleware, authenticateMiddleware, api.createChangefeed)
    61  	changefeedGroup.GET("", controllerMiddleware, api.listChangeFeeds)
    62  	changefeedGroup.PUT("/:changefeed_id", changefeedOwnerMiddleware, authenticateMiddleware, api.updateChangefeed)
    63  	changefeedGroup.DELETE("/:changefeed_id", controllerMiddleware, authenticateMiddleware, api.deleteChangefeed)
    64  	changefeedGroup.GET("/:changefeed_id/meta_info", changefeedOwnerMiddleware, api.getChangeFeedMetaInfo)
    65  	changefeedGroup.POST("/:changefeed_id/resume", changefeedOwnerMiddleware, authenticateMiddleware, api.resumeChangefeed)
    66  	changefeedGroup.POST("/:changefeed_id/pause", changefeedOwnerMiddleware, authenticateMiddleware, api.pauseChangefeed)
    67  	changefeedGroup.GET("/:changefeed_id/status", changefeedOwnerMiddleware, api.status)
    68  	changefeedGroup.GET("/:changefeed_id/synced", changefeedOwnerMiddleware, api.synced)
    69  
    70  	// capture apis
    71  	captureGroup := v2.Group("/captures")
    72  	captureGroup.Use(controllerMiddleware)
    73  	captureGroup.POST("/:capture_id/drain", api.drainCapture)
    74  	captureGroup.GET("", api.listCaptures)
    75  
    76  	// processor apis
    77  	processorGroup := v2.Group("/processors")
    78  	processorGroup.GET("/:changefeed_id/:capture_id", changefeedOwnerMiddleware, api.getProcessor)
    79  	processorGroup.GET("", controllerMiddleware, api.listProcessors)
    80  
    81  	verifyTableGroup := v2.Group("/verify_table")
    82  	verifyTableGroup.POST("", api.verifyTable)
    83  
    84  	// unsafe apis
    85  	unsafeGroup := v2.Group("/unsafe")
    86  	unsafeGroup.Use(controllerMiddleware)
    87  	unsafeGroup.GET("/metadata", authenticateMiddleware, api.CDCMetaData)
    88  	unsafeGroup.POST("/resolve_lock", authenticateMiddleware, api.ResolveLock)
    89  	unsafeGroup.DELETE("/service_gc_safepoint", authenticateMiddleware, api.DeleteServiceGcSafePoint)
    90  
    91  	// owner apis
    92  	ownerGroup := v2.Group("/owner")
    93  	unsafeGroup.Use(controllerMiddleware)
    94  	ownerGroup.POST("/resign", api.resignController)
    95  
    96  	// common APIs
    97  	v2.POST("/tso", api.QueryTso)
    98  }
    99  
   100  // getChangefeedFromRequest returns the changefeed that parse from request
   101  func getChangefeedFromRequest(ctx *gin.Context) model.ChangeFeedID {
   102  	namespace := getNamespaceValueWithDefault(ctx)
   103  	return model.ChangeFeedID{
   104  		Namespace: namespace,
   105  		ID:        ctx.Param(api.APIOpVarChangefeedID),
   106  	}
   107  }