@@ -2,15 +2,17 @@ package cmd
22
33import (
44 "fmt"
5- "github.com/maetthu/dirhttps/internal/lib/version"
6- "github.com/mitchellh/go-homedir"
7- "github.com/rs/cors"
8- "github.com/spf13/cobra"
95 "log"
106 "net/http"
117 "net/http/httputil"
128 "os"
139 "path/filepath"
10+
11+ "github.com/maetthu/dirhttps/internal/lib/static"
12+ "github.com/maetthu/dirhttps/internal/lib/version"
13+ "github.com/mitchellh/go-homedir"
14+ "github.com/rs/cors"
15+ "github.com/spf13/cobra"
1416)
1517
1618const (
@@ -19,9 +21,9 @@ const (
1921)
2022
2123var rootCmd = & cobra.Command {
22- Use : "dirhttps" ,
23- Short : "Serving contents of current directory by HTTPS." ,
24- Args : cobra .NoArgs ,
24+ Use : "dirhttps" ,
25+ Short : "Serving contents of current directory by HTTPS." ,
26+ Args : cobra .NoArgs ,
2527 Version : fmt .Sprintf ("%s -- %s" , version .Version , version .Commit ),
2628 Run : func (cmd * cobra.Command , args []string ) {
2729
@@ -39,11 +41,11 @@ var rootCmd = &cobra.Command{
3941
4042 certAvailable := true
4143
42- if _ , err := os .Stat (certFile ); err != nil {
44+ if _ , err := os .Stat (certFile ); err != nil {
4345 certAvailable = false
4446 }
4547
46- if _ , err := os .Stat (keyFile ); err != nil {
48+ if _ , err := os .Stat (keyFile ); err != nil {
4749 certAvailable = false
4850 }
4951
@@ -53,7 +55,7 @@ var rootCmd = &cobra.Command{
5355 "Store a certificate to \" %s\" and a key file to \" %s\" or provide the --cert and --key flags" ,
5456 certFile ,
5557 keyFile ,
56- )
58+ )
5759 }
5860
5961 dir , err := os .Getwd ()
@@ -71,13 +73,23 @@ var rootCmd = &cobra.Command{
7173 log .Printf ("Listening for HTTPS connections on %s" , addr )
7274 log .Printf ("Serving from directory %s" , dir )
7375
74- handler := logger (http .FileServer (http .Dir (dir )))
76+ handler := http .FileServer (http .Dir (dir ))
77+
78+ // favicon
79+ if disableFavicon , _ := cmd .Flags ().GetBool ("no-favicon" ); ! disableFavicon {
80+ handler = favicon (handler )
81+ }
82+
83+ // logger
84+ if quiet , _ := cmd .Flags ().GetBool ("quiet" ); ! quiet {
85+ handler = logger (handler )
86+ }
7587
7688 // permit some CORS stuff
7789 if disableCORS , _ := cmd .Flags ().GetBool ("no-cors" ); ! disableCORS {
7890 handler = cors .New (cors.Options {
7991 AllowCredentials : true ,
80- AllowOriginFunc : func (origin string ) bool {return true },
92+ AllowOriginFunc : func (origin string ) bool { return true },
8193 }).Handler (handler )
8294 }
8395
@@ -103,7 +115,7 @@ func Execute() {
103115 }
104116}
105117
106- func init (){
118+ func init () {
107119 home , err := homedir .Dir ()
108120
109121 if err != nil {
@@ -120,13 +132,42 @@ func init(){
120132 rootCmd .Flags ().StringP ("listen" , "l" , ":8443" , "Listen address" )
121133
122134 rootCmd .Flags ().Bool ("cache" , false , "Enable client side caching" )
123- rootCmd .Flags ().BoolP ("dump" , "d" ,false , "Dump client request headers to STDOUT" )
135+ rootCmd .Flags ().BoolP ("dump" , "d" , false , "Dump client request headers to STDOUT" )
136+ rootCmd .Flags ().BoolP ("quiet" , "q" , false , "Don't log requests to STDOUT" )
124137 rootCmd .Flags ().Bool ("no-cors" , false , "Disable CORS handling" )
138+ rootCmd .Flags ().Bool (
139+ "no-favicon" ,
140+ false ,
141+ "Disable default favicon delivered when no favicon.ico is present in current directory" ,
142+ )
143+ }
144+
145+ func favicon (handler http.Handler ) http.Handler {
146+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
147+ if r .RequestURI != "/favicon.ico" {
148+ handler .ServeHTTP (w , r )
149+ return
150+ }
151+
152+ i , err := os .Stat ("favicon.ico" )
153+
154+ if ! os .IsNotExist (err ) && ! i .IsDir () {
155+ handler .ServeHTTP (w , r )
156+ return
157+ }
158+
159+ w .Header ().Add ("Content-Type" , "image/vnd.microsoft.icon" )
160+ _ , err = w .Write (static .Favicon )
161+
162+ if err != nil {
163+ log .Print (err )
164+ }
165+ })
125166}
126167
127168func logger (handler http.Handler ) http.Handler {
128169 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
129- log .Printf ( "%s \" %s %s %s\" \n " , r .RemoteAddr , r .Method , r .URL , r .Proto )
170+ log .Printf ("%s \" %s %s %s\" \n " , r .RemoteAddr , r .Method , r .URL , r .Proto )
130171 handler .ServeHTTP (w , r )
131172 })
132173}
0 commit comments