Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion config/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ package config

import "encoding/json"

func fromJson(content []byte,v interface{}) error {
// UnmarshalJSON parses the JSON-encoded data and stores the result
// in the value pointed to by v.
func UnmarshalJSON(content []byte, v interface{}) error {
return json.Unmarshal(content, v)
}

// MarshalJSON returns the JSON encoding of v.
func MarshalJSON(v interface{}) (out []byte, err error) {
return json.Marshal(v)
}

// MarshalJSONString returns the JSON encoding string format of v.
func MarshalJSONString(v interface{}) (out string) {
marshal, err := json.Marshal(v)
if err != nil {
return ""
}
return string(marshal)
}
22 changes: 20 additions & 2 deletions config/config_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import (
"encoding/xml"
)

func fromXml(content []byte,v interface{}) error {
// UnmarshalXML parses the XML-encoded data and stores the result in
// the value pointed to by v, which must be an arbitrary struct,
// slice, or string. Well-formed data that does not fit into v is
// discarded.
func UnmarshalXML(content []byte, v interface{}) error {
return xml.Unmarshal(content, v)
}
}

// MarshalXML returns the XML encoding of v.
func MarshalXML(v interface{}) (out []byte, err error) {
return xml.Marshal(v)
}

// MarshalXMLString returns the XML encoding string format of v.
func MarshalXMLString(v interface{}) (out string) {
marshal, err := xml.Marshal(v)
if err != nil {
return ""
}
return string(marshal)
}
41 changes: 41 additions & 0 deletions config/config_yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import (
"gopkg.in/yaml.v2"
)

// UnmarshalYaml decodes the first document found within the in byte slice
// and assigns decoded values into the out value.
// For example:
//
// type T struct {
// F int `yaml:"a,omitempty"`
// B int
// }
// var t T
// yaml.Unmarshal([]byte("a: 1\nb: 2"), &t)
func UnmarshalYaml(content []byte, v interface{}) error {
return yaml.Unmarshal(content, v)
}

// MarshalYaml Marshal serializes the value provided into a YAML document.
// For example:
//
// type T struct {
// F int "a,omitempty"
// B int
// }
// yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
// yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
func MarshalYaml(v interface{}) (out []byte, err error) {
return yaml.Marshal(v)
}

// MarshalYamlString returns the Ymal encoding string format of v.
func MarshalYamlString(v interface{}) (out string) {
marshal, err := yaml.Marshal(v)
if err != nil {
return ""
}
return string(marshal)
}
21 changes: 13 additions & 8 deletions config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"github.com/devfeel/dotweb/core"
"github.com/devfeel/dotweb/framework/file"
"io/ioutil"
//"time"
)

