github.com/polarismesh/polaris@v1.17.8/admin/default.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package admin
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  
    24  	"github.com/polarismesh/polaris/admin/job"
    25  	"github.com/polarismesh/polaris/auth"
    26  	"github.com/polarismesh/polaris/cache"
    27  	"github.com/polarismesh/polaris/service"
    28  	"github.com/polarismesh/polaris/service/healthcheck"
    29  	"github.com/polarismesh/polaris/store"
    30  )
    31  
    32  var (
    33  	server         AdminOperateServer
    34  	maintainServer = &Server{}
    35  	finishInit     bool
    36  )
    37  
    38  // Initialize 初始化
    39  func Initialize(ctx context.Context, cfg *Config, namingService service.DiscoverServer,
    40  	healthCheckServer *healthcheck.Server, cacheMgn *cache.CacheManager, storage store.Store) error {
    41  
    42  	if finishInit {
    43  		return nil
    44  	}
    45  
    46  	err := initialize(ctx, cfg, namingService, healthCheckServer, cacheMgn, storage)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	finishInit = true
    52  	return nil
    53  }
    54  
    55  func initialize(_ context.Context, cfg *Config, namingService service.DiscoverServer,
    56  	healthCheckServer *healthcheck.Server, cacheMgn *cache.CacheManager, storage store.Store) error {
    57  
    58  	userMgn, err := auth.GetUserServer()
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	strategyMgn, err := auth.GetStrategyServer()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	maintainServer.namingServer = namingService
    69  	maintainServer.healthCheckServer = healthCheckServer
    70  	maintainServer.cacheMgn = cacheMgn
    71  	maintainServer.storage = storage
    72  
    73  	maintainJobs := job.NewMaintainJobs(namingService, cacheMgn, storage)
    74  	if err := maintainJobs.StartMaintianJobs(cfg.Jobs); err != nil {
    75  		return err
    76  	}
    77  
    78  	server = newServerAuthAbility(maintainServer, userMgn, strategyMgn)
    79  	return nil
    80  }
    81  
    82  // GetServer 获取已经初始化好的Server
    83  func GetServer() (AdminOperateServer, error) {
    84  	if !finishInit {
    85  		return nil, errors.New("AdminOperateServer has not done Initialize")
    86  	}
    87  
    88  	return server, nil
    89  }
    90  
    91  // GetOriginServer 获取已经初始化好的Server
    92  func GetOriginServer() (*Server, error) {
    93  	if !finishInit {
    94  		return nil, errors.New("AdminOperateServer has not done Initialize")
    95  	}
    96  
    97  	return maintainServer, nil
    98  }