Files
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
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.
2026-07-07 14:44:12 +08:00

83 lines
2.5 KiB
Go

package line
import (
"encoding/json"
"io"
"net/http"
"github.com/gochat/gochat/internal/model"
applogger "github.com/gochat/gochat/pkg/logger"
)
// WebhookHandler handles LINE webhook HTTP requests.
type WebhookHandler struct {
pipeline *IncomingProcessor
service *LineService
}
// NewWebhookHandler creates a LINE webhook handler.
func NewWebhookHandler(pipeline *IncomingProcessor, service *LineService) *WebhookHandler {
return &WebhookHandler{
pipeline: pipeline,
service: service,
}
}
// HandleWebhook processes an incoming LINE webhook HTTP request.
func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request, inbox *model.Inbox) {
// Read request body for signature verification
body, err := io.ReadAll(r.Body)
if err != nil {
applogger.L().Errorf("LINE HandleWebhook: failed to read body: %v", err)
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
// Verify webhook signature
config := parseInboxConfig(inbox.ChannelConfig)
channelSecret := configStr(config, "channel_secret", "")
signature := r.Header.Get("X-Line-Signature")
if channelSecret != "" && signature != "" {
if !h.service.VerifySignature(channelSecret, string(body), signature) {
applogger.L().Warnf("LINE HandleWebhook: invalid signature for inbox=%d", inbox.ID)
http.Error(w, "Invalid signature", http.StatusUnauthorized)
return
}
}
// Parse webhook event
var webhookEvent WebhookEvent
if err := json.Unmarshal(body, &webhookEvent); err != nil {
applogger.L().Errorf("LINE HandleWebhook: failed to parse JSON: %v", err)
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Process each event
for _, event := range webhookEvent.Events {
ctx := r.Context()
incomingMsg, err := h.pipeline.ProcessEvent(ctx, inbox, event)
if err != nil {
applogger.L().Errorf("LINE HandleWebhook: process event failed: %v", err)
continue
}
if incomingMsg == nil {
continue // unhandled event type
}
// Log processed event
applogger.L().Debugf("LINE HandleWebhook: processed event type=%s sender=%s",
event.Type, incomingMsg.SenderID)
}
w.WriteHeader(http.StatusOK)
}
// HandleWebhookVerification responds to LINE webhook URL verification requests.
func (h *WebhookHandler) HandleWebhookVerification(w http.ResponseWriter, r *http.Request) {
// LINE webhook verification is handled during the initial setup
// The verification endpoint returns a 200 OK to confirm the webhook URL
w.WriteHeader(http.StatusOK)
}