Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package webhook
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gochat/gochat/internal/channel"
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// WebhookHandler processes incoming webhook events from external channels.
|
|
type WebhookHandler struct {
|
|
registry *channel.ChannelRegistry
|
|
}
|
|
|
|
// NewHandler creates a new WebhookHandler.
|
|
func NewHandler(registry *channel.ChannelRegistry) *WebhookHandler {
|
|
return &WebhookHandler{registry: registry}
|
|
}
|
|
|
|
// Handle processes an incoming webhook request.
|
|
// The URL pattern is /webhooks/:channel_type/:inbox_id
|
|
// The handler looks up the ChannelProvider for the given channel_type
|
|
// and delegates processing to that provider's ProcessIncoming method.
|
|
func (h *WebhookHandler) Handle(c *gin.Context) {
|
|
channelType := c.Param("channel_type")
|
|
inboxIDStr := c.Param("inbox_id")
|
|
|
|
provider, err := h.registry.Get(channel.ChannelType(channelType))
|
|
if err != nil || provider == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "unknown channel type: " + channelType,
|
|
})
|
|
return
|
|
}
|
|
|
|
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
|
|
return
|
|
}
|
|
|
|
rawPayload, err := c.GetRawData()
|
|
if err != nil {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "failed to read payload"})
|
|
return
|
|
}
|
|
|
|
// Construct an Inbox stub for the provider.
|
|
inbox := &model.Inbox{Base: model.Base{ID: uint(inboxID)}}
|
|
|
|
msg, err := provider.ProcessIncoming(c.Request.Context(), inbox, rawPayload)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "processed",
|
|
"conversation_id": msg.ConversationID,
|
|
})
|
|
} |