type (
Config struct {
XMLName xml.Name `xml:"config" json:"-"`
XMLName xml.Name `xml:"config" json:"-" yaml:"-"`
App *AppNode `xml:"app"`
AppSets []*AppSetNode `xml:"appset>set"`
Offline *OfflineNode `xml:"offline"`
Expand All @@ -20,7 +19,7 @@ type (
Routers []*RouterNode `xml:"routers>router"`
Groups []*GroupNode `xml:"groups>group"`
Middlewares []*MiddlewareNode `xml:"middlewares>middleware"`
AppSetConfig *core.ItemContext
AppSetConfig *core.ItemContext `json:"-" yaml:"-"`
}
OfflineNode struct {
Offline bool `xml:"offline,attr"` //是否维护,默认false
Expand All @@ -47,7 +46,7 @@ type (
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` //设置是否忽略favicon.ico请求,若设置,网站将把所有favicon.ico请求直接空返回
EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
Port int `xml:"port,attr"` //端口
EnabledTLS bool `xml:"enabledtls,attr"` //是否启用TLS模式
TLSCertFile string `xml:"tlscertfile,attr"` //TLS模式下Certificate证书文件地址
Expand Down Expand Up @@ -89,6 +88,7 @@ type (
const (
ConfigType_Xml = "xml"
ConfigType_Json = "json"
ConfigType_Yaml = "yaml"
)

func NewConfig() *Config {
Expand Down Expand Up @@ -155,11 +155,16 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
if len(confType) > 0 && confType[0] == ConfigType_Json {
cType = ConfigType_Json
}
if len(confType) > 0 && confType[0] == ConfigType_Yaml {
cType = ConfigType_Yaml
}

if cType == ConfigType_Xml {
config, err = initConfig(realFile, cType, fromXml)
config, err = initConfig(realFile, cType, UnmarshalXML)
} else if cType == ConfigType_Yaml {
config, err = initConfig(realFile, cType, UnmarshalYaml)
} else {
config, err = initConfig(realFile, cType, fromJson)
config, err = initConfig(realFile, cType, UnmarshalJSON)
}

if err != nil {
Expand Down Expand Up @@ -198,14 +203,14 @@ func dealConfigDefaultSet(c *Config) {

}

func initConfig(configFile string, ctType string, f func([]byte, interface{}) error) (*Config, error) {
func initConfig(configFile string, ctType string, parser func([]byte, interface{}) error) (*Config, error) {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.New("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]无法解析 - " + err.Error())
}

var config *Config
err = f(content, &config)
err = parser(content, &config)
if err != nil {
return nil, errors.New("DotWeb:Config:initConfig 当前cType:" + ctType + " 配置文件[" + configFile + "]解析失败 - " + err.Error())
}
Expand Down
5 changes: 2 additions & 3 deletions example/config/dotweb.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<config>
<app logpath="d:/gotmp/" enabledlog="true" runmode="development"/>
<offline offline="false" offlinetext="server is offline!" offlineurl="" />
<server isrun="true" indexpage="index.html" port="8080" enabledgzip="false" enabledlistdir="false" enabledautohead="true" requesttimeout="30000/>
<session enabled="true" mode="runtime" timeout="20"/>
<server isrun="true" indexpage="index.html" port="8080" enabledgzip="false" enabledlistdir="false" enabledautohead="true" requesttimeout="30000"/>
<session enabled="true" mode="runtime" timeout="20" />
<appset>
<set key="set1" value="1" />
<set key="set2" value="2" />
Expand Down
145 changes: 145 additions & 0 deletions example/config/dotweb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"App": {
"LogPath": "d:/gotmp/",
"EnabledLog": true,
"RunMode": "development",
"PProfPort": 0,
"EnabledPProf": false
},
"AppSets": [{
"Key": "set1",
"Value": "1"
}, {
"Key": "set2",
"Value": "2"
}, {
"Key": "set3",
"Value": "3"
}, {
"Key": "set4",
"Value": "4"
}],
"Offline": {
"Offline": false,
"OfflineText": "",
"OfflineUrl": ""
},
"Server": {
"EnabledListDir": false,
"EnabledRequestID": false,
"EnabledGzip": false,
"EnabledAutoHEAD": true,
"EnabledAutoCORS": false,
"EnabledIgnoreFavicon": false,
"EnabledBindUseJsonTag": false,
"Port": 8080,
"EnabledTLS": false,
"TLSCertFile": "",
"TLSKeyFile": "",
"IndexPage": "index.html",
"EnabledDetailRequestData": false
},
"Session": {
"EnabledSession": true,
"SessionMode": "runtime",
"Timeout": 20,
"ServerIP": "",
"UserName": "",
"Password": ""
},
"Routers": [{
"Method": "GET",
"Path": "/index",
"HandlerName": "Index",
"Middlewares": [{
"Name": "urllog",
"IsUse": true
}],
"IsUse": true
}, {
"Method": "GET",
"Path": "/index2",
"HandlerName": "Index",
"Middlewares": [{
"Name": "urllog",
"IsUse": true
}],
"IsUse": true
}, {
"Method": "GET",
"Path": "/index3",
"HandlerName": "Index",
"Middlewares": [{
"Name": "urllog",
"IsUse": true
}],
"IsUse": true
}, {
"Method": "GET",
"Path": "/redirect",
"HandlerName": "Redirect",
"Middlewares": null,
"IsUse": true
}, {
"Method": "GET",
"Path": "/error",
"HandlerName": "Error",
"Middlewares": null,
"IsUse": true
}, {
"Method": "GET",
"Path": "/panic",
"HandlerName": "Panic",
"Middlewares": null,
"IsUse": true
}, {
"Method": "GET",
"Path": "/appset",
"HandlerName": "appset",
"Middlewares": null,
"IsUse": true
}],
"Groups": [{
"Path": "/admin",
"Routers": [{
"Method": "GET",
"Path": "/login",
"HandlerName": "Login",
"Middlewares": [{
"Name": "urllog",
"IsUse": true
}],
"IsUse": true
}, {
"Method": "GET",
"Path": "/login3",
"HandlerName": "Login",
"Middlewares": null,
"IsUse": true
}, {
"Method": "GET",
"Path": "/logout",
"HandlerName": "Logout",
"Middlewares": null,
"IsUse": true
}, {
"Method": "GET",
"Path": "/login2",
"HandlerName": "Login",
"Middlewares": null,
"IsUse": true
}],
"Middlewares": [{
"Name": "grouplog",
"IsUse": true
}, {
"Name": "simpleauth",
"IsUse": true
}],
"IsUse": true
}],
"Middlewares": [{
"Name": "applog",
"IsUse": true
}]
}
Loading