mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-03-10 23:25:37 +08:00
初步实现对象存储源站
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ossconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
|
||||
"strconv"
|
||||
@@ -18,6 +19,7 @@ type OriginConfig struct {
|
||||
Version int `yaml:"version" json:"version"` // 版本
|
||||
Name string `yaml:"name" json:"name"` // 名称
|
||||
Addr *NetworkAddressConfig `yaml:"addr" json:"addr"` // 地址
|
||||
OSS *ossconfigs.OSSConfig `yaml:"oss" json:"oss"` // 对象存储配置
|
||||
Description string `yaml:"description" json:"description"` // 描述 TODO
|
||||
Code string `yaml:"code" json:"code"` // 代号 TODO
|
||||
|
||||
@@ -171,6 +173,14 @@ func (this *OriginConfig) Init(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// oss
|
||||
if this.OSS != nil {
|
||||
err := this.OSS.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -217,3 +227,22 @@ func (this *OriginConfig) RequestURIHasVariables() bool {
|
||||
func (this *OriginConfig) UniqueKey() string {
|
||||
return this.uniqueKey
|
||||
}
|
||||
|
||||
// IsOSS 判断当前源站是否为OSS
|
||||
func (this *OriginConfig) IsOSS() bool {
|
||||
return this.Addr != nil && ossconfigs.IsOSSProtocol(this.Addr.Protocol.String())
|
||||
}
|
||||
|
||||
// AddrSummary 地址描述
|
||||
func (this *OriginConfig) AddrSummary() string {
|
||||
if this.Addr == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// OSS
|
||||
if ossconfigs.IsOSSProtocol(this.Addr.Protocol.String()) && this.OSS != nil {
|
||||
return this.OSS.Summary()
|
||||
}
|
||||
|
||||
return this.Addr.Protocol.String() + "://" + this.Addr.Host + ":" + this.Addr.PortRange
|
||||
}
|
||||
|
||||
85
pkg/serverconfigs/ossconfigs/oss_config.go
Normal file
85
pkg/serverconfigs/ossconfigs/oss_config.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package ossconfigs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type OSSConfig struct {
|
||||
Type OSSType `yaml:"oss" json:"type"`
|
||||
Options any `yaml:"options" json:"options"`
|
||||
}
|
||||
|
||||
func NewOSSConfig() *OSSConfig {
|
||||
return &OSSConfig{}
|
||||
}
|
||||
|
||||
func (this *OSSConfig) Init() error {
|
||||
if this.Options != nil {
|
||||
// decode options
|
||||
if reflect.TypeOf(this.Options).Kind() == reflect.Map {
|
||||
optionsJSON, err := json.Marshal(this.Options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newOptions, decodeErr := DecodeOSSOptions(this.Type, optionsJSON)
|
||||
if decodeErr != nil {
|
||||
return decodeErr
|
||||
}
|
||||
if newOptions != nil {
|
||||
this.Options = newOptions
|
||||
}
|
||||
}
|
||||
|
||||
options, ok := this.Options.(OSSOptions)
|
||||
if ok {
|
||||
err := options.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *OSSConfig) Summary() string {
|
||||
var name = ""
|
||||
var found = false
|
||||
for _, def := range FindAllOSSTypes() {
|
||||
if def.Code == this.Type {
|
||||
name = def.Name
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return ""
|
||||
}
|
||||
|
||||
var summary = ""
|
||||
if this.Options != nil {
|
||||
// decode options
|
||||
if reflect.TypeOf(this.Options).Kind() == reflect.Map {
|
||||
optionsJSON, err := json.Marshal(this.Options)
|
||||
if err == nil { // ignore error
|
||||
newOptions, decodeErr := DecodeOSSOptions(this.Type, optionsJSON)
|
||||
if decodeErr == nil && newOptions != nil {
|
||||
this.Options = newOptions
|
||||
}
|
||||
}
|
||||
}
|
||||
options, ok := this.Options.(OSSOptions)
|
||||
if ok {
|
||||
summary = options.Summary()
|
||||
}
|
||||
}
|
||||
|
||||
if len(summary) == 0 {
|
||||
return name
|
||||
}
|
||||
return name + " - " + summary
|
||||
}
|
||||
9
pkg/serverconfigs/ossconfigs/oss_options.go
Normal file
9
pkg/serverconfigs/ossconfigs/oss_options.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package ossconfigs
|
||||
|
||||
type OSSOptions interface {
|
||||
Init() error // 初始化
|
||||
Summary() string // 内容简述
|
||||
UniqueId() string // 唯一标识
|
||||
}
|
||||
16
pkg/serverconfigs/ossconfigs/oss_types.go
Normal file
16
pkg/serverconfigs/ossconfigs/oss_types.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package ossconfigs
|
||||
|
||||
import "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
|
||||
type OSSType = string
|
||||
|
||||
func FindAllOSSTypes() []*shared.Definition {
|
||||
return []*shared.Definition{}
|
||||
}
|
||||
|
||||
func DecodeOSSOptions(ossType OSSType, optionsJSON []byte) (any, error) {
|
||||
return nil, nil
|
||||
}
|
||||
9
pkg/serverconfigs/ossconfigs/oss_utils.go
Normal file
9
pkg/serverconfigs/ossconfigs/oss_utils.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package ossconfigs
|
||||
|
||||
import "strings"
|
||||
|
||||
func IsOSSProtocol(protocol string) bool {
|
||||
return strings.HasPrefix(protocol, "oss:")
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ossconfigs"
|
||||
|
||||
type Protocol string
|
||||
|
||||
const (
|
||||
@@ -52,7 +54,11 @@ func (this Protocol) IsUDPFamily() bool {
|
||||
return this == ProtocolUDP
|
||||
}
|
||||
|
||||
// 主协议
|
||||
func (this Protocol) IsOSS() bool {
|
||||
return ossconfigs.IsOSSProtocol(this.String())
|
||||
}
|
||||
|
||||
// Primary 主协议
|
||||
func (this Protocol) Primary() Protocol {
|
||||
switch this {
|
||||
case ProtocolHTTP, ProtocolHTTP4, ProtocolHTTP6:
|
||||
@@ -72,7 +78,7 @@ func (this Protocol) Primary() Protocol {
|
||||
}
|
||||
}
|
||||
|
||||
// Scheme
|
||||
// Scheme schema
|
||||
func (this Protocol) Scheme() string {
|
||||
return string(this)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user