@@ -6,12 +6,11 @@ import (
66 "github.com/devfeel/dotweb/core"
77 "github.com/devfeel/dotweb/framework/file"
88 "io/ioutil"
9- //"time"
109)
1110
1211type (
1312 Config struct {
14- XMLName xml.Name `xml:"config" json:"-"`
13+ XMLName xml.Name `xml:"config" json:"-" yaml:"-" `
1514 App * AppNode `xml:"app"`
1615 AppSets []* AppSetNode `xml:"appset>set"`
1716 Offline * OfflineNode `xml:"offline"`
2019 Routers []* RouterNode `xml:"routers>router"`
2120 Groups []* GroupNode `xml:"groups>group"`
2221 Middlewares []* MiddlewareNode `xml:"middlewares>middleware"`
23- AppSetConfig * core.ItemContext
22+ AppSetConfig * core.ItemContext `json:"-" yaml:"-"`
2423 }
2524 OfflineNode struct {
2625 Offline bool `xml:"offline,attr"` //是否维护,默认false
4746 EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
4847 EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
4948 EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` //设置是否忽略favicon.ico请求,若设置,网站将把所有favicon.ico请求直接空返回
50- EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
49+ EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
5150 Port int `xml:"port,attr"` //端口
5251 EnabledTLS bool `xml:"enabledtls,attr"` //是否启用TLS模式
5352 TLSCertFile string `xml:"tlscertfile,attr"` //TLS模式下Certificate证书文件地址
8988const (
9089 ConfigType_Xml = "xml"
9190 ConfigType_Json = "json"
91+ ConfigType_Yaml = "yaml"
9292)
9393
9494func NewConfig () * Config {
@@ -155,11 +155,16 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
155155 if len (confType ) > 0 && confType [0 ] == ConfigType_Json {
156156 cType = ConfigType_Json
157157 }
158+ if len (confType ) > 0 && confType [0 ] == ConfigType_Yaml {
159+ cType = ConfigType_Yaml
160+ }
158161
159162 if cType == ConfigType_Xml {
160- config , err = initConfig (realFile , cType , fromXml )
163+ config , err = initConfig (realFile , cType , UnmarshalXML )
164+ } else if cType == ConfigType_Yaml {
165+ config , err = initConfig (realFile , cType , UnmarshalYaml )
161166 } else {
162- config , err = initConfig (realFile , cType , fromJson )
167+ config , err = initConfig (realFile , cType , UnmarshalJSON )
163168 }
164169
165170 if err != nil {
@@ -198,14 +203,14 @@ func dealConfigDefaultSet(c *Config) {
198203
199204}
200205
201- func initConfig (configFile string , ctType string , f func ([]byte , interface {}) error ) (* Config , error ) {
206+ func initConfig (configFile string , ctType string , parser func ([]byte , interface {}) error ) (* Config , error ) {
202207 content , err := ioutil .ReadFile (configFile )
203208 if err != nil {
204209 return nil , errors .New ("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]无法解析 - " + err .Error ())
205210 }
206211
207212 var config * Config
208- err = f (content , & config )
213+ err = parser (content , & config )
209214 if err != nil {
210215 return nil , errors .New ("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]解析失败 - " + err .Error ())
211216 }
0 commit comments