oceanbase

Форк
0
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

13
package server
14

15
import (
16
	"context"
17
	"net/http"
18
	"sync"
19
	"time"
20

21
	"github.com/gin-gonic/gin"
22
	"github.com/pkg/errors"
23
	log "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

30
var invalidActionOnce sync.Once
31
var invalidActionFunc func(*gin.Context)
32

33
func getInvalidActionFunc() func(*gin.Context) {
34
	invalidActionOnce.Do(func() {
35
		invalidActionFunc = handlerFunctionWrapper(invalidAction)
36
	})
37
	return invalidActionFunc
38
}
39

40
func getServerIdentity() string {
41
	ip, _ := net.GetLocalIpAddress()
42
	return ip
43
}
44

45
func handlerFunctionWrapper(f func(context.Context, *gin.Context) *ApiResponse) func(*gin.Context) {
46
	fn := func(c *gin.Context) {
47
		tStart := time.Now()
48
		traceId := trace.RandomTraceId()
49
		ctxlog := trace.ContextWithTraceId(traceId)
50
		log.WithContext(ctxlog).Infof("handle request: %s %s", c.Request.Method, c.Request.RequestURI)
51
		response := f(ctxlog, c)
52
		cost := time.Now().Sub(tStart).Milliseconds()
53
		response.TraceId = traceId
54
		response.Cost = cost
55
		response.Server = getServerIdentity()
56
		responseJson, err := codec.MarshalToJsonString(response)
57
		if err != nil {
58
			log.WithContext(ctxlog).Errorf("response: %s", "response serialization error")
59
			c.JSON(http.StatusInternalServerError, NewErrorResponse(errors.Wrap(err, "serialize response")))
60
		} else {
61
			log.WithContext(ctxlog).Infof("response: %s", responseJson)
62
			c.String(response.Code, string(responseJson))
63
		}
64
	}
65
	return fn
66
}
67

68
func invalidAction(ctxlog context.Context, c *gin.Context) *ApiResponse {
69
	log.WithContext(ctxlog).Error("invalid action")
70
	return NewIllegalArgumentResponse(errors.New("invalid action"))
71
}
72

73
func getHandler() gin.HandlerFunc {
74
	fn := func(c *gin.Context) {
75
		action := c.Query("Action")
76
		switch action {
77
		case "ObRootServiceInfo":
78
			getObRootServiceGetFunc()(c)
79

80
		case "GetObProxyConfig":
81
			getObProxyConfigFunc()(c)
82

83
		case "GetObRootServiceInfoUrlTemplate":
84
			getObProxyConfigWithTemplateFunc()(c)
85

86
		case "ObIDCRegionInfo":
87
			getObIdcRegionInfoFunc()(c)
88

89
		default:
90
			getInvalidActionFunc()(c)
91
		}
92
	}
93
	return gin.HandlerFunc(fn)
94
}
95

96
func postHandler() gin.HandlerFunc {
97

98
	fn := func(c *gin.Context) {
99
		action, _ := c.GetQuery("Action")
100
		switch action {
101
		case "ObRootServiceInfo":
102
			getObRootServicePostFunc()(c)
103

104
		case "GetObProxyConfig":
105
			getObProxyConfigFunc()(c)
106

107
		case "GetObRootServiceInfoUrlTemplate":
108
			getObProxyConfigWithTemplateFunc()(c)
109
		default:
110
			getInvalidActionFunc()(c)
111
		}
112
	}
113

114
	return gin.HandlerFunc(fn)
115
}
116

117
func deleteHandler() gin.HandlerFunc {
118
	fn := func(c *gin.Context) {
119
		action, _ := c.GetQuery("Action")
120
		switch action {
121
		case "ObRootServiceInfo":
122
			getObRootServiceDeleteFunc()(c)
123
		default:
124
			getInvalidActionFunc()(c)
125
		}
126
	}
127
	return gin.HandlerFunc(fn)
128
}
129

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.