From ba4777b28d097f1c99fd327272b6fa445911b609 Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Sat, 20 Jan 2018 23:54:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?####=20Version=201.4.5=20*=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eyaml=E6=A0=BC=E5=BC=8F=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81=EF=BC=8C=E5=85=B7=E4=BD=93=E5=8F=82?= =?UTF-8?q?=E8=80=83=20example/config/dotweb.yaml=20*=20config=E6=96=B0?= =?UTF-8?q?=E5=A2=9EUnmarshalYaml\MarshalYaml\MarshalYamlString=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E9=92=88=E5=AF=B9Yaml=E7=9A=84=E5=B8=B8?= =?UTF-8?q?=E8=A7=84=E5=A4=84=E7=90=86=20*=20config=E6=96=B0=E5=A2=9EUnmar?= =?UTF-8?q?shalXml\MarshalXml\MarshalXmlString=EF=BC=8C=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E9=92=88=E5=AF=B9Xml=E7=9A=84=E5=B8=B8=E8=A7=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=20*=20config=E6=96=B0=E5=A2=9EUnmarshalJson\MarshalJs?= =?UTF-8?q?on\MarshalJsonString=EF=BC=8C=E6=8F=90=E4=BE=9B=E9=92=88?= =?UTF-8?q?=E5=AF=B9Json=E7=9A=84=E5=B8=B8=E8=A7=84=E5=A4=84=E7=90=86=20*?= =?UTF-8?q?=20UploadFile=E6=96=B0=E5=A2=9EBytes=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=BF=94=E5=9B=9E=E4=B8=8A=E4=BC=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=AC=E8=BA=AB=20*=20=E5=AE=8C=E5=96=84=20example/?= =?UTF-8?q?config=20*=20=E7=A7=BB=E9=99=A4=20example/session=EF=BC=8C?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E6=9B=B4=E5=A4=9A=E7=A4=BA=E4=BE=8B=EF=BC=8C?= =?UTF-8?q?=E8=AF=B7=E7=A7=BB=E6=AD=A5https://github.com/devfeel/dotweb-ex?= =?UTF-8?q?ample=20*=202018-01-20=2023:40?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config_json.go | 18 +++- config/config_xml.go | 22 +++- config/config_yaml.go | 41 ++++++++ config/configs.go | 21 ++-- example/config/dotweb.conf | 5 +- example/config/dotweb.json | 145 +++++++++++++++++++++++++++ example/config/dotweb.json.conf | 171 -------------------------------- example/config/dotweb.yaml | 116 ++++++++++++++++++++++ example/config/main.go | 6 +- example/session/main.go | 82 --------------- uploadfile.go | 21 +++- version.MD | 10 ++ 12 files changed, 387 insertions(+), 271 deletions(-) create mode 100644 config/config_yaml.go create mode 100644 example/config/dotweb.json delete mode 100644 example/config/dotweb.json.conf create mode 100644 example/config/dotweb.yaml delete mode 100644 example/session/main.go diff --git a/config/config_json.go b/config/config_json.go index 487aa2f..d020957 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -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) +} diff --git a/config/config_xml.go b/config/config_xml.go index d496e14..6d76a5f 100644 --- a/config/config_xml.go +++ b/config/config_xml.go @@ -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) -} \ No newline at end of file +} + +// 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) +} diff --git a/config/config_yaml.go b/config/config_yaml.go new file mode 100644 index 0000000..3027472 --- /dev/null +++ b/config/config_yaml.go @@ -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) +} diff --git a/config/configs.go b/config/configs.go index ee58bff..47698e0 100644 --- a/config/configs.go +++ b/config/configs.go @@ -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"` @@ -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 @@ -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证书文件地址 @@ -89,6 +88,7 @@ type ( const ( ConfigType_Xml = "xml" ConfigType_Json = "json" + ConfigType_Yaml = "yaml" ) func NewConfig() *Config { @@ -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 { @@ -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()) } diff --git a/example/config/dotweb.conf b/example/config/dotweb.conf index a446ac3..350975e 100644 --- a/example/config/dotweb.conf +++ b/example/config/dotweb.conf @@ -1,9 +1,8 @@ - - + + diff --git a/example/config/dotweb.json b/example/config/dotweb.json new file mode 100644 index 0000000..b0ef82a --- /dev/null +++ b/example/config/dotweb.json @@ -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": "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/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 + }] +} \ No newline at end of file diff --git a/example/config/dotweb.json.conf b/example/config/dotweb.json.conf deleted file mode 100644 index 4ad69db..0000000 --- a/example/config/dotweb.json.conf +++ /dev/null @@ -1,171 +0,0 @@ -{ - "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": "server is offline!", - "OfflineUrl": "" - }, - "Server": { - "EnabledListDir": false, - "EnabledGzip": false, - "EnabledAutoHEAD": true, - "EnabledAutoCORS": false, - "Port": 8080 - }, - "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": "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/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 - } - ], - "AppSetConfig": {} -} \ No newline at end of file diff --git a/example/config/dotweb.yaml b/example/config/dotweb.yaml new file mode 100644 index 0000000..3c060d5 --- /dev/null +++ b/example/config/dotweb.yaml @@ -0,0 +1,116 @@ +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: [] + isuse: true +- method: GET + path: /error + handlername: Error + middlewares: [] + isuse: true +- method: GET + path: /panic + handlername: Panic + middlewares: [] + isuse: true +- method: GET + path: /appset + handlername: appset + middlewares: [] + 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: [] + isuse: true + - method: GET + path: /logout + handlername: Logout + middlewares: [] + isuse: true + - method: GET + path: /login2 + handlername: Login + middlewares: [] + isuse: true + middlewares: + - name: grouplog + isuse: true + - name: simpleauth + isuse: true + isuse: true +middlewares: +- name: applog + isuse: true \ No newline at end of file diff --git a/example/config/main.go b/example/config/main.go index f291177..7f24e0f 100644 --- a/example/config/main.go +++ b/example/config/main.go @@ -17,14 +17,18 @@ func main() { //注册HttpHandler RegisterHandler(app.HttpServer) + //xml config //appConfig, err := config.InitConfig("d:/gotmp/dotweb.conf") //json config - appConfig, err := config.InitConfig("d:/gotmp/dotweb.json.conf", "json") + //appConfig, err := config.InitConfig("d:/gotmp/dotweb.json", "json") + //yaml config + appConfig, err := config.InitConfig("d:/gotmp/dotweb.yaml", "yaml") if err != nil { fmt.Println("dotweb.InitConfig error => " + fmt.Sprint(err)) return } fmt.Println(jsonutil.GetJsonString(appConfig)) + RegisterMiddlewares(app) err = app.SetConfig(appConfig) diff --git a/example/session/main.go b/example/session/main.go deleted file mode 100644 index 843aeac..0000000 --- a/example/session/main.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "fmt" - "github.com/devfeel/dotweb" - "github.com/devfeel/dotweb/framework/file" - "github.com/devfeel/dotweb/session" - "strconv" -) - -func main() { - //初始化DotServer - app := dotweb.New() - - //设置dotserver日志目录 - app.SetLogPath(file.GetCurrentDirectory()) - - //设置Session开关 - app.HttpServer.SetEnabledSession(true) - - //设置Session配置 - //runtime mode - //app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig()) - //redis mode - app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("redis://192.168.8.175:6379/1")) - - //设置路由 - InitRoute(app.HttpServer) - - // 开始服务 - port := 8080 - fmt.Println("dotweb.StartServer => " + strconv.Itoa(port)) - err := app.StartServer(port) - fmt.Println("dotweb.StartServer error => ", err) -} - -type UserInfo struct { - UserName string - NickName string -} - -func TestSession(ctx dotweb.Context) error { - - user := UserInfo{UserName: "test", NickName: "testName"} - var userRead UserInfo - - ctx.WriteString("welcome to dotweb - CreateSession - sessionid=> "+ctx.SessionID(), "\r\n") - err := ctx.Session().Set("username", user) - if err != nil { - ctx.WriteString("session set error => ", err, "\r\n") - } - c := ctx.Session().Get("username") - if c != nil { - userRead = c.(UserInfo) - } else { - ctx.WriteString("session read failed, get nil", "\r\n") - } - - return ctx.WriteString("userinfo=>" + fmt.Sprintln(userRead)) - return err -} - -func TestReadSession(ctx dotweb.Context) error { - - var userRead UserInfo - - ctx.WriteString("welcome to dotweb - ReadSession - sessionid=> "+ctx.SessionID(), "\r\n") - - c := ctx.Session().Get("username") - if c != nil { - userRead = c.(UserInfo) - } else { - ctx.WriteString("session read failed, get nil", "\r\n") - } - - return ctx.WriteString("userinfo=>" + fmt.Sprintln(userRead)) -} - -func InitRoute(server *dotweb.HttpServer) { - server.Router().GET("/", TestSession) - server.Router().GET("/read", TestReadSession) -} diff --git a/uploadfile.go b/uploadfile.go index 29c4e0a..48d9cbb 100644 --- a/uploadfile.go +++ b/uploadfile.go @@ -1,8 +1,8 @@ package dotweb import ( + "bytes" "errors" - "io" "mime/multipart" "os" "path/filepath" @@ -14,6 +14,7 @@ type UploadFile struct { fileExt string //file extensions fileName string fileSize int64 + content []byte } func NewUploadFile(file multipart.File, header *multipart.FileHeader) *UploadFile { @@ -22,6 +23,7 @@ func NewUploadFile(file multipart.File, header *multipart.FileHeader) *UploadFil Header: header, fileName: header.Filename, fileExt: filepath.Ext(header.Filename), //update for issue #99 + content: parseFileToBytes(file), } } @@ -46,7 +48,7 @@ func (f *UploadFile) Size() int64 { } //save file in server-local with filename -func (f *UploadFile) SaveFile(fileName string) (size int64, err error) { +func (f *UploadFile) SaveFile(fileName string) (size int, err error) { size = 0 if fileName == "" { return size, errors.New("filename not allow empty") @@ -57,7 +59,9 @@ func (f *UploadFile) SaveFile(fileName string) (size int64, err error) { return size, err } defer fileWriter.Close() - size, err = io.Copy(fileWriter, f.File) + f.File.Read(f.content) + //size, err = io.Copy(fileWriter, f.File) + size, err = fileWriter.Write(f.content) return size, err } @@ -65,3 +69,14 @@ func (f *UploadFile) SaveFile(fileName string) (size int64, err error) { func (f *UploadFile) GetFileExt() string { return f.fileExt } + +// Bytes returns a slice of byte hoding the UploadFile.File +func (f *UploadFile) Bytes() []byte { + return f.content +} + +func parseFileToBytes(file multipart.File) []byte { + buf := new(bytes.Buffer) + buf.ReadFrom(file) + return buf.Bytes() +} diff --git a/version.MD b/version.MD index 6cccea7..7cda957 100644 --- a/version.MD +++ b/version.MD @@ -1,5 +1,15 @@ ## dotweb版本记录: +#### Version 1.4.5 +* 新增yaml格式配置文件支持,具体参考 example/config/dotweb.yaml +* config新增UnmarshalYaml\MarshalYaml\MarshalYamlString,提供针对Yaml的常规处理 +* config新增UnmarshalXml\MarshalXml\MarshalXmlString,提供针对Xml的常规处理 +* config新增UnmarshalJson\MarshalJson\MarshalJsonString,提供针对Json的常规处理 +* UploadFile新增Bytes接口,用于返回上传文件本身 +* 完善 example/config +* 移除 example/session,查看更多示例,请移步https://github.com/devfeel/dotweb-example +* 2018-01-20 23:40 + #### Version 1.4.4 * 调整Bind模块内部获取req.Body为HttpContext.PostBody,避免因为Bind后无法再次获取Post内容 * 完善debug log 输出 From c38bb77f776710d406633c1b637044970284cba6 Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Sun, 21 Jan 2018 00:04:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?####=20Version=201.4.5=20*=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eyaml=E6=A0=BC=E5=BC=8F=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81=EF=BC=8C=E5=85=B7=E4=BD=93=E5=8F=82?= =?UTF-8?q?=E8=80=83=20example/config/dotweb.yaml=20*=20config=E6=96=B0?= =?UTF-8?q?=E5=A2=9EUnmarshalYaml\MarshalYaml\MarshalYamlString=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E9=92=88=E5=AF=B9Yaml=E7=9A=84=E5=B8=B8?= =?UTF-8?q?=E8=A7=84=E5=A4=84=E7=90=86=20*=20config=E6=96=B0=E5=A2=9EUnmar?= =?UTF-8?q?shalXML\MarshalXML\MarshalXMLString=EF=BC=8C=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E9=92=88=E5=AF=B9Xml=E7=9A=84=E5=B8=B8=E8=A7=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=20*=20config=E6=96=B0=E5=A2=9EUnmarshalJSON\MarshalJS?= =?UTF-8?q?ON\MarshalJSONString=EF=BC=8C=E6=8F=90=E4=BE=9B=E9=92=88?= =?UTF-8?q?=E5=AF=B9Json=E7=9A=84=E5=B8=B8=E8=A7=84=E5=A4=84=E7=90=86=20*?= =?UTF-8?q?=20UploadFile=E6=96=B0=E5=A2=9EBytes=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=BF=94=E5=9B=9E=E4=B8=8A=E4=BC=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=AC=E8=BA=AB=20*=20=E5=AE=8C=E5=96=84=20example/?= =?UTF-8?q?config=20*=20=E7=A7=BB=E9=99=A4=20example/session=EF=BC=8C?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E6=9B=B4=E5=A4=9A=E7=A4=BA=E4=BE=8B=EF=BC=8C?= =?UTF-8?q?=E8=AF=B7=E7=A7=BB=E6=AD=A5https://github.com/devfeel/dotweb-ex?= =?UTF-8?q?ample=20*=202018-01-20=2023:40?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config_json.go | 6 +++--- config/config_xml.go | 6 +++--- config/configs.go | 4 ++-- version.MD | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/config/config_json.go b/config/config_json.go index d020957..2bc0787 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -4,17 +4,17 @@ import "encoding/json" // UnmarshalJson parses the JSON-encoded data and stores the result // in the value pointed to by v. -func UnmarshalJson(content []byte, v interface{}) error { +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) { +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) { +func MarshalJSONString(v interface{}) (out string) { marshal, err := json.Marshal(v) if err != nil { return "" diff --git a/config/config_xml.go b/config/config_xml.go index 6d76a5f..6f0bc99 100644 --- a/config/config_xml.go +++ b/config/config_xml.go @@ -8,17 +8,17 @@ import ( // 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 { +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) { +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) { +func MarshalXMLString(v interface{}) (out string) { marshal, err := xml.Marshal(v) if err != nil { return "" diff --git a/config/configs.go b/config/configs.go index 47698e0..129c69b 100644 --- a/config/configs.go +++ b/config/configs.go @@ -160,11 +160,11 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err } if cType == ConfigType_Xml { - config, err = initConfig(realFile, cType, UnmarshalXml) + config, err = initConfig(realFile, cType, UnmarshalXML) } else if cType == ConfigType_Yaml { config, err = initConfig(realFile, cType, UnmarshalYaml) } else { - config, err = initConfig(realFile, cType, UnmarshalJson) + config, err = initConfig(realFile, cType, UnmarshalJSON) } if err != nil { diff --git a/version.MD b/version.MD index 7cda957..163290a 100644 --- a/version.MD +++ b/version.MD @@ -3,8 +3,8 @@ #### Version 1.4.5 * 新增yaml格式配置文件支持,具体参考 example/config/dotweb.yaml * config新增UnmarshalYaml\MarshalYaml\MarshalYamlString,提供针对Yaml的常规处理 -* config新增UnmarshalXml\MarshalXml\MarshalXmlString,提供针对Xml的常规处理 -* config新增UnmarshalJson\MarshalJson\MarshalJsonString,提供针对Json的常规处理 +* config新增UnmarshalXML\MarshalXML\MarshalXMLString,提供针对Xml的常规处理 +* config新增UnmarshalJSON\MarshalJSON\MarshalJSONString,提供针对Json的常规处理 * UploadFile新增Bytes接口,用于返回上传文件本身 * 完善 example/config * 移除 example/session,查看更多示例,请移步https://github.com/devfeel/dotweb-example From 8ef96183f387013f549ac833f76050355730c49c Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Sun, 21 Jan 2018 00:09:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?####=20Version=201.4.5=20*=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eyaml=E6=A0=BC=E5=BC=8F=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81=EF=BC=8C=E5=85=B7=E4=BD=93=E5=8F=82?= =?UTF-8?q?=E8=80=83=20example/config/dotweb.yaml=20*=20config=E6=96=B0?= =?UTF-8?q?=E5=A2=9EUnmarshalYaml\MarshalYaml\MarshalYamlString=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E9=92=88=E5=AF=B9Yaml=E7=9A=84=E5=B8=B8?= =?UTF-8?q?=E8=A7=84=E5=A4=84=E7=90=86=20*=20config=E6=96=B0=E5=A2=9EUnmar?= =?UTF-8?q?shalXML\MarshalXML\MarshalXMLString=EF=BC=8C=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E9=92=88=E5=AF=B9Xml=E7=9A=84=E5=B8=B8=E8=A7=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=20*=20config=E6=96=B0=E5=A2=9EUnmarshalJSON\MarshalJS?= =?UTF-8?q?ON\MarshalJSONString=EF=BC=8C=E6=8F=90=E4=BE=9B=E9=92=88?= =?UTF-8?q?=E5=AF=B9Json=E7=9A=84=E5=B8=B8=E8=A7=84=E5=A4=84=E7=90=86=20*?= =?UTF-8?q?=20UploadFile=E6=96=B0=E5=A2=9EBytes=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=BF=94=E5=9B=9E=E4=B8=8A=E4=BC=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=AC=E8=BA=AB=20*=20=E5=AE=8C=E5=96=84=20example/?= =?UTF-8?q?config=20*=20=E7=A7=BB=E9=99=A4=20example/session=EF=BC=8C?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E6=9B=B4=E5=A4=9A=E7=A4=BA=E4=BE=8B=EF=BC=8C?= =?UTF-8?q?=E8=AF=B7=E7=A7=BB=E6=AD=A5https://github.com/devfeel/dotweb-ex?= =?UTF-8?q?ample=20*=202018-01-20=2023:40?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config_json.go | 6 +++--- config/config_xml.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/config_json.go b/config/config_json.go index 2bc0787..09de0b7 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -2,18 +2,18 @@ package config import "encoding/json" -// UnmarshalJson parses the JSON-encoded data and stores the result +// 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. +// 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. +// MarshalJSONString returns the JSON encoding string format of v. func MarshalJSONString(v interface{}) (out string) { marshal, err := json.Marshal(v) if err != nil { diff --git a/config/config_xml.go b/config/config_xml.go index 6f0bc99..ae72794 100644 --- a/config/config_xml.go +++ b/config/config_xml.go @@ -4,7 +4,7 @@ import ( "encoding/xml" ) -// UnmarshalXml parses the XML-encoded data and stores the result in +// 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. @@ -12,12 +12,12 @@ func UnmarshalXML(content []byte, v interface{}) error { return xml.Unmarshal(content, v) } -// MarshalXml returns the XML encoding of 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. +// MarshalXMLString returns the XML encoding string format of v. func MarshalXMLString(v interface{}) (out string) { marshal, err := xml.Marshal(v) if err != nil {