Skip to content

Commit

Permalink
fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Feb 7, 2024
1 parent edbfa74 commit 60c903a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
25 changes: 25 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ storages:
redirect-base: https://oss.example.com/base/paths
# 启动之前在 measure 子文件夹内生成 1-200MB 的测速文件 (默认为动态生成)
pre-gen-measures: false
# local 为本地存储
- type: local
# 使用该子节点的概率 (非负整数)
weight: 100
# 节点附加数据
data:
# 链接到下方 webdav-users 的键值对
alias: example-user
# Webdav 入口 URL
endpoint: https://webdav.example.com/path/to/optional/another/endpoint/
# 用户名
username: optional-another-username
# 密码
password: optional-another-password
# 同上
pre-gen-measures: false

webdav-users:
example-user:
# Webdav 入口 URL
endpoint: https://webdav.example.com/path/to/endpoint/
# 用户名
username: example-username
# 密码
password: example-password

```

Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type DashboardConfig struct {
}

type WebDavUser struct {
EndPoint string `yaml:"endpoint"`
Username string `yaml:"username"`
Password string `yaml:"password"`
EndPoint string `yaml:"endpoint,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
}

type Config struct {
Expand Down
69 changes: 48 additions & 21 deletions storage_webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ import (
)

type basicWebDavStorageOption struct {
Alias string `yaml:"alias,omitempty"`
PreGenMeasures bool `yaml:"pre-gen-measures"`
}

type WebDavStorageOption struct {
basicWebDavStorageOption
*WebDavUser
PreGenMeasures bool `yaml:"pre-gen-measures"`

Alias string `yaml:"alias,omitempty"`
aliasUser *WebDavUser `yaml:"-"`

WebDavUser `yaml:",inline,omitempty"`
}

var (
Expand All @@ -52,25 +54,50 @@ var (
)

func (o *WebDavStorageOption) MarshalYAML() (any, error) {
if o.Alias != "" {
return o.basicWebDavStorageOption, nil
}
type T WebDavStorageOption
return (*T)(o), nil
}

func (o *WebDavStorageOption) UnmarshalYAML(n *yaml.Node) (err error) {
o.Alias = ""
if err = n.Decode(&o.basicWebDavStorageOption); err != nil {
type T WebDavStorageOption
if err = n.Decode((*T)(o)); err != nil {
return
}
return
}

func (o *WebDavStorageOption) GetEndPoint() string {
if o.EndPoint != "" {
return o.EndPoint
}
if o.Alias != "" {
return
// assert o.aliasUser != nil
return o.aliasUser.EndPoint
}
if err = n.Decode(&o.WebDavUser); err != nil {
return
return ""
}

func (o *WebDavStorageOption) GetUsername() string {
if o.Username != "" {
return o.Username
}
return
if o.Alias != "" {
// assert o.aliasUser != nil
return o.aliasUser.Username
}
return ""
}

func (o *WebDavStorageOption) GetPassword() string {
if o.Password != "" {
return o.Password
}
if o.Alias != "" {
// assert o.aliasUser != nil
return o.aliasUser.Password
}
return ""
}

type WebDavStorage struct {
Expand All @@ -89,7 +116,7 @@ func init() {
}

func (s *WebDavStorage) String() string {
return fmt.Sprintf("<WebDavStorage endpoint=%q user=%s>", s.opt.EndPoint, s.opt.Username)
return fmt.Sprintf("<WebDavStorage endpoint=%q user=%s>", s.opt.GetEndPoint(), s.opt.GetUsername())
}

func (s *WebDavStorage) Options() any {
Expand All @@ -102,17 +129,17 @@ func (s *WebDavStorage) SetOptions(newOpts any) {

func (s *WebDavStorage) Init(ctx context.Context) (err error) {
if alias := s.opt.Alias; alias != "" {
user := config.WebdavUsers[alias]
if user == nil {
user, ok := config.WebdavUsers[alias]
if !ok {
logErrorf("Web dav user %q does not exists", alias)
os.Exit(1)
}
s.opt.WebDavUser = user
s.opt.aliasUser = user
}

if s.cli, err = webdav.NewClient(
webdav.HTTPClientWithBasicAuth(http.DefaultClient, s.opt.Username, s.opt.Password),
s.opt.EndPoint); err != nil {
webdav.HTTPClientWithBasicAuth(http.DefaultClient, s.opt.GetUsername(), s.opt.GetPassword()),
s.opt.GetEndPoint()); err != nil {
return
}

Expand Down Expand Up @@ -166,7 +193,7 @@ var noRedirectCli = &http.Client{
}

func (s *WebDavStorage) serveWithRedirectIfPossible(rw http.ResponseWriter, req *http.Request, size int64, path string) (int64, error) {
target, err := url.JoinPath(s.opt.EndPoint, path)
target, err := url.JoinPath(s.opt.GetEndPoint(), path)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -228,7 +255,7 @@ func copyHeader(key string, dst, src http.Header) {
}

func (s *WebDavStorage) ServeDownload(rw http.ResponseWriter, req *http.Request, hash string, size int64) (int64, error) {
target, err := url.JoinPath(s.opt.EndPoint, "download", hash[0:2], hash)
target, err := url.JoinPath(s.opt.GetEndPoint(), "download", hash[0:2], hash)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -286,7 +313,7 @@ func (s *WebDavStorage) ServeMeasure(rw http.ResponseWriter, req *http.Request,
if err := s.createMeasureFile(req.Context(), size); err != nil {
return err
}
target, err := url.JoinPath(s.opt.EndPoint, "measure", strconv.Itoa(size))
target, err := url.JoinPath(s.opt.GetEndPoint(), "measure", strconv.Itoa(size))
if err != nil {
return err
}
Expand Down

0 comments on commit 60c903a

Please sign in to comment.