Google Go语言实现http共享(带trace) - Go语言中文社区

Google Go语言实现http共享(带trace)


我之前有篇文章(http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html)中提到过用Go语言实现http文件共享,这个版本的程序比python的实现快了点,默认情况下支持的客户端多了些,但是没有客户访问的trace,程序运行过程中,感觉像是死掉了。我想改进下,让它有trace。

代码如下:

  1. /* 
  2. File      : httpShareWithTrace.go 
  3. Author    : Mike 
  4. E-Mail    : Mike_Zhang@live.com 
  5. */ 
  6. package main 
  7. import( 
  8.     "fmt" 
  9.     "net/http" 
  10.     "io/ioutil" 
  11.     "log" 
  12.     "time" 
  13.     "os" 
  14.     "strings" 
  15.  
  16. func getFilelist(path string) string { 
  17.         m_files,err  :=  ioutil.ReadDir(path) 
  18.         if err !=nil{ 
  19.         //     println( "Get filelist error !" ) 
  20.                 return "" 
  21.         } 
  22.         var strRet string 
  23.         for _,f :range m_files  { 
  24.                 //    println(f.Name(),f.IsDir()) 
  25.                 if path == "./" { 
  26.                         strRet += "<p><a href=""+path+""+f.Name() +" ">" + f.Name() + "</a></p>
  27.                 }else{ 
  28.                         strRet += "<p><a href=""+path[1:]+"/"+f.Name() +" ">" + f.Name() + "</a></p>
  29.                 } 
  30.         } 
  31.         return strRet 
  32.  
  33. func Handler( w http.ResponseWriter,r *http.Request ){ 
  34.         println("Request ",r.URL.Path," from ",r.RemoteAddr) 
  35.         //   path :r.URL.Path[1:] 
  36.         path :"." + r.URL.Path 
  37.         if path == "./favicon.ico" {http.NotFound(w,r);return} 
  38.         if path == "./" ||  getFilelist(path) != "" {fmt.Fprintf( w,"%s",getFilelist(path));return} 
  39.         fin,err :os.Open(path) 
  40.         defer fin.Close() 
  41.         if err != nil {fmt.Fprintf( w,"404 : Not found" );return} 
  42.         readLen :1024 * 1024 
  43.         buf :make([]byte,readLen) 
  44.         startPos :0 
  45.         println("Transfer file ",path," ... ") 
  46.         for { 
  47.                 n,err :fin.ReadAt(buf,int64(startPos)) 
  48.                 fmt.Fprintf(w,"%s",buf[:n]) 
  49.                 if 0 == n || err != nil {break} 
  50.                 startPos += readLen 
  51.         } 
  52. func main(){ 
  53.         port :"8080"  //Default port  
  54.         if len(os.Args)>1 { port = strings.Join(os.Args[1:2],"")} 
  55.         http.HandleFunc( "/",Handler) 
  56.         s := &http.Server{ 
  57.                 Addr:           ":"+port, 
  58.                 ReadTimeout:    1 * time.Hour,  
  59.                 WriteTimeout:   1 * time.Hour, 
  60.                 MaxHeaderBytes: (1 << 31) - 1 , //Max file size is 2048M 
  61.         } 
  62.         println("Listening on port ",port,"...") 
  63.         log.Fatal(s.ListenAndServe()) 

运行效果如下:

1、启动http文件共享

2、web访问

3、后台trace

说明:最大支持2G文件的下载,限时为1个小时,这里没有用充分使用http协议,直接用文件io做的。时间有限,这里暂时达到了预期功能,够局域网使用,这个等以后有时间了做进一步的优化。

原文链接:http://www.cnblogs.com/MikeZhang/archive/2012/08/06/httpShareGolang20120805.html

版权声明:本文来源51CTO,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:http://developer.51cto.com/art/201208/351673.htm
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-15 23:55:57
  • 阅读 ( 1130 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