MSP-8172 Add vendor
This commit is contained in:
1 parent
5175d87e08
commit
eb066cb055
2619 files changed
+757077
-2
No files matched your search
+91
@@ -0,0 +1,91 @@
|
||||
# Package `common_endpoints`
|
||||
|
||||
Пакет `common_endpoints` предназначен для формирования набора стандартных (общих) эндпоинтов сервиса.
|
||||
|
||||
## Types
|
||||
|
||||
### CommonEndpointOption
|
||||
|
||||
Тип `CommonEndpointOption` представляет собой функциональную опцию для конфигурации набора общих эндпоинтов.
|
||||
|
||||
Используется для модификации внутренней конфигурации `commonEndpointsCfg`.
|
||||
|
||||
```go
|
||||
type CommonEndpointOption func(cfg *commonEndpointsCfg)
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
### `CommonEndpoints(basePath string, opts ...CommonEndpointOption) []cluster.EndpointDescriptor`
|
||||
|
||||
Создаёт и возвращает список `cluster.EndpointDescriptor` на основе переданных опций.
|
||||
|
||||
* `basePath` — базовый путь для всех создаваемых эндпоинтов.
|
||||
* `opts` — набор опций конфигурации (например, добавление swagger-endpoint).
|
||||
|
||||
### `WithSwaggerEndpoint(swagger []byte) CommonEndpointOption`
|
||||
|
||||
Добавляет endpoint для получения Swagger-спецификации.
|
||||
|
||||
* `swagger` — содержимое swagger-файла в виде `[]byte`, если он пустой, endpoint не будет добавлен.
|
||||
* Данные автоматически кодируются в Base64 перед возвратом клиенту.
|
||||
|
||||
Endpoint будет доступен по пути:
|
||||
|
||||
```
|
||||
{basePath}/swagger
|
||||
```
|
||||
|
||||
и будет иметь параметры:
|
||||
|
||||
* HTTP метод: `GET`
|
||||
* `UserAuthRequired = false`
|
||||
* `Inner = false`
|
||||
|
||||
|
||||
## Internal behavior
|
||||
|
||||
### Swagger endpoint
|
||||
|
||||
Swagger endpoint формируется функцией `swaggerEndpoint` и возвращает `cluster.EndpointDescriptor` со следующими характеристиками:
|
||||
|
||||
* Path: `basePath + "/swagger"`
|
||||
* Handler: возвращает Base64-кодированную строку swagger-файла
|
||||
* Не требует аутентификации пользователя
|
||||
* Использует метод `GET`
|
||||
|
||||
## Usage
|
||||
|
||||
### Default usage flow
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/common_endpoints"
|
||||
)
|
||||
|
||||
func main() {
|
||||
swaggerBytes, _ := os.ReadFile("swagger.json")
|
||||
|
||||
endpoints := common_endpoints.CommonEndpoints(
|
||||
"/api",
|
||||
common_endpoints.WithSwaggerEndpoint(swaggerBytes),
|
||||
)
|
||||
|
||||
var descriptors []cluster.EndpointDescriptor
|
||||
descriptors = append(descriptors, endpoints...)
|
||||
|
||||
// передаём descriptors в config сервис/router
|
||||
}
|
||||
```
|
||||
|
||||
### Without swagger endpoint
|
||||
|
||||
```go
|
||||
endpoints := common_endpoints.CommonEndpoints("/api")
|
||||
// endpoints будет пустым
|
||||
```
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package common_endpoints
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
)
|
||||
|
||||
// commonEndpointsCfg holds configuration for common endpoints.
|
||||
type commonEndpointsCfg struct {
|
||||
swagger []byte
|
||||
}
|
||||
|
||||
// CommonEndpoints creates a slice of cluster.EndpointDescriptor for common service endpoints.
|
||||
// It accepts a basePath for URL path prefix and variadic CommonEndpointOption functions to configure endpoints.
|
||||
// Returns a slice of endpoint descriptors that can be registered with a cluster router.
|
||||
// Currently supports Swagger documentation endpoint when configured via WithSwaggerEndpoint.
|
||||
func CommonEndpoints(basePath string, opts ...CommonEndpointOption) []cluster.EndpointDescriptor {
|
||||
cfg := &commonEndpointsCfg{}
|
||||
for _, opt := range opts {
|
||||
opt(cfg)
|
||||
}
|
||||
|
||||
endpoints := make([]cluster.EndpointDescriptor, 0)
|
||||
if len(cfg.swagger) > 0 {
|
||||
endpoints = append(endpoints, swaggerEndpoint(basePath, cfg.swagger))
|
||||
}
|
||||
|
||||
return endpoints
|
||||
}
|
||||
|
||||
// swaggerEndpoint creates an endpoint descriptor for serving Swagger documentation.
|
||||
// The endpoint is registered at basePath+"/swagger" and returns the swagger JSON on HTTP GET.
|
||||
// User authentication is not required for this endpoint.
|
||||
func swaggerEndpoint(basePath string, swaggerJson json.RawMessage) cluster.EndpointDescriptor {
|
||||
return cluster.EndpointDescriptor{
|
||||
Path: basePath + "/swagger",
|
||||
Inner: false,
|
||||
UserAuthRequired: false,
|
||||
HttpMethod: http.MethodGet,
|
||||
Handler: func() json.RawMessage {
|
||||
return swaggerJson
|
||||
},
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// Package common_endpoints provides common HTTP endpoint descriptors for services.
|
||||
// It offers a simple way to register standard endpoints like Swagger documentation.
|
||||
package common_endpoints
|
||||
|
||||
// CommonEndpointOption is a functional option for configuring commonEndpointsCfg.
|
||||
type CommonEndpointOption func(cfg *commonEndpointsCfg)
|
||||
|
||||
// WithSwaggerEndpoint configures the CommonEndpoints builder to include a Swagger endpoint.
|
||||
// The swagger parameter should contain the JSON-formatted Swagger specification.
|
||||
// The endpoint is exposed at {basePath}/swagger and is accessible via HTTP GET.
|
||||
func WithSwaggerEndpoint(swagger []byte) CommonEndpointOption {
|
||||
return func(cfg *commonEndpointsCfg) {
|
||||
cfg.swagger = swagger
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user