github.com/polarismesh/polaris@v1.17.8/apiserver/apiserver.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 apiserver 19 20 import ( 21 "context" 22 "fmt" 23 ) 24 25 const ( 26 DiscoverAccess string = "discover" 27 RegisterAccess string = "register" 28 HealthcheckAccess string = "healthcheck" 29 CreateFileAccess string = "createfile" 30 ) 31 32 // Config API服务器配置 33 type Config struct { 34 Name string 35 Option map[string]interface{} 36 API map[string]APIConfig 37 } 38 39 // APIConfig API配置 40 type APIConfig struct { 41 Enable bool 42 Include []string 43 } 44 45 // Apiserver API服务器接口 46 type Apiserver interface { 47 // GetProtocol API协议名 48 GetProtocol() string 49 // GetPort API的监听端口 50 GetPort() uint32 51 // Initialize API初始化逻辑 52 Initialize(ctx context.Context, option map[string]interface{}, api map[string]APIConfig) error 53 // Run API服务的主逻辑循环 54 Run(errCh chan error) 55 // Stop 停止API端口监听 56 Stop() 57 // Restart 重启API 58 Restart(option map[string]interface{}, api map[string]APIConfig, errCh chan error) error 59 } 60 61 var ( 62 Slots = make(map[string]Apiserver) 63 ) 64 65 // Register 注册API服务器 66 func Register(name string, server Apiserver) error { 67 if _, exist := Slots[name]; exist { 68 return fmt.Errorf("apiserver name:%s exist", name) 69 } 70 71 Slots[name] = server 72 73 return nil 74 }