From 59318ca18410ccefa3e01beb2cc60720e6040a29 Mon Sep 17 00:00:00 2001 From: john Date: Thu, 8 Nov 2018 14:00:28 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgfile=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=93=8D=E4=BD=9C=E4=B8=BA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?gfpool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gfile/gfile_contents.go | 27 ++++++++++++++------------- g/os/gfpool/gfpool.go | 12 +++++++----- g/os/gfpool/gfpool_test.go | 35 +++++++++++++++++++++++++++++++---- geg/other/test.go | 7 +++++-- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/g/os/gfile/gfile_contents.go b/g/os/gfile/gfile_contents.go index 87738a114..716ee85f8 100644 --- a/g/os/gfile/gfile_contents.go +++ b/g/os/gfile/gfile_contents.go @@ -7,6 +7,7 @@ package gfile import ( + "gitee.com/johng/gf/g/os/gfpool" "io" "io/ioutil" "os" @@ -15,8 +16,8 @@ import ( const ( // 方法中涉及到读取的时候的缓冲大小 gREAD_BUFFER = 1024 - // 方法中涉及到文件指针池的默认缓存时间(秒) - gFILE_POOL_EXPIRE = 60 + // 方法中涉及到文件指针池的默认缓存时间(毫秒) + gFILE_POOL_EXPIRE = 60000 ) // (文本)读取文件内容 @@ -43,7 +44,7 @@ func putContents(path string, data []byte, flag int, perm int) error { } } // 创建/打开文件 - f, err := OpenWithFlagPerm(path, flag, perm) + f, err := gfpool.Open(path, flag, os.FileMode(perm), gFILE_POOL_EXPIRE) if err != nil { return err } @@ -82,11 +83,11 @@ func PutBinContentsAppend(path string, content []byte) error { } // 获得文件内容下一个指定字节的位置 -func GetNextCharOffset(file *os.File, char byte, start int64) int64 { +func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64 { buffer := make([]byte, gREAD_BUFFER) offset := start for { - if n, err := file.ReadAt(buffer, offset); n > 0 { + if n, err := reader.ReadAt(buffer, offset); n > 0 { for i := 0; i < n; i++ { if buffer[i] == char { return int64(i) + offset @@ -102,7 +103,7 @@ func GetNextCharOffset(file *os.File, char byte, start int64) int64 { // 获得文件内容下一个指定字节的位置 func GetNextCharOffsetByPath(path string, char byte, start int64) int64 { - if f, err := OpenWithFlagPerm(path, os.O_RDONLY, gDEFAULT_PERM); err == nil { + if f, err := gfpool.Open(path, os.O_RDONLY, gDEFAULT_PERM, gFILE_POOL_EXPIRE); err == nil { defer f.Close() return GetNextCharOffset(f, char, start) } else { @@ -112,16 +113,16 @@ func GetNextCharOffsetByPath(path string, char byte, start int64) int64 { } // 获得文件内容直到下一个指定字节的位置(返回值包含该位置字符内容) -func GetBinContentsTilChar(file *os.File, char byte, start int64) ([]byte, int64) { - if offset := GetNextCharOffset(file, char, start); offset != -1 { - return GetBinContentsByTwoOffsets(file, start, offset + 1), offset +func GetBinContentsTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64) { + if offset := GetNextCharOffset(reader, char, start); offset != -1 { + return GetBinContentsByTwoOffsets(reader, start, offset + 1), offset } return nil, -1 } // 获得文件内容直到下一个指定字节的位置(返回值包含该位置字符内容) func GetBinContentsTilCharByPath(path string, char byte, start int64) ([]byte, int64) { - if f, err := OpenWithFlagPerm(path, os.O_RDONLY, gDEFAULT_PERM); err == nil { + if f, err := gfpool.Open(path, os.O_RDONLY, gDEFAULT_PERM, gFILE_POOL_EXPIRE); err == nil { defer f.Close() return GetBinContentsTilChar(f, char, start) } else { @@ -131,9 +132,9 @@ func GetBinContentsTilCharByPath(path string, char byte, start int64) ([]byte, i } // 获得文件内容中两个offset之间的内容 [start, end) -func GetBinContentsByTwoOffsets(file *os.File, start int64, end int64) []byte { +func GetBinContentsByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte { buffer := make([]byte, end - start) - if _, err := file.ReadAt(buffer, start); err != nil { + if _, err := reader.ReadAt(buffer, start); err != nil { return nil } return buffer @@ -141,7 +142,7 @@ func GetBinContentsByTwoOffsets(file *os.File, start int64, end int64) []byte { // 获得文件内容中两个offset之间的内容 [start, end) func GetBinContentsByTwoOffsetsByPath(path string, start int64, end int64) []byte { - if f, err := OpenWithFlagPerm(path, os.O_RDONLY, gDEFAULT_PERM); err == nil { + if f, err := gfpool.Open(path, os.O_RDONLY, gDEFAULT_PERM, gFILE_POOL_EXPIRE); err == nil { defer f.Close() return GetBinContentsByTwoOffsets(f, start, end) } else { diff --git a/g/os/gfpool/gfpool.go b/g/os/gfpool/gfpool.go index 9e9b73c9f..95a8d7e8c 100644 --- a/g/os/gfpool/gfpool.go +++ b/g/os/gfpool/gfpool.go @@ -28,7 +28,7 @@ type Pool struct { // 文件指针池指针 type File struct { - os.File // 底层文件指针 + *os.File // 底层文件指针 mu sync.RWMutex // 互斥锁 pool *Pool // 所属池 poolid int // 所属池ID,如果池ID不同表示池已经重建,那么该文件指针也应当销毁,不能重新丢到原有的池中 @@ -81,8 +81,8 @@ func newFilePool(p *Pool, path string, flag int, perm os.FileMode, expire int) * if err != nil { return nil, err } - return &File{ - File : *file, + return &File { + File : file, pool : p, poolid : p.id.Val(), flag : flag, @@ -108,7 +108,7 @@ func (p *Pool) File() (*File, error) { if file, err := os.OpenFile(f.path, f.flag, f.perm); err != nil { return nil, err } else { - f.File = *file + f.File = file if stat, err = f.Stat(); err != nil { return nil, err } @@ -127,7 +127,9 @@ func (p *Pool) File() (*File, error) { return nil, err } } else { - f.Seek(0, 0) + if _, err := f.Seek(0, 0); err != nil { + return nil, err + } } if !p.inited.Set(true) { gfsnotify.Add(f.path, func(event *gfsnotify.Event) { diff --git a/g/os/gfpool/gfpool_test.go b/g/os/gfpool/gfpool_test.go index beef6771f..179705c50 100644 --- a/g/os/gfpool/gfpool_test.go +++ b/g/os/gfpool/gfpool_test.go @@ -5,17 +5,44 @@ import ( "os" ) -func Benchmark_os_Open_Close(b *testing.B) { +func Benchmark_os_Open_Close_ALLFlags(b *testing.B) { for i := 0; i < b.N; i++ { - f, _ := os.OpenFile("/tmp/bench-test", os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0766) + f, _ := os.OpenFile("/tmp/bench-test", os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) f.Close() } } -func Benchmark_gfpool_Open_Close(b *testing.B) { +func Benchmark_gfpool_Open_Close_ALLFlags(b *testing.B) { for i := 0; i < b.N; i++ { - f, _ := Open("/tmp/bench-test", os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0766) + f, _ := Open("/tmp/bench-test", os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666) f.Close() } } +func Benchmark_os_Open_Close_RDWR(b *testing.B) { + for i := 0; i < b.N; i++ { + f, _ := os.OpenFile("/tmp/bench-test", os.O_RDWR, 0666) + f.Close() + } +} + +func Benchmark_gfpool_Open_Close_RDWR(b *testing.B) { + for i := 0; i < b.N; i++ { + f, _ := Open("/tmp/bench-test", os.O_RDWR, 0666) + f.Close() + } +} + +func Benchmark_os_Open_Close_RDONLY(b *testing.B) { + for i := 0; i < b.N; i++ { + f, _ := os.OpenFile("/tmp/bench-test", os.O_RDONLY, 0666) + f.Close() + } +} + +func Benchmark_gfpool_Open_Close_RDONLY(b *testing.B) { + for i := 0; i < b.N; i++ { + f, _ := Open("/tmp/bench-test", os.O_RDONLY, 0666) + f.Close() + } +} \ No newline at end of file diff --git a/geg/other/test.go b/geg/other/test.go index 9a28077b6..f8bfab37a 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,15 +2,18 @@ package main import ( "fmt" + "gitee.com/johng/gf/g/os/gfpool" "os" "time" ) func main() { for { - stat, err := os.Stat("/home/john/temp/log") + f, err := gfpool.Open("/home/john/temp/log", os.O_RDWR, 0666) fmt.Println(err) - fmt.Println(stat.Size()) + _, err = f.WriteString("123") + fmt.Println(err) + //f.Close() time.Sleep(time.Second) } } \ No newline at end of file From feaec67b710780e06b59885533ec8b654a057665 Mon Sep 17 00:00:00 2001 From: john Date: Thu, 8 Nov 2018 18:53:43 +0800 Subject: [PATCH 02/10] =?UTF-8?q?gvalid=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geg/other/test.go | 16 +--------------- geg/util/gvalid/gvalid_result.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 geg/util/gvalid/gvalid_result.go diff --git a/geg/other/test.go b/geg/other/test.go index f8bfab37a..16c5cabe5 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,19 +1,5 @@ package main -import ( - "fmt" - "gitee.com/johng/gf/g/os/gfpool" - "os" - "time" -) - func main() { - for { - f, err := gfpool.Open("/home/john/temp/log", os.O_RDWR, 0666) - fmt.Println(err) - _, err = f.WriteString("123") - fmt.Println(err) - //f.Close() - time.Sleep(time.Second) - } + <- make(chan struct{}) } \ No newline at end of file diff --git a/geg/util/gvalid/gvalid_result.go b/geg/util/gvalid/gvalid_result.go new file mode 100644 index 000000000..ca456b7d7 --- /dev/null +++ b/geg/util/gvalid/gvalid_result.go @@ -0,0 +1,25 @@ +package main + +import ( + "gitee.com/johng/gf/g/util/gutil" + "gitee.com/johng/gf/g/util/gvalid" +) + +func main() { + type User struct { + Name string `gvalid:"name @required|length:6,30#请输入用户名称|用户名称长度非法"` + Pass1 string `gvalid:"password1@required|password3"` + Pass2 string `gvalid:"password2@required|password3|same:password1#||两次密码不一致,请重新输入"` + } + + user := &User{ + Name : "john", + Pass1: "Abc123!@#", + Pass2: "123", + } + + err := gvalid.CheckStruct(user, nil) + gutil.Dump(err) + gutil.Dump(err.String()) + gutil.Dump(err.FirstString()) +} From 0d5d357857e765f214ffd12979f77a385e59a19d Mon Sep 17 00:00:00 2001 From: John Date: Fri, 9 Nov 2018 17:48:57 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geg/net/ghttp/server/template/tpl.go | 16 ------------- geg/net/ghttp/server/template/tpl1/index.html | 11 +++++++++ geg/net/ghttp/server/template/tpl1/tpl1.go | 23 +++++++++++++++++++ geg/other/test.go | 4 +++- 4 files changed, 37 insertions(+), 17 deletions(-) delete mode 100644 geg/net/ghttp/server/template/tpl.go create mode 100644 geg/net/ghttp/server/template/tpl1/index.html create mode 100644 geg/net/ghttp/server/template/tpl1/tpl1.go diff --git a/geg/net/ghttp/server/template/tpl.go b/geg/net/ghttp/server/template/tpl.go deleted file mode 100644 index 87be74714..000000000 --- a/geg/net/ghttp/server/template/tpl.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "gitee.com/johng/gf/g" - "gitee.com/johng/gf/g/net/ghttp" -) - -func main() { - s := g.Server() - s.BindHandler("/", func(r *ghttp.Request){ - content :=`{{if (get "name")}} {{get "name"}} {{else}} NoName {{end}}` - r.Response.WriteTplContent(content, nil) - }) - s.SetPort(8199) - s.Run() -} \ No newline at end of file diff --git a/geg/net/ghttp/server/template/tpl1/index.html b/geg/net/ghttp/server/template/tpl1/index.html new file mode 100644 index 000000000..8a50061ce --- /dev/null +++ b/geg/net/ghttp/server/template/tpl1/index.html @@ -0,0 +1,11 @@ + + + + + {{.title}} + + +

姓名 : {{.name}}

+12311 + + \ No newline at end of file diff --git a/geg/net/ghttp/server/template/tpl1/tpl1.go b/geg/net/ghttp/server/template/tpl1/tpl1.go new file mode 100644 index 000000000..f47bef17d --- /dev/null +++ b/geg/net/ghttp/server/template/tpl1/tpl1.go @@ -0,0 +1,23 @@ +package main + +import ( + "gitee.com/johng/gf/g/frame/gmvc" + "gitee.com/johng/gf/g/net/ghttp" +) +type ControllerIndex struct { + gmvc.Controller +} +func (c *ControllerIndex) Info() { + c.View.Assign("title", "Go Frame 第一个网站") + c.View.Assigns(map[string]interface{}{ + "name" : "很开心1", + "score" : 100, + }) + c.View.Display("index.html") +} +func main() { + s := ghttp.GetServer() + s.BindController("/", new(ControllerIndex)) + s.SetPort(8199) + s.Run() +} diff --git a/geg/other/test.go b/geg/other/test.go index 16c5cabe5..f080414e3 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,5 +1,7 @@ package main +import "fmt" + func main() { - <- make(chan struct{}) + fmt.Print(1) } \ No newline at end of file From 463b35ed0d2739ad2bf9e086bc01ca35a5d246ac Mon Sep 17 00:00:00 2001 From: John Date: Fri, 9 Nov 2018 23:29:31 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgview=E5=86=85=E7=BD=AE?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=9C=A8=E6=B2=A1=E6=9C=89=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=96=87=E4=BB=B6=E6=97=B6=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/ghttp_response_view.go | 8 +++++++- geg/other/test.go | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/g/net/ghttp/ghttp_response_view.go b/g/net/ghttp/ghttp_response_view.go index 7abffa7c3..b57a19b57 100644 --- a/g/net/ghttp/ghttp_response_view.go +++ b/g/net/ghttp/ghttp_response_view.go @@ -65,7 +65,13 @@ func (r *Response) buildInVars(params map[string]interface{}) map[string]interfa if params == nil { params = make(map[string]interface{}) } - params["Config"] = gins.Config().GetMap("") + + c := gins.Config() + if c.GetFilePath() != "" { + params["Config"] = c.GetMap("") + } else { + params["Config"] = nil + } params["Cookie"] = r.request.Cookie.Map() params["Session"] = r.request.Session.Data() return params diff --git a/geg/other/test.go b/geg/other/test.go index f080414e3..dbf231df7 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,7 +1,10 @@ package main -import "fmt" +import ( + "fmt" + "gitee.com/johng/gf/g/frame/gins" +) func main() { - fmt.Print(1) + fmt.Print(gins.Config().GetFilePath()) } \ No newline at end of file From 6ff44ad123404ddd5cb1762cbb95304e135cd98c Mon Sep 17 00:00:00 2001 From: John Date: Sat, 10 Nov 2018 16:18:50 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0HTTP=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=A4=BA=E4=BE=8B=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geg/net/ghttp/server/download/download.go | 15 +++++++++++++++ geg/net/ghttp/server/download/text.txt | 1 + 2 files changed, 16 insertions(+) create mode 100644 geg/net/ghttp/server/download/download.go create mode 100644 geg/net/ghttp/server/download/text.txt diff --git a/geg/net/ghttp/server/download/download.go b/geg/net/ghttp/server/download/download.go new file mode 100644 index 000000000..f54893e57 --- /dev/null +++ b/geg/net/ghttp/server/download/download.go @@ -0,0 +1,15 @@ +package main + +import ( + "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/net/ghttp" +) + +func main() { + s := g.Server() + s.BindHandler("/download", func(r *ghttp.Request){ + r.Response.ServeFile("text.txt") + }) + s.SetPort(8199) + s.Run() +} \ No newline at end of file diff --git a/geg/net/ghttp/server/download/text.txt b/geg/net/ghttp/server/download/text.txt new file mode 100644 index 000000000..7e7566d8c --- /dev/null +++ b/geg/net/ghttp/server/download/text.txt @@ -0,0 +1 @@ +下载文件内容。 \ No newline at end of file From 882c69cb08ea1c355aa4e5f15b0c9bcfd3c74387 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 11 Nov 2018 11:26:03 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgfsnotify=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=BC=96=E8=BE=91=E5=99=A8=E5=AF=B9=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=9D=9E=E6=A0=87=E5=87=86=E7=BC=96=E8=BE=91=E6=97=B6?= =?UTF-8?q?(RENAME+CHMOD)=E7=9A=84=E7=83=AD=E6=9B=B4=E6=96=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gfsnotify/gfsnotify_watcher.go | 36 ++++++++++++------- geg/net/ghttp/server/template/tpl1/index.html | 2 +- geg/os/gfsnotify/gfsnotify_limit.go | 19 ++++++++++ 3 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 geg/os/gfsnotify/gfsnotify_limit.go diff --git a/g/os/gfsnotify/gfsnotify_watcher.go b/g/os/gfsnotify/gfsnotify_watcher.go index ee6a32fcc..44bbfc9dc 100644 --- a/g/os/gfsnotify/gfsnotify_watcher.go +++ b/g/os/gfsnotify/gfsnotify_watcher.go @@ -207,19 +207,31 @@ func (w *Watcher) startEventLoop() { for { if v := w.events.Pop(); v != nil { event := v.(*Event) - // 如果是删除操作,那么需要判断是否文件真正不存在了 - if event.IsRemove() { - if fileExists(event.Path) { - // 如果是文件删除事件,判断该文件是否存在,如果存在,那么将此事件认为“假删除”, - // 并重新添加监控(底层fsnotify会自动删除掉监控,这里重新添加回去) - w.watcher.Add(event.Path) - // 修改事件操作为重命名(相当于重命名为自身名称,最终名称没变) - event.Op = RENAME - } else { - // 如果是真实删除,那么递归删除监控信息 - w.Remove(event.Path) - } + fmt.Println(event) + fmt.Println(fileExists(event.Path)) + switch { + // 如果是删除操作,那么需要判断是否文件真正不存在了,如果存在,那么将此事件认为“假删除” + case event.IsRemove(): + if fileExists(event.Path) { + // 重新添加监控(底层fsnotify会自动删除掉监控,这里重新添加回去) + // 注意这里调用的是底层fsnotify添加监控,只会产生回调事件,并不会使回调函数重复注册 + w.watcher.Add(event.Path) + // 修改事件操作为重命名(相当于重命名为自身名称,最终名称没变) + event.Op = RENAME + } else { + // 如果是真实删除,那么递归删除监控信息 + w.Remove(event.Path) + } + + // 如果是删除操作,那么需要判断是否文件真正不存在了,如果存在,那么将此事件认为“假命名” + // (特别是某些编辑器在编辑文件时会先对文件RENAME再CHMOD) + case event.IsRename(): + if fileExists(event.Path) { + // 重新添加监控 + w.watcher.Add(event.Path) + } } + callbacks := w.getCallbacks(event.Path) // 如果创建了新的目录,那么将这个目录递归添加到监控中 if event.IsCreate() && fileIsDir(event.Path) { diff --git a/geg/net/ghttp/server/template/tpl1/index.html b/geg/net/ghttp/server/template/tpl1/index.html index 8a50061ce..ec5f48cfc 100644 --- a/geg/net/ghttp/server/template/tpl1/index.html +++ b/geg/net/ghttp/server/template/tpl1/index.html @@ -6,6 +6,6 @@

姓名 : {{.name}}

-12311 +12345678910 \ No newline at end of file diff --git a/geg/os/gfsnotify/gfsnotify_limit.go b/geg/os/gfsnotify/gfsnotify_limit.go new file mode 100644 index 000000000..f42a29824 --- /dev/null +++ b/geg/os/gfsnotify/gfsnotify_limit.go @@ -0,0 +1,19 @@ +package main + +import ( + "gitee.com/johng/gf/g/os/gfsnotify" + "gitee.com/johng/gf/g/os/glog" +) + +// 对同一个文件多次Add是否超过系统inotify限制 +func main() { + path := "/Users/john/temp/log" + for i := 0; i < 9999999; i++ { + _, err := gfsnotify.Add(path, func(event *gfsnotify.Event) { + glog.Println(event) + }) + if err != nil { + glog.Fatalln(err) + } + } +} \ No newline at end of file From b303bf12b578953d50af5a544f6077248921be71 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 11 Nov 2018 13:55:32 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ghttp=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geg/net/ghttp/server/download/download.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/geg/net/ghttp/server/download/download.go b/geg/net/ghttp/server/download/download.go index f54893e57..5c1d01abb 100644 --- a/geg/net/ghttp/server/download/download.go +++ b/geg/net/ghttp/server/download/download.go @@ -8,6 +8,11 @@ import ( func main() { s := g.Server() s.BindHandler("/download", func(r *ghttp.Request){ + r.Response.Header().Set("Content-Type", "text/html;charset=utf-8"); + r.Response.Header().Set("Content-type", "application/force-download"); + r.Response.Header().Set("Content-Type", "application/octet-stream"); + r.Response.Header().Set("Accept-Ranges", "bytes"); + r.Response.Header().Set("Content-Disposition", "attachment;filename=\"下载文件名称.txt\""); r.Response.ServeFile("text.txt") }) s.SetPort(8199) From e4b1b03b7fed2baf38d86c3640e4573e42780105 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 11 Nov 2018 13:59:40 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgfsnotify=E5=9C=A8windo?= =?UTF-8?q?ws=E7=B3=BB=E7=BB=9F=E4=B8=8B=E7=9A=84=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gfsnotify/gfsnotify_watcher.go | 36 +++++++++++++++++++++++++---- geg/os/gfsnotify/fsnotify.go | 33 ++++++-------------------- geg/os/gfsnotify/gfsnotify.go | 20 ++-------------- 3 files changed, 41 insertions(+), 48 deletions(-) diff --git a/g/os/gfsnotify/gfsnotify_watcher.go b/g/os/gfsnotify/gfsnotify_watcher.go index 44bbfc9dc..e10a64b4f 100644 --- a/g/os/gfsnotify/gfsnotify_watcher.go +++ b/g/os/gfsnotify/gfsnotify_watcher.go @@ -191,24 +191,52 @@ func (w *Watcher) startWatchLoop() { // 检索给定path的回调方法**列表** func (w *Watcher) getCallbacks(path string) *glist.List { - for path != "/" { + for { if l := w.callbacks.Get(path); l != nil { return l.(*glist.List) } else { - path = fileDir(path) + if p := fileDir(path); p != path { + path = p + } else { + break + } } } return nil } +// 获得真正监听的文件路径,判断规则: +// 1、在 callbacks 中应当有回调注册函数(否则监听根本没意义); +// 2、如果该path下不存在回调注册函数,则按照path长度从右往左递减,直到减到目录地址为止(不包含); +// 3、如果仍旧无法匹配回调函数,那么忽略,否则使用查找到的新path覆盖掉event的path; +func (w *Watcher) getWatchTruePath(path string) string { + if w.getCallbacks(path) != nil { + return path + } + dirPath := fileDir(path) + for { + path = path[0 : len(path) - 1] + if path == dirPath { + break + } + if w.getCallbacks(path) != nil { + return path + } + } + return "" +} + // 事件循环 func (w *Watcher) startEventLoop() { go func() { for { if v := w.events.Pop(); v != nil { event := v.(*Event) - fmt.Println(event) - fmt.Println(fileExists(event.Path)) + if path := w.getWatchTruePath(event.Path); path == "" { + continue + } else { + event.Path = path + } switch { // 如果是删除操作,那么需要判断是否文件真正不存在了,如果存在,那么将此事件认为“假删除” case event.IsRemove(): diff --git a/geg/os/gfsnotify/fsnotify.go b/geg/os/gfsnotify/fsnotify.go index e69058c6a..8fb717e00 100644 --- a/geg/os/gfsnotify/fsnotify.go +++ b/geg/os/gfsnotify/fsnotify.go @@ -3,6 +3,7 @@ package main import ( "gitee.com/johng/gf/third/github.com/fsnotify/fsnotify" "log" + "gitee.com/johng/gf/g/os/glog" ) func main() { @@ -13,7 +14,7 @@ func main() { } defer watch.Close() //添加要监控的对象,文件或文件夹 - err = watch.Add("/home/john/temp") + err = watch.Add("D:\\Workspace\\Go\\GOPATH\\src\\gitee.com\\johng\\gf\\geg\\other\\test.go") if err != nil { log.Fatal(err) } @@ -21,32 +22,12 @@ func main() { go func() { for { select { - case ev := <-watch.Events: - //判断事件发生的类型,如下5种 - // Create 创建 - // Write 写入 - // Remove 删除 - // Rename 重命名 - // Chmod 修改权限 - if ev.Op&fsnotify.Create == fsnotify.Create { - log.Println("创建文件 : ", ev.Name) - } - if ev.Op&fsnotify.Write == fsnotify.Write { - log.Println("写入文件 : ", ev.Name) - } - if ev.Op&fsnotify.Remove == fsnotify.Remove { - log.Println("删除文件 : ", ev.Name) - } - if ev.Op&fsnotify.Rename == fsnotify.Rename { - log.Println("重命名文件 : ", ev.Name) - } - if ev.Op&fsnotify.Chmod == fsnotify.Chmod { - log.Println("修改权限 : ", ev.Name) - } + case ev := <-watch.Events: + glog.Println(ev) - case err := <-watch.Errors: - log.Println("error : ", err) - return + case err := <-watch.Errors: + log.Println("error : ", err) + return } } diff --git a/geg/os/gfsnotify/gfsnotify.go b/geg/os/gfsnotify/gfsnotify.go index 702d21227..ba4b2fffa 100644 --- a/geg/os/gfsnotify/gfsnotify.go +++ b/geg/os/gfsnotify/gfsnotify.go @@ -6,29 +6,13 @@ import ( ) func main() { - // /home/john/temp 是一个目录,当然也可以指定文件 - path := "/home/john/temp" + path := "D:\\Workspace\\Go\\GOPATH\\src\\gitee.com\\johng\\gf\\geg\\other\\test.go" _, err := gfsnotify.Add(path, func(event *gfsnotify.Event) { - if event.IsCreate() { - glog.Println("创建文件 : ", event.Path) - } - if event.IsWrite() { - glog.Println("写入文件 : ", event.Path) - } - if event.IsRemove() { - glog.Println("删除文件 : ", event.Path) - } - if event.IsRename() { - glog.Println("重命名文件 : ", event.Path) - } - if event.IsChmod() { - glog.Println("修改权限 : ", event.Path) - } glog.Println(event) }) // 移除对该path的监听 - gfsnotify.Remove(path) + //gfsnotify.Remove(path) if err != nil { glog.Fatalln(err) From 72a18c0a1033c5d6f425b9341656999688b6e8c9 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 11 Nov 2018 14:21:22 +0800 Subject: [PATCH 09/10] =?UTF-8?q?glog=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=B1=A0=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/glog/glog_logger.go | 5 +++-- geg/net/ghttp/server/template/tpl1/index.html | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index ed2ea114f..92004d6db 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -11,6 +11,7 @@ import ( "fmt" "gitee.com/johng/gf/g/container/gtype" "gitee.com/johng/gf/g/os/gfile" + "gitee.com/johng/gf/g/os/gfpool" "gitee.com/johng/gf/g/os/gmlock" "gitee.com/johng/gf/g/os/gtime" "gitee.com/johng/gf/g/util/gregex" @@ -127,14 +128,14 @@ func (l *Logger) GetWriter() io.Writer { } // 获取默认的文件IO -func (l *Logger) getFilePointer() *os.File { +func (l *Logger) getFilePointer() *gfpool.File { if path := l.path.Val(); path != "" { // 文件名称中使用"{}"包含的内容使用gtime格式化 file, _ := gregex.ReplaceStringFunc(`{.+?}`, l.file.Val(), func(s string) string { return gtime.Now().Format(strings.Trim(s, "{}")) }) fpath := path + gfile.Separator + file - if fp, err := gfile.OpenWithFlagPerm(fpath, gDEFAULT_FILE_POOL_FLAGS, 0666); err == nil { + if fp, err := gfpool.Open(fpath, gDEFAULT_FILE_POOL_FLAGS, 0666, 60000); err == nil { return fp } else { fmt.Fprintln(os.Stderr, err) diff --git a/geg/net/ghttp/server/template/tpl1/index.html b/geg/net/ghttp/server/template/tpl1/index.html index ec5f48cfc..bebaf5f32 100644 --- a/geg/net/ghttp/server/template/tpl1/index.html +++ b/geg/net/ghttp/server/template/tpl1/index.html @@ -6,6 +6,6 @@

姓名 : {{.name}}

-12345678910 +12 \ No newline at end of file From 41c85ab84f08ffac03e114d96656478e406a53c2 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 11 Nov 2018 14:24:04 +0800 Subject: [PATCH 10/10] =?UTF-8?q?glog=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=B1=A0=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/glog/glog_logger.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index 92004d6db..a5b1fa111 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -39,6 +39,8 @@ type Logger struct { const ( gDEFAULT_FILE_FORMAT = `{Y-m-d}.log` gDEFAULT_FILE_POOL_FLAGS = os.O_CREATE|os.O_WRONLY|os.O_APPEND + gDEFAULT_FPOOL_PERM = os.FileMode(0666) + gDEFAULT_FPOOL_EXPIRE = 60000 ) var ( @@ -135,7 +137,7 @@ func (l *Logger) getFilePointer() *gfpool.File { return gtime.Now().Format(strings.Trim(s, "{}")) }) fpath := path + gfile.Separator + file - if fp, err := gfpool.Open(fpath, gDEFAULT_FILE_POOL_FLAGS, 0666, 60000); err == nil { + if fp, err := gfpool.Open(fpath, gDEFAULT_FILE_POOL_FLAGS, gDEFAULT_FPOOL_PERM, gDEFAULT_FPOOL_EXPIRE); err == nil { return fp } else { fmt.Fprintln(os.Stderr, err)