oceanbase

Форк
0
82 строки · 2.1 Кб
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
	"fmt"
18
	"net/http"
19

20
	"github.com/gin-gonic/gin"
21
	_ "github.com/go-sql-driver/mysql"
22
	_ "github.com/mattn/go-sqlite3"
23
	"github.com/pkg/errors"
24

25
	"github.com/oceanbase/configserver/config"
26
	"github.com/oceanbase/configserver/ent"
27
	"github.com/oceanbase/configserver/lib/trace"
28
	"github.com/oceanbase/configserver/logger"
29
)
30

31
var configServer *ConfigServer
32

33
func GetConfigServer() *ConfigServer {
34
	return configServer
35
}
36

37
type ConfigServer struct {
38
	Config *config.ConfigServerConfig
39
	Server *HttpServer
40
	Client *ent.Client
41
}
42

43
func NewConfigServer(conf *config.ConfigServerConfig) *ConfigServer {
44
	server := &ConfigServer{
45
		Config: conf,
46
		Server: &HttpServer{
47
			Counter: new(Counter),
48
			Router:  gin.Default(),
49
			Server:  &http.Server{},
50
			Address: conf.Server.Address,
51
		},
52
		Client: nil,
53
	}
54
	configServer = server
55
	return configServer
56
}
57

58
func (server *ConfigServer) Run() error {
59
	client, err := ent.Open(server.Config.Storage.DatabaseType, server.Config.Storage.ConnectionUrl)
60
	if err != nil {
61
		return errors.Wrap(err, fmt.Sprintf("initialize storage client with config %v", server.Config.Storage))
62
	}
63

64
	server.Client = client
65

66
	defer server.Client.Close()
67

68
	if err := server.Client.Schema.Create(context.Background()); err != nil {
69
		return errors.Wrap(err, "create configserver schema")
70
	}
71

72
	// start http server
73
	ctx, cancel := context.WithCancel(trace.ContextWithTraceId(logger.INIT_TRACEID))
74
	server.Server.Cancel = cancel
75

76
	// register route
77
	InitConfigServerRoutes(server.Server.Router)
78

79
	// run http server
80
	server.Server.Run(ctx)
81
	return nil
82
}
83

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

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

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

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