50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"code.gitea.io/sdk/gitea"
|
|
"encoding/json"
|
|
"git.lumeweb.com/LumeWeb/gitea-github-proxy/config"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type webhookApi struct {
|
|
config *config.Config
|
|
logger *zap.Logger
|
|
db *gorm.DB
|
|
}
|
|
|
|
func newWebhookApi(cfg *config.Config, db *gorm.DB, logger *zap.Logger) *webhookApi {
|
|
return &webhookApi{config: cfg, db: db, logger: logger}
|
|
}
|
|
|
|
func (a *webhookApi) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
var webhook map[string]interface{}
|
|
err := json.Unmarshal(body, &webhook)
|
|
if err != nil {
|
|
a.logger.Error("Failed to unmarshal webhook", zap.Error(err))
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
a.logger.Debug("Received webhook", zap.Any("webhook", webhook))
|
|
}
|
|
|
|
func setupWebhookRoutes(params RouteParams) {
|
|
cfg := params.Config
|
|
db := params.Db
|
|
logger := params.Logger
|
|
r := params.R
|
|
|
|
webhookApi := newWebhookApi(cfg, db, logger)
|
|
webhookRouter := r.PathPrefix("/api").Subrouter()
|
|
|
|
webhookRouter.Use(gitea.VerifyWebhookSignatureMiddleware(cfg.GiteaWebHookSecret))
|
|
|
|
webhookRouter.HandleFunc("/webhook", webhookApi.handleWebhook).Methods("POST")
|
|
}
|