47 lines
1.2 KiB
Go
Raw Normal View History

2025-03-06 22:47:02 +01:00
package pullrequest
import (
"net/http"
"forge.cadoles.com/wpetit/clearcase/internal/core/service"
httpCtx "forge.cadoles.com/wpetit/clearcase/internal/http/context"
"forge.cadoles.com/wpetit/clearcase/internal/http/handler/webui/common"
"forge.cadoles.com/wpetit/clearcase/internal/http/url"
"github.com/pkg/errors"
)
type Handler struct {
mux *http.ServeMux
forge *service.ForgeManager
}
// ServeHTTP implements http.Handler.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}
func NewHandler(forge *service.ForgeManager) *Handler {
h := &Handler{
mux: http.NewServeMux(),
forge: forge,
}
h.mux.HandleFunc("GET /", h.getPullRequestPage)
h.mux.HandleFunc("PUT /", h.handlePullRequestSummaryForm)
h.mux.HandleFunc("POST /", h.handlePullRequestForm)
return h
}
func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) {
if errors.Is(err, service.ErrForgeNotAvailable) {
baseURL := url.Mutate(httpCtx.BaseURL(r.Context()), url.WithPath("/auth/logout"))
http.Redirect(w, r, baseURL.String(), http.StatusSeeOther)
return
}
common.HandleError(w, r, errors.WithStack(err))
}
var _ http.Handler = &Handler{}