webhook

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package webhook provides HTTP webhook handling for Notion events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Author

type Author struct {
	ID   string `json:"id"`
	Type string `json:"type"` // "person" or "bot"
}

Author represents a user who triggered the event.

type Entity

type Entity struct {
	ID   string `json:"id"`
	Type string `json:"type"` // "page", "database", etc.
}

Entity represents the affected object (page, database, etc.).

type Event

type Event struct {
	ID                string    `json:"id"`                           // Event ID
	Type              string    `json:"type"`                         // Event type (e.g., "page.content_updated")
	Timestamp         string    `json:"timestamp,omitempty"`          // ISO 8601 timestamp
	WorkspaceID       string    `json:"workspace_id,omitempty"`       // Workspace ID
	WorkspaceName     string    `json:"workspace_name,omitempty"`     // Workspace name
	SubscriptionID    string    `json:"subscription_id,omitempty"`    // Webhook subscription ID
	IntegrationID     string    `json:"integration_id,omitempty"`     // Integration ID
	AttemptNumber     int       `json:"attempt_number,omitempty"`     // Delivery attempt number
	APIVersion        string    `json:"api_version,omitempty"`        // Notion API version
	Authors           []Author  `json:"authors,omitempty"`            // Users who triggered the event
	Entity            *Entity   `json:"entity,omitempty"`             // The affected entity (page, database, etc.)
	Data              EventData `json:"data"`                         // Event-specific data
	VerificationToken string    `json:"verification_token,omitempty"` // For URL verification requests
}

Event represents a Notion webhook event payload.

func (*Event) GetEntityID

func (e *Event) GetEntityID() string

GetEntityID returns the entity ID from the event.

func (*Event) GetEntityType

func (e *Event) GetEntityType() string

GetEntityType returns the entity type from the event.

type EventData

type EventData struct {
	Parent        *Parent        `json:"parent,omitempty"`
	UpdatedBlocks []UpdatedBlock `json:"updated_blocks,omitempty"` // For content_updated events
}

EventData contains event-specific details.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles incoming webhook requests.

func NewHandler

func NewHandler(
	queueManager *queue.Manager,
	storeInst store.Store,
	secret string,
	autoSync bool,
	logger *slog.Logger,
	syncWorker *SyncWorker,
	remoteConfig *store.RemoteConfig,
) *Handler

NewHandler creates a new webhook handler. If syncWorker is nil, automatic background sync is disabled.

func (*Handler) HandleHealth added in v0.3.0

func (h *Handler) HandleHealth(writer http.ResponseWriter, req *http.Request)

HandleHealth handles the /health endpoint for health checks.

func (*Handler) HandleVersion

func (h *Handler) HandleVersion(writer http.ResponseWriter, req *http.Request)

HandleVersion handles the /api/version endpoint.

func (*Handler) HandleWebhook

func (h *Handler) HandleWebhook(writer http.ResponseWriter, req *http.Request)

HandleWebhook handles incoming webhook requests.

type Parent

type Parent struct {
	ID   string `json:"id"`
	Type string `json:"type"` // "workspace", "page", "database", "team", etc.
}

Parent contains parent information.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents the webhook HTTP server.

func NewServer

func NewServer(
	cfg *ServerConfig,
	queueManager *queue.Manager,
	storeInst store.Store,
	logger *slog.Logger,
	syncWorker *SyncWorker,
	remoteConfig *store.RemoteConfig,
) *Server

NewServer creates a new webhook server. If syncWorker is not nil, it will be started alongside the HTTP server.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the server's address. Useful for testing.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the HTTP server. This method blocks until the server is stopped.

type ServerConfig

type ServerConfig struct {
	Port      int           // HTTP port to listen on (NTN_WEBHOOK_PORT, default 8080)
	Path      string        // Webhook endpoint path (NTN_WEBHOOK_PATH, default /webhooks/notion)
	Secret    string        // Webhook secret for signature verification (NTN_WEBHOOK_SECRET, optional)
	AutoSync  bool          // Automatically run sync after queuing webhook events (NTN_WEBHOOK_AUTO_SYNC, default true)
	SyncDelay time.Duration // Delay before processing queue (NTN_WEBHOOK_SYNC_DELAY, default 0)
}

ServerConfig holds configuration for the webhook server.

func LoadConfigFromEnv

func LoadConfigFromEnv() *ServerConfig

LoadConfigFromEnv loads webhook configuration from environment variables.

func (*ServerConfig) IsValid

func (c *ServerConfig) IsValid() bool

IsValid returns true if the configuration is valid. Secret is optional (signature verification is skipped if not set).

type SyncWorker

type SyncWorker struct {
	// contains filtered or unexported fields
}

SyncWorker processes queued items in the background.

func NewSyncWorker

func NewSyncWorker(
	crawler *sync.Crawler,
	localStore *store.LocalStore,
	remoteConfig *store.RemoteConfig,
	logger *slog.Logger,
	opts ...SyncWorkerOption,
) *SyncWorker

NewSyncWorker creates a new sync worker.

func (*SyncWorker) Notify

func (w *SyncWorker) Notify()

Notify signals that there is new work to process. This is non-blocking - if a notification is already pending, it's a no-op.

func (*SyncWorker) Start

func (w *SyncWorker) Start(ctx context.Context)

Start runs the sync worker until the context is canceled or a fatal error occurs. This method blocks and should be called in a goroutine.

type SyncWorkerOption

type SyncWorkerOption func(*SyncWorker)

SyncWorkerOption configures the SyncWorker.

func WithSyncDelay

func WithSyncDelay(d time.Duration) SyncWorkerOption

WithSyncDelay sets the debounce delay before processing. This allows multiple rapid notifications to coalesce into a single sync.

type UpdatedBlock

type UpdatedBlock struct {
	ID   string `json:"id"`
	Type string `json:"type"`
}

UpdatedBlock represents a block that was updated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL