(f handlerFunc)
| 644 | } |
| 645 | |
| 646 | func (h *Handler) AllReader(f handlerFunc) http.HandlerFunc { |
| 647 | return func(w http.ResponseWriter, r *http.Request) { |
| 648 | h.handleError(w, r, func() error { |
| 649 | status := 200 |
| 650 | start := time.Now() |
| 651 | |
| 652 | defer func() { |
| 653 | if e := recover(); e != nil { |
| 654 | log.Error("%s:\n%s", e, debug.Stack()) |
| 655 | impart.WriteError(w, impart.HTTPError{http.StatusInternalServerError, "Something didn't work quite right."}) |
| 656 | status = 500 |
| 657 | } |
| 658 | |
| 659 | log.Info(h.app.ReqLog(r, status, time.Since(start))) |
| 660 | }() |
| 661 | |
| 662 | // Allow any origin, as public endpoints are handled in here |
| 663 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 664 | |
| 665 | if h.app.App().cfg.App.Private { |
| 666 | // This instance is private, so ensure it's being accessed by a valid user |
| 667 | // Check if authenticated with an access token |
| 668 | _, apiErr := optionalAPIAuth(h.app.App(), r) |
| 669 | if apiErr != nil { |
| 670 | if err, ok := apiErr.(impart.HTTPError); ok { |
| 671 | status = err.Status |
| 672 | } else { |
| 673 | status = 500 |
| 674 | } |
| 675 | |
| 676 | if apiErr == ErrNotLoggedIn { |
| 677 | // Fall back to web auth since there was no access token given |
| 678 | _, err := webAuth(h.app.App(), r) |
| 679 | if err != nil { |
| 680 | if err, ok := apiErr.(impart.HTTPError); ok { |
| 681 | status = err.Status |
| 682 | } else { |
| 683 | status = 500 |
| 684 | } |
| 685 | return err |
| 686 | } |
| 687 | } else { |
| 688 | return apiErr |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | err := f(h.app.App(), w, r) |
| 694 | if err != nil { |
| 695 | if err, ok := err.(impart.HTTPError); ok { |
| 696 | status = err.Status |
| 697 | } else { |
| 698 | status = 500 |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return err |
| 703 | }()) |
no test coverage detected