oceanbase
128 строк · 3.3 Кб
1/**
2* Copyright (c) 2021 OceanBase
3* OceanBase CE is licensed under Mulan PubL v2.
4* You can use this software according to the terms and conditions of the Mulan PubL v2.
5* You may obtain a copy of Mulan PubL v2 at:
6* http://license.coscl.org.cn/MulanPubL-2.0
7* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10* See the Mulan PubL v2 for more details.
11*/
12
13package server
14
15import (
16"context"
17"net/http"
18"sync"
19"time"
20
21"github.com/gin-gonic/gin"
22"github.com/pkg/errors"
23log "github.com/sirupsen/logrus"
24
25"github.com/oceanbase/configserver/lib/codec"
26"github.com/oceanbase/configserver/lib/net"
27"github.com/oceanbase/configserver/lib/trace"
28)
29
30var invalidActionOnce sync.Once
31var invalidActionFunc func(*gin.Context)
32
33func getInvalidActionFunc() func(*gin.Context) {
34invalidActionOnce.Do(func() {
35invalidActionFunc = handlerFunctionWrapper(invalidAction)
36})
37return invalidActionFunc
38}
39
40func getServerIdentity() string {
41ip, _ := net.GetLocalIpAddress()
42return ip
43}
44
45func handlerFunctionWrapper(f func(context.Context, *gin.Context) *ApiResponse) func(*gin.Context) {
46fn := func(c *gin.Context) {
47tStart := time.Now()
48traceId := trace.RandomTraceId()
49ctxlog := trace.ContextWithTraceId(traceId)
50log.WithContext(ctxlog).Infof("handle request: %s %s", c.Request.Method, c.Request.RequestURI)
51response := f(ctxlog, c)
52cost := time.Now().Sub(tStart).Milliseconds()
53response.TraceId = traceId
54response.Cost = cost
55response.Server = getServerIdentity()
56responseJson, err := codec.MarshalToJsonString(response)
57if err != nil {
58log.WithContext(ctxlog).Errorf("response: %s", "response serialization error")
59c.JSON(http.StatusInternalServerError, NewErrorResponse(errors.Wrap(err, "serialize response")))
60} else {
61log.WithContext(ctxlog).Infof("response: %s", responseJson)
62c.String(response.Code, string(responseJson))
63}
64}
65return fn
66}
67
68func invalidAction(ctxlog context.Context, c *gin.Context) *ApiResponse {
69log.WithContext(ctxlog).Error("invalid action")
70return NewIllegalArgumentResponse(errors.New("invalid action"))
71}
72
73func getHandler() gin.HandlerFunc {
74fn := func(c *gin.Context) {
75action := c.Query("Action")
76switch action {
77case "ObRootServiceInfo":
78getObRootServiceGetFunc()(c)
79
80case "GetObProxyConfig":
81getObProxyConfigFunc()(c)
82
83case "GetObRootServiceInfoUrlTemplate":
84getObProxyConfigWithTemplateFunc()(c)
85
86case "ObIDCRegionInfo":
87getObIdcRegionInfoFunc()(c)
88
89default:
90getInvalidActionFunc()(c)
91}
92}
93return gin.HandlerFunc(fn)
94}
95
96func postHandler() gin.HandlerFunc {
97
98fn := func(c *gin.Context) {
99action, _ := c.GetQuery("Action")
100switch action {
101case "ObRootServiceInfo":
102getObRootServicePostFunc()(c)
103
104case "GetObProxyConfig":
105getObProxyConfigFunc()(c)
106
107case "GetObRootServiceInfoUrlTemplate":
108getObProxyConfigWithTemplateFunc()(c)
109default:
110getInvalidActionFunc()(c)
111}
112}
113
114return gin.HandlerFunc(fn)
115}
116
117func deleteHandler() gin.HandlerFunc {
118fn := func(c *gin.Context) {
119action, _ := c.GetQuery("Action")
120switch action {
121case "ObRootServiceInfo":
122getObRootServiceDeleteFunc()(c)
123default:
124getInvalidActionFunc()(c)
125}
126}
127return gin.HandlerFunc(fn)
128}
129