golang file server two ways of can access any directory

  • 2020-10-31 21:47:13
  • OfStack

1. Method 1:

The main method used is FileServer of the http package. The argument is simple, namely the path of the folder to be routed.


package main

import (
  "fmt"
  "net/http"
)

func main() {
  http.Handle("/", http.FileServer(http.Dir("./")))

  e := http.ListenAndServe(":8080", nil)
  fmt.Println(e)
}

In the example above, the route can only map the root directory, i.e. the "/ "directory. For example, if you write" http.Handle ("/files", http.FileServer (http.Dir ("./")) ", you cannot map the file under the current path by accessing "/ files ". Hence the StripPrefix method for the http package.

2. Method 2:

Implements access to files under any folder.


package main

import (
  "fmt"
  "net/http"
)

func main() {
  mux := http.NewServeMux()
  mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("/"))))
  mux.Handle("/c/", http.StripPrefix("/c/", http.FileServer(http.Dir("c:"))))
  mux.Handle("/d/", http.StripPrefix("/d/", http.FileServer(http.Dir("d:"))))
  mux.Handle("/e/", http.StripPrefix("/e/", http.FileServer(http.Dir("e:"))))
  if err := http.ListenAndServe(":3008", mux); err != nil {
    log.Fatal(err)
  }
}

An ServeMux is generated here, independent of the file server, so you can ignore it for now. In this way, you can route the files under any folder out.

ps: File server implemented by golang

Recently, I have been learning golang, using golang to achieve the simplest file server, the program has only a simple 10 lines of code, can be compiled into windows, linux, mac multi-platform executable file.

The source code


package main

import (
 "fmt"
 "net/http"
 "os"
 "path/filepath"
)

func main() {
 p, _ := filepath.Abs(filepath.Dir(os.Args[0]))
 http.Handle("/", http.FileServer(http.Dir(p)))
 err := http.ListenAndServe(":8088", nil)
 if err != nil {
 fmt.Println(err)
 }
}

The source code to explain

os. Args[0] gets the first parameter when the program is executed, and the default first parameter is the directory where the program is located
filepath.Abs (filepath.Dir (os.Args [0])) is the absolute path to get the current executable
http. Handle("/", http. FileServer(http. Dir(p))) is to open 1 file server using the path where the current executable is located
http.ListenAndServe (":8088", nil) is listening on port 8088 and opening the file server

compile

To compile the source code into executables for different platforms, use the gox tool to install gox using the following commands:

go get github.com/mitchellh/gox

After successful execution, the executables for each platform can be compiled automatically using the gox command. If you want to compile separately for a platform, you can use the following method:

gox -os "windows linux" -arch amd64

The -ES82en parameter specifies the compilation platform, and the -ES83en parameter specifies the processor architecture

run

Open the compiled executable program directly and run it. Visit http://ip:8088 in the browser and you can see all the files in the directory where the executable file is located.


Related articles: