Skip to content

Commit

Permalink
support include http/https url
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuzuki616 committed Sep 22, 2023
1 parent cecfffd commit a1e14d0
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions conf/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package conf

import (
"fmt"
"io"
"os"

"github.com/InazumaV/V2bX/common/json5"
"github.com/goccy/go-json"
"io"
"net/http"
"os"
"strings"
)

type NodeConfig struct {
Expand Down Expand Up @@ -36,12 +37,29 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) {
return err
}
if len(rn.Include) != 0 {
f, err := os.Open(rn.Include)
if err != nil {
return fmt.Errorf("open include file error: %s", err)
file, _ := strings.CutPrefix(rn.Include, ":")
switch file {
case "http", "https":
rsp, err := http.Get(file)
if err != nil {
return err
}
defer rsp.Body.Close()
data, err = io.ReadAll(json5.NewTrimNodeReader(rsp.Body))
if err != nil {
return fmt.Errorf("open include file error: %s", err)
}
default:
f, err := os.Open(rn.Include)
if err != nil {
return fmt.Errorf("open include file error: %s", err)
}
defer f.Close()
data, err = io.ReadAll(json5.NewTrimNodeReader(f))
if err != nil {
return fmt.Errorf("open include file error: %s", err)
}
}
defer f.Close()
data, err = io.ReadAll(json5.NewTrimNodeReader(f))
err = json.Unmarshal(data, &rn)
if err != nil {
return fmt.Errorf("unmarshal include file error: %s", err)
Expand Down

0 comments on commit a1e14d0

Please sign in to comment.