github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/repo/7_file_server.go (about) 1 package repo 2 3 import ( 4 "net/http" 5 "os" 6 "strconv" 7 8 "github.com/pbberlin/tools/net/http/fileserver" 9 "github.com/pbberlin/tools/net/http/tplx" 10 "github.com/pbberlin/tools/os/fsi" 11 "github.com/pbberlin/tools/os/fsi/dsfs" 12 "github.com/pbberlin/tools/os/fsi/osfs" 13 "golang.org/x/net/context" 14 "google.golang.org/appengine" 15 ) 16 17 // GetFS instantiates a filesystem, depending on whichtype 18 func GetFS(c context.Context) (fs fsi.FileSystem) { 19 switch whichType { 20 case 0: 21 // must be re-instantiated for each request 22 docRoot = "" 23 dsFileSys := dsfs.New(dsfs.DirSort("byDateDesc"), dsfs.MountName(mountName), dsfs.AeContext(c)) 24 fs = fsi.FileSystem(dsFileSys) 25 case 1: 26 docRoot = "c:/docroot/" 27 os.Chdir(docRoot) 28 osFileSys := osfs.New(osfs.DirSort("byDateDesc")) 29 fs = fsi.FileSystem(osFileSys) 30 case 2: 31 // re-instantiation would delete contents 32 docRoot = "" 33 fs = fsi.FileSystem(memMapFileSys) 34 default: 35 panic("invalid whichType ") 36 } 37 38 return 39 } 40 41 // setFSType sets an internal variable, determining what FileSystems 42 // should be used. Default is dsfs. 43 func setFSType(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 44 45 wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Set fetcher reservoir filesystem type"})) 46 defer wpf(w, tplx.Foot) 47 48 stp := r.FormValue("type") 49 newTp, err := strconv.Atoi(stp) 50 51 if err == nil && newTp >= 0 && newTp <= 2 { 52 whichType = newTp 53 wpf(w, "new type: %v<br><br>\n", whichType) 54 } 55 56 if whichType != 0 { 57 wpf(w, "<a href='%v?type=0' >dsfs</a><br>\n", uriSetType) 58 } else { 59 wpf(w, "<b>dsfs</b><br>\n") 60 } 61 if whichType != 1 { 62 wpf(w, "<a href='%v?type=1' >osfs</a><br>\n", uriSetType) 63 } else { 64 wpf(w, "<b>osfs</b><br>\n") 65 } 66 if whichType != 2 { 67 wpf(w, "<a href='%v?type=2' >memfs</a><br>\n", uriSetType) 68 } else { 69 wpf(w, "<b>memfs</b><br>\n") 70 } 71 72 } 73 74 // UNUSED, since appengine context is required for our filesystems 75 func serveSingleRootFile(pattern string, filename string) { 76 http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { 77 http.ServeFile(w, r, filename) // filename refers to local path; unusable for fsi 78 }) 79 } 80 81 // serveFile makes the previously fetched files available like 82 // a static fileserver. 83 func serveFile(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 84 fs1 := GetFS(appengine.NewContext(r)) 85 fileserver.FsiFileServer(w, r, fileserver.Options{FS: fs1, Prefix: UriMountNameY}) 86 }