github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/upload/3_http_fileserver.go (about) 1 package upload 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/pbberlin/tools/net/http/fileserver" 8 "github.com/pbberlin/tools/os/fsi/dsfs" 9 "github.com/pbberlin/tools/os/fsi/httpfs" 10 "golang.org/x/net/context" 11 "google.golang.org/appengine" 12 ) 13 14 var cx context.Context 15 var mountPoint string 16 17 func howIsContext(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 18 if cx == nil { 19 wpf(w, "nil\n") 20 } else { 21 wpf(w, "context for mp %q is well: %v\n", mountPoint, cx) 22 } 23 } 24 25 // 26 // A static fileserver is NOT working 27 // Since we need an appengine.context 28 // 29 // UNUSED, NOT WORKING 30 func serveDsFs(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 31 32 // Examples 33 http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./img/")))) 34 http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/")))) 35 http.Handle("/tmp/", http.StripPrefix("/tmp/", http.FileServer(http.Dir("c:\\temp")))) 36 37 c := appengine.NewContext(r) 38 cx = c 39 40 mountPoint = dsfs.MountPointLast() 41 fs1 := dsfs.New( 42 dsfs.MountName(mountPoint), 43 dsfs.AeContext(c), 44 ) 45 httpFSys := &httpfs.HttpFs{SourceFs: fs1} 46 47 // HERE is the trouble! 48 http.Handle("/tmp1/", http.StripPrefix("/tmp1/", http.FileServer(httpFSys.Dir("./")))) 49 50 wpf(w, "serving %v", mountPoint) 51 52 // time.Sleep(5 * time.Second) 53 54 } 55 56 // We cannot use http.FileServer(http.Dir("./css/") to dispatch our dsfs files. 57 // We need the appengine context to initialize dsfs. 58 // 59 // Thus we re-implement a serveFile method: 60 func ServeDsFsFile(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 61 62 urlPath := m["dir"].(string) 63 if len(urlPath) > 0 { 64 urlPath = urlPath[1:] 65 } 66 67 prefix := "/mnt00" 68 69 pos := strings.Index(urlPath, "/") 70 if pos > 0 { 71 prefix = "/" + urlPath[:pos] 72 } 73 if pos == -1 { 74 prefix = "/" + urlPath 75 } 76 77 fs2 := dsfs.New( 78 dsfs.MountName(prefix[1:]), 79 dsfs.AeContext(appengine.NewContext(r)), 80 ) 81 82 fileserver.FsiFileServer(w, r, fileserver.Options{FS: fs2, Prefix: prefix + "/"}) 83 84 }