mirror of
https://github.com/hanzo-fi/webhooks.git
synced 2026-10-30 20:32:55 +00:00
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/ThreeDotsLabs/watermill/message"
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/hanzo-fi/go-libs/v2/service"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/audit/httpaudit"
|
|
|
|
"github.com/hanzo-fi/go-libs/v2/auth"
|
|
"github.com/hanzo-fi/go-libs/v2/logging"
|
|
"github.com/hanzo-fi/webhooks/pkg/storage"
|
|
)
|
|
|
|
const (
|
|
PathHealthCheck = "/_healthcheck"
|
|
PathInfo = "/_info"
|
|
PathConfigs = "/configs"
|
|
PathTest = "/test"
|
|
PathActivate = "/activate"
|
|
PathDeactivate = "/deactivate"
|
|
PathChangeSecret = "/secret/change"
|
|
PathId = "/{" + PathParamId + "}"
|
|
PathParamId = "id"
|
|
)
|
|
|
|
type serverHandler struct {
|
|
*chi.Mux
|
|
|
|
store storage.Store
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func newServerHandler(
|
|
store storage.Store,
|
|
httpClient *http.Client,
|
|
logger logging.Logger,
|
|
info ServiceInfo,
|
|
authenticator auth.Authenticator,
|
|
publisher message.Publisher,
|
|
debug bool,
|
|
auditEnabled bool,
|
|
) http.Handler {
|
|
h := &serverHandler{
|
|
Mux: chi.NewRouter(),
|
|
store: store,
|
|
httpClient: httpClient,
|
|
}
|
|
|
|
if auditEnabled {
|
|
h.Use(httpaudit.Middleware(publisher, "audit-events", "webhooks", nil))
|
|
}
|
|
h.Use(func(handler http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
handler.ServeHTTP(w, r)
|
|
})
|
|
})
|
|
h.Use(func(handler http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
handler.ServeHTTP(w, r.WithContext(logging.ContextWithLogger(r.Context(), logger)))
|
|
})
|
|
})
|
|
h.Get(PathHealthCheck, h.HealthCheckHandle)
|
|
h.Get(PathInfo, h.getInfo(info))
|
|
|
|
h.Group(func(r chi.Router) {
|
|
r.Use(auth.Middleware(authenticator))
|
|
r.Use(service.OTLPMiddleware("webhooks", debug))
|
|
|
|
r.Get(PathConfigs, h.getManyConfigsHandle)
|
|
r.Post(PathConfigs, h.insertOneConfigHandle)
|
|
r.Delete(PathConfigs+PathId, h.deleteOneConfigHandle)
|
|
r.Put(PathConfigs+PathId, h.updateOneConfigHandle)
|
|
r.Get(PathConfigs+PathId+PathTest, h.testOneConfigHandle)
|
|
r.Put(PathConfigs+PathId+PathActivate, h.activateOneConfigHandle)
|
|
r.Put(PathConfigs+PathId+PathDeactivate, h.deactivateOneConfigHandle)
|
|
r.Put(PathConfigs+PathId+PathChangeSecret, h.changeSecretHandle)
|
|
})
|
|
|
|
return h
|
|
}
|