commit fafd4e51b4b8e8f6098736961bd7ea5eb0c3db05
parent c8a7410f87bcbde23a26c2e933baec108a0c24a0
Author: Brian C. Lane <bcl@brianlane.com>
Date: Sun, 19 Oct 2025 15:04:35 -0700
Add -verbose to log requests
Logs the client IP:SOCKET METHOD URL
Fixes #1
Diffstat:
| M | main.go | | | 59 | ++++++++++++++++++++++++++++++++++++++++++----------------- |
1 file changed, 42 insertions(+), 17 deletions(-)
diff --git a/main.go b/main.go
@@ -1,20 +1,21 @@
-/*dir-server - simple http or https server for current directory
+/*
+dir-server - simple http or https server for current directory
- Copyright (C) 2019-2020 Brian C. Lane <bcl@brianlane.com>
+Copyright (C) 2019 Brian C. Lane <bcl@brianlane.com>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package main
@@ -42,6 +43,7 @@ type cmdlineArgs struct {
Key string
SinglePage bool
Path string
+ Verbose bool
}
var cfg = cmdlineArgs{
@@ -52,6 +54,7 @@ var cfg = cmdlineArgs{
Key: "/var/tmp/key.pem",
SinglePage: false,
Path: "./",
+ Verbose: false,
}
func init() {
@@ -62,6 +65,7 @@ func init() {
flag.StringVar(&cfg.Key, "key", cfg.Key, "Path to temporary key.pem")
flag.BoolVar(&cfg.SinglePage, "single", cfg.SinglePage, "Single page app, direct all unknown routes to index.html")
flag.StringVar(&cfg.Path, "path", cfg.Path, "Path to serve files from")
+ flag.BoolVar(&cfg.Verbose, "verbose", cfg.Verbose, "verbose request logging")
flag.Parse()
}
@@ -163,22 +167,43 @@ func (fs singlePageFileServer) Open(name string) (http.File, error) {
return singlePageFile{file}, nil
}
+func LogHandler(h http.Handler) http.Handler {
+ fn := func(w http.ResponseWriter, r *http.Request) {
+ h.ServeHTTP(w, r)
+
+ if cfg.Verbose {
+ fmt.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
+ }
+ }
+ return http.HandlerFunc(fn)
+}
+
func main() {
+ mux := http.NewServeMux()
+
if !cfg.SinglePage {
- http.Handle("/", http.FileServer(http.Dir(cfg.Path)))
+ mux.Handle("/", http.FileServer(http.Dir(cfg.Path)))
} else {
fs := singlePageFileServer{http.Dir(cfg.Path)}
- http.Handle("/", http.FileServer(fs))
+ mux.Handle("/", http.FileServer(fs))
}
listen := fmt.Sprintf("%s:%d", cfg.ListenIP, cfg.ListenPort)
+ srv := &http.Server{
+ ReadTimeout: 60 * time.Second,
+ WriteTimeout: 60 * time.Second,
+ IdleTimeout: 60 * time.Second,
+ Addr: listen,
+ Handler: LogHandler(mux),
+ }
+
if !cfg.TLS {
- if err := http.ListenAndServe(listen, nil); err != nil {
+ if err := srv.ListenAndServe(); err != nil {
panic(err)
}
} else {
makeSelfSigned(cfg.Cert, cfg.Key)
- if err := http.ListenAndServeTLS(listen, cfg.Cert, cfg.Key, nil); err != nil {
+ if err := srv.ListenAndServeTLS(cfg.Cert, cfg.Key); err != nil {
panic(err)
}