-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
46 lines (39 loc) · 1.15 KB
/
Copy pathmain.go
File metadata and controls
46 lines (39 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"binary-tree/handlers"
"flag"
"fmt"
"log"
"net/http"
)
var port int
func initConfig() {
// Define the port flag
flag.IntVar(&port, "port", 8080, "HTTP server port")
// Parse the flags
flag.Parse()
}
func main() {
initConfig()
mux := http.NewServeMux()
// add calculation endpoint
mux.HandleFunc("/calculate", handlers.CalculationHandler)
address := fmt.Sprintf(":%d", port)
log.Printf("Server started, listening on port %d\n", port)
panic(http.ListenAndServe(address, RecoveryMiddleware(mux)))
}
// RecoveryMiddleware is an HTTP middleware that recovers gracefully
// from panics in the HTTP handlers down the chain. Instead of crashing the server,
// it logs the error and responds with a generic "Internal server error" message.
func RecoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered from panic: %v", err)
// Send a generic server error message to the client
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}