Skip to content

Commit

Permalink
feat: add qrcode output
Browse files Browse the repository at this point in the history
  • Loading branch information
sssvip committed May 24, 2018
1 parent 32b3556 commit 1b20c21
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ Transfer and browse files as simple as possiable via HTTP. Supported Windows, Li
b. open on port 8080:

> simplefileserver.exe 8080
$ ./simplefileserver 8080
$ ./simplefileserver 8080

4. [New Option] Show the url qrcode in the terminal.(Windows default window not support now)

a. output all local ip addr qrcode:

> simplefileserver.exe qr
$ ./simplefileserver qr

b. output local ip addr qrcode with special key(eg: just output ip addr contains 192 once)

> simplefileserver.exe qr192
$ ./simplefileserver qr192
51 changes: 42 additions & 9 deletions simplefileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"strconv"
"net"
"github.com/mdp/qrterminal"
)

var staticPath = "/static/"
Expand Down Expand Up @@ -38,14 +39,11 @@ func getCurrentDirectory() string {
func home(w http.ResponseWriter, r *http.Request) {
currentPath := getCurrentDirectory()
fmt.Fprintf(w, "<h1>Files in [%s]</h1></br></br></br>", currentPath)
filenames := listDir(currentPath)
for _, filename := range filenames {
fileNames := listDir(currentPath)
for _, filename := range fileNames {
fmt.Fprintf(w, "<a href=\"%s%s\">%s</a></br>", staticPath, string(filename), string(filename))
}
fmt.Fprintf(w, "</br></br></br>See the new version of SimpleFileServer :<a href=\"%s\">%s</a>", repoUrl, repoUrl)
//if len(filenames)<1{
// fmt.Fprintln(w,"no file in this directory")
//}
}

func getLocalIPs() []string {
Expand All @@ -66,24 +64,59 @@ func getLocalIPs() []string {
}

func entry() {
port := "80"
var defaultPort = "80"
port := defaultPort
showQRCode := false
const qr = "qr"
showQRCodeIpFilter := qr
if len(os.Args) > 1 {
tmpPort, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Println("http port must a num, eg: simplefileserver.exe 80")
log.Println("http port must a num, eg: simplefileserver.exe 8080")
log.Println("use defualut http port:" + port)
} else {
port = strconv.Itoa(tmpPort)
}
//check qr
for _, arg := range os.Args {
if strings.Contains(arg, qr) {
showQRCode = true
showQRCodeIpFilter = arg
break
}
}
}

http.HandleFunc("/", home)
http.HandleFunc(staticPath, func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path)
http.ServeFile(w, r, r.URL.Path[len(staticPath):])
})
log.Println("starttd file server http://127.0.0.1:" + port)
var startEcho = "started file server http://127.0.0.1"
if port != defaultPort {
startEcho = fmt.Sprintf("%s:%s", startEcho, port)
}
log.Println(startEcho)
var optPreAddr = "Optional addr: http://"
for _, localIp := range getLocalIPs() {
log.Println("Optional addr: http://" + localIp + ":" + port)
urlAddr := fmt.Sprintf("%s%s", optPreAddr, localIp)
if port != defaultPort {
urlAddr = fmt.Sprintf("%s:%s", urlAddr, port)
}
log.Println(urlAddr)
if showQRCode {
if showQRCodeIpFilter != qr {
if strings.Contains(urlAddr, strings.Replace(showQRCodeIpFilter, qr, "", -1)) {
qrterminal.Generate(urlAddr, qrterminal.L, os.Stdout)
fmt.Println()
break
}
} else {
qrterminal.Generate(urlAddr, qrterminal.L, os.Stdout)
fmt.Println()
}
}

}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
Expand Down

0 comments on commit 1b20c21

Please sign in to comment.