oceanbase
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
13package server
14
15import (
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
31var configServer *ConfigServer
32
33func GetConfigServer() *ConfigServer {
34return configServer
35}
36
37type ConfigServer struct {
38Config *config.ConfigServerConfig
39Server *HttpServer
40Client *ent.Client
41}
42
43func NewConfigServer(conf *config.ConfigServerConfig) *ConfigServer {
44server := &ConfigServer{
45Config: conf,
46Server: &HttpServer{
47Counter: new(Counter),
48Router: gin.Default(),
49Server: &http.Server{},
50Address: conf.Server.Address,
51},
52Client: nil,
53}
54configServer = server
55return configServer
56}
57
58func (server *ConfigServer) Run() error {
59client, err := ent.Open(server.Config.Storage.DatabaseType, server.Config.Storage.ConnectionUrl)
60if err != nil {
61return errors.Wrap(err, fmt.Sprintf("initialize storage client with config %v", server.Config.Storage))
62}
63
64server.Client = client
65
66defer server.Client.Close()
67
68if err := server.Client.Schema.Create(context.Background()); err != nil {
69return errors.Wrap(err, "create configserver schema")
70}
71
72// start http server
73ctx, cancel := context.WithCancel(trace.ContextWithTraceId(logger.INIT_TRACEID))
74server.Server.Cancel = cancel
75
76// register route
77InitConfigServerRoutes(server.Server.Router)
78
79// run http server
80server.Server.Run(ctx)
81return nil
82}
83