func (s *server) makeHTTPHandler( ctx types.Context, route types.Route) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set(types.ServerNameHeader, s.name) ctx := context.WithRequestRoute(ctx, req, route) if req.TLS != nil { if len(req.TLS.PeerCertificates) > 0 { userName := req.TLS.PeerCertificates[0].Subject.CommonName ctx = ctx.WithValue(context.UserKey, userName) } } ctx.Info("http request") vars := mux.Vars(req) if vars == nil { vars = map[string]string{} } store := utils.NewStoreWithVars(vars) handlerFunc := s.handleWithMiddleware(ctx, route) if err := handlerFunc(ctx, w, req, store); err != nil { ctx.Error(err) http.Error(w, err.Error(), http.StatusInternalServerError) } } }
// TrapSignals tells the process to trap incoming process signals. func TrapSignals(ctx apitypes.Context) { context.RegisterCustomKey(signalContextKey, context.CustomLoggerKey) sigc := make(chan os.Signal, 1) signal.Notify(sigc) go func() { for s := range sigc { ctx := ctx.WithValue(signalContextKey, s.String()) if ok, graceful := IsExitSignal(s); ok && !graceful { ctx.Error("received signal; aborting") os.Exit(1) } func() { sigHandlersRWL.RLock() defer sigHandlersRWL.RUnlock() // execute the signal handlers in reverse order. the first // one registered should be executed last as it was registered // the earliest for i := len(sigHandlers) - 1; i >= 0; i-- { sigHandlers[i](ctx, s) } }() if ok, graceful := IsExitSignal(s); ok && graceful { ctx.Error("received signal; shutting down") os.Exit(0) } } }() }
// Handle is the type's Handler function. func (h *errorHandler) Handle( ctx types.Context, w http.ResponseWriter, req *http.Request, store types.Store) error { err := h.handler(ctx, w, req, store) if err == nil { return nil } ctx.Error(err) httpErr := goof.NewHTTPError(err, getStatus(err)) httputils.WriteJSON(w, httpErr.Status(), httpErr) return nil }