4
4
"fmt"
5
5
"net/http"
6
6
"os"
7
+ "path/filepath"
7
8
"strconv"
8
9
9
10
"github.com/mdp/qrterminal/v3"
@@ -18,7 +19,7 @@ var showQrCode bool
18
19
func init () {
19
20
rootCmd .AddCommand (startCmd )
20
21
startCmd .Flags ().StringVarP (& folder , "directory" , "d" , "" , "Path of the web folder" )
21
- startCmd .Flags ().StringVarP (& filePath , "file" , "f" , "" , "Path of a file (for hosting a single file)" )
22
+ startCmd .Flags ().StringVarP (& filePath , "file" , "f" , "" , "Path of a file (for sharing a single file)" )
22
23
startCmd .Flags ().BoolVar (& showQrCode , "qrcode" , false , "Show QR code of server URL" )
23
24
startCmd .Flags ().IntVarP (& port , "port" , "p" , 8080 , "Port number" )
24
25
}
@@ -30,18 +31,22 @@ var startCmd = &cobra.Command{
30
31
Args : cobra .MinimumNArgs (0 ),
31
32
32
33
Run : func (cmd * cobra.Command , args []string ) {
33
-
34
34
if filePath != "" {
35
+ http .HandleFunc ("/" , func (res http.ResponseWriter , req * http.Request ) {
36
+ res .Header ().Add ("Content-Disposition" , "attachment;filename=\" " + filepath .Base (filePath )+ "\" " )
37
+ http .ServeFile (res , req , filePath )
38
+ })
39
+ } else {
40
+ fileHandler := http .FileServer (http .Dir (folder ))
41
+ http .Handle ("/" , fileHandler )
35
42
}
36
43
37
- fileHandler := http .FileServer (http .Dir (folder ))
38
-
39
44
ips , err := externalIPs ()
40
45
if err != nil {
41
46
panic (err )
42
47
}
43
48
44
- fmt .Println ("Http server is running at: " )
49
+ fmt .Println ("Http server is running at:" )
45
50
fmt .Println ("http://localhost:" + strconv .Itoa (port ))
46
51
for _ , ip := range ips {
47
52
url := "http://" + ip + ":" + strconv .Itoa (port )
@@ -51,7 +56,7 @@ var startCmd = &cobra.Command{
51
56
}
52
57
}
53
58
54
- err = http .ListenAndServe (fmt .Sprint (":" , port ), fileHandler )
59
+ err = http .ListenAndServe (fmt .Sprint (":" , port ), nil )
55
60
fmt .Println ("bye~" )
56
61
if err != nil {
57
62
panic (err )
0 commit comments