88 "net/http"
99 "os"
1010 "os/signal"
11+ "runtime/debug"
1112 "sync"
1213 "syscall"
1314
@@ -48,6 +49,18 @@ const (
4849 HeklaBlock ProveMode = "hekla" // deprecated
4950)
5051
52+ func recoverMiddleware (next http.Handler ) http.Handler {
53+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
54+ defer func () {
55+ if err := recover (); err != nil {
56+ fmt .Printf ("panic: %v\n %s" , err , debug .Stack ())
57+ http .Error (w , "Internal Server Error" , http .StatusInternalServerError )
58+ }
59+ }()
60+ next .ServeHTTP (w , r )
61+ })
62+ }
63+
5164func proveHandler (ctx context.Context , args * flags.Arguments , sgxProver * prover.SGXProver , w http.ResponseWriter , r * http.Request , proveMode ProveMode ) {
5265 contentType := r .Header .Get ("Content-Type" )
5366 if contentType != "application/json" {
@@ -112,7 +125,9 @@ func runServer(c *cli.Context) error {
112125 port = "8080"
113126 }
114127 args := flags .NewArguments (c )
115- http .HandleFunc ("POST /prove/{action}" , func (w http.ResponseWriter , r * http.Request ) {
128+
129+ mux := http .NewServeMux ()
130+ mux .HandleFunc ("POST /prove/{action}" , func (w http.ResponseWriter , r * http.Request ) {
116131 args := args .Copy ()
117132 defer r .Body .Close ()
118133 // override the proof writer to get the proof & return as response
@@ -136,7 +151,8 @@ func runServer(c *cli.Context) error {
136151 })
137152
138153 server := & http.Server {
139- Addr : ":" + port ,
154+ Addr : ":" + port ,
155+ Handler : recoverMiddleware (mux ),
140156 }
141157
142158 go func () {
0 commit comments