summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-06 15:36:36 -0500
committerJeff Carr <[email protected]>2025-09-06 16:04:51 -0500
commit0d1f53afee67293e930d9f67bd71442c154d196a (patch)
tree6d7d559e27aea94c1b746be9d3e3f4965bebc598 /main.go
parentd795dbeb610a8b0aa315e39175f574dd5ee0cc4e (diff)
all kindsa crazy. this needs massive cleanup
Diffstat (limited to 'main.go')
-rw-r--r--main.go81
1 files changed, 69 insertions, 12 deletions
diff --git a/main.go b/main.go
index eb2f68f..62b4241 100644
--- a/main.go
+++ b/main.go
@@ -76,21 +76,78 @@ func main() {
}
if argv.Daemon == true {
- log.Info("Running in --daemon mode")
- http.HandleFunc("/", okHandler)
- // go https() // use caddy instead
+ mux := http.NewServeMux()
+ okHandlerFunc := http.HandlerFunc(okHandler)
+
+ // Set a limit of 50 kilobytes for requests to this handler.
+ // Adjust this value to your needs.
+ const maxUploadSize = 1025 * 1024 // 1 MB
+ mux.Handle("/", http.MaxBytesHandler(okHandlerFunc, maxUploadSize))
+
p := fmt.Sprintf(":%d", argv.Port)
- log.Println(argv.Version(), "HOSTNAME set to:", HOSTNAME)
- log.Println("Running on port", "http://localhost"+p)
- log.Println("Running on port", "http://localhost"+p+"/ipv6.png")
- err := http.ListenAndServe(p, nil)
- if err != nil {
- log.Println("Error starting server:", err)
+ log.Printf("Server starting on port %s...\n", p)
+ log.Printf("Test with: curl -d 'hello world' http://localhost:%s/\n", p)
+
+ server := &http.Server{
+ Addr: p,
+ Handler: mux,
+ ReadTimeout: 5 * time.Minute,
+ WriteTimeout: 10 * time.Second,
+ IdleTimeout: 120 * time.Second,
}
- } else {
- log.Info("--daemon was not set. Just list the patches.")
- doList()
+
+ log.Printf("Server starting on port %s with a 1 MB request body limit...\n", p)
+ if err := server.ListenAndServe(); err != nil {
+ log.Fatal("Could not start server: %s\n", err)
+ }
+ okExit("")
}
+
+ /*
+ // --- Best Practice: Create a custom http.Server ---
+ server := &http.Server{
+ Addr: p,
+ Handler: mux,
+
+ // ReadTimeout is the total time to read the entire request, including the body.
+ // Increase this to a value that can accommodate your largest expected uploads.
+ // For example, 5 minutes.
+ ReadTimeout: 5 * time.Minute,
+
+ // WriteTimeout is the maximum duration before timing out writes of the response.
+ WriteTimeout: 10 * time.Second,
+
+ // IdleTimeout is the maximum amount of time to wait for the
+ // next request when keep-alives are enabled.
+ IdleTimeout: 120 * time.Second,
+ }
+ */
+
+ /*
+ log.Println(argv.Version(), "HOSTNAME set to:", HOSTNAME)
+ log.Println("Running on port", "http://localhost"+p)
+ log.Println("Running on port", "http://localhost"+p+"/ipv6.png")
+ // if err := http.ListenAndServe(p, nil); err != nil {
+ if err := server.ListenAndServe(); err != nil {
+ log.Fatalf("Could not start server: %s\n", err)
+ }
+ /*
+ log.Info("Running in --daemon mode")
+ http.HandleFunc("/", okHandler)
+ // go https() // use caddy instead
+ p := fmt.Sprintf(":%d", argv.Port)
+ log.Println(argv.Version(), "HOSTNAME set to:", HOSTNAME)
+ log.Println("Running on port", "http://localhost"+p)
+ log.Println("Running on port", "http://localhost"+p+"/ipv6.png")
+ err := http.ListenAndServe(p, nil)
+ if err != nil {
+ log.Println("Error starting server:", err)
+ }
+ return
+ }
+ */
+ log.Info("--daemon was not set. Just list the patches.")
+ // doList()
}
func formatDuration(d time.Duration) string {