github.com/v2fly/v2ray-core/v4@v4.45.2/main/jsonem/jsonem.go (about)

     1  package jsonem
     2  
     3  import (
     4  	"io"
     5  
     6  	core "github.com/v2fly/v2ray-core/v4"
     7  	"github.com/v2fly/v2ray-core/v4/common"
     8  	"github.com/v2fly/v2ray-core/v4/common/cmdarg"
     9  	"github.com/v2fly/v2ray-core/v4/infra/conf"
    10  	"github.com/v2fly/v2ray-core/v4/infra/conf/serial"
    11  	"github.com/v2fly/v2ray-core/v4/main/confloader"
    12  )
    13  
    14  func init() {
    15  	common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
    16  		Name:      "JSON",
    17  		Extension: []string{"json"},
    18  		Loader: func(input interface{}) (*core.Config, error) {
    19  			switch v := input.(type) {
    20  			case cmdarg.Arg:
    21  				cf := &conf.Config{}
    22  				for i, arg := range v {
    23  					newError("Reading config: ", arg).AtInfo().WriteToLog()
    24  					r, err := confloader.LoadConfig(arg)
    25  					common.Must(err)
    26  					c, err := serial.DecodeJSONConfig(r)
    27  					common.Must(err)
    28  					if i == 0 {
    29  						// This ensure even if the muti-json parser do not support a setting,
    30  						// It is still respected automatically for the first configure file
    31  						*cf = *c
    32  						continue
    33  					}
    34  					cf.Override(c, arg)
    35  				}
    36  				return cf.Build()
    37  			case io.Reader:
    38  				return serial.LoadJSONConfig(v)
    39  			default:
    40  				return nil, newError("unknow type")
    41  			}
    42  		},
    43  	}))
    44  }