gobin

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2023 License: Apache-2.0 Imports: 52 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WebhookEventUpdate string = "update"
	WebhookEventDelete string = "delete"
)

Variables

View Source
var (
	ErrNoPermissions     = errors.New("no permissions provided")
	ErrUnknownPermission = func(p Permission) error {
		return fmt.Errorf("unknown permission: %s", p)
	}
	ErrPermissionDenied = func(p Permission) error {
		return fmt.Errorf("permission denied: %s", p)
	}
)
View Source
var (
	ErrDocumentNotFound = errors.New("document not found")
	ErrRateLimit        = errors.New("rate limit exceeded")
	ErrEmptyBody        = errors.New("empty request body")
	ErrContentTooLarge  = func(maxLength int) error {
		return fmt.Errorf("content too large, must be less than %d chars", maxLength)
	}
)
View Source
var (
	ErrWebhookNotFound            = errors.New("webhook not found")
	ErrMissingWebhookSecret       = errors.New("missing webhook secret")
	ErrMissingWebhookURL          = errors.New("missing webhook url")
	ErrMissingWebhookEvents       = errors.New("missing webhook events")
	ErrMissingURLOrSecretOrEvents = errors.New("missing url, secret or events")
)
View Source
var VersionTimeFormat = "2006-01-02 15:04:05"

Functions

func FormatBuildVersion

func FormatBuildVersion(version string, commit string, buildTime time.Time) string

func GetWebhookSecret

func GetWebhookSecret(r *http.Request) string

Types

type Claims

type Claims struct {
	jwt.Claims
	Permissions []Permission `json:"permissions"`
}

func GetClaims

func GetClaims(r *http.Request) Claims

type Config

type Config struct {
	Log              LogConfig        `cfg:"log"`
	Debug            bool             `cfg:"debug"`
	DevMode          bool             `cfg:"dev_mode"`
	ListenAddr       string           `cfg:"listen_addr"`
	HTTPTimeout      time.Duration    `cfg:"http_timeout"`
	Database         DatabaseConfig   `cfg:"database"`
	MaxDocumentSize  int              `cfg:"max_document_size"`
	MaxHighlightSize int              `cfg:"max_highlight_size"`
	RateLimit        *RateLimitConfig `cfg:"rate_limit"`
	JWTSecret        string           `cfg:"jwt_secret"`
	Preview          *PreviewConfig   `cfg:"preview"`
	Otel             *OtelConfig      `cfg:"otel"`
	Webhook          *WebhookConfig   `cfg:"webhook"`
	CustomStyles     string           `cfg:"custom_styles"`
	DefaultStyle     string           `cfg:"default_style"`
}

func (Config) String

func (c Config) String() string

type DB

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

func NewDB

func NewDB(ctx context.Context, cfg DatabaseConfig, schema string) (*DB, error)

func (*DB) Close

func (d *DB) Close() error

func (*DB) CreateDocument

func (d *DB) CreateDocument(ctx context.Context, content string, language string) (Document, error)

func (*DB) CreateWebhook

func (d *DB) CreateWebhook(ctx context.Context, documentID string, url string, secret string, events []string) (*Webhook, error)

func (*DB) DeleteDocument

func (d *DB) DeleteDocument(ctx context.Context, documentID string) (Document, error)

func (*DB) DeleteDocumentByVersion

func (d *DB) DeleteDocumentByVersion(ctx context.Context, documentID string, version int64) (Document, error)

func (*DB) DeleteExpiredDocuments

func (d *DB) DeleteExpiredDocuments(ctx context.Context, expireAfter time.Duration) error

func (*DB) DeleteWebhook

func (d *DB) DeleteWebhook(ctx context.Context, documentID string, webhookID string, secret string) error

func (*DB) GetAndDeleteWebhooksByDocumentID

func (d *DB) GetAndDeleteWebhooksByDocumentID(ctx context.Context, documentID string) ([]Webhook, error)

func (*DB) GetDocument

func (d *DB) GetDocument(ctx context.Context, documentID string) (Document, error)

func (*DB) GetDocumentVersion

func (d *DB) GetDocumentVersion(ctx context.Context, documentID string, version int64) (Document, error)

func (*DB) GetDocumentVersions

func (d *DB) GetDocumentVersions(ctx context.Context, documentID string, withContent bool) ([]Document, error)

func (*DB) GetVersionCount

func (d *DB) GetVersionCount(ctx context.Context, documentID string) (int, error)

func (*DB) GetWebhook

func (d *DB) GetWebhook(ctx context.Context, documentID string, webhookID string, secret string) (*Webhook, error)

func (*DB) GetWebhooksByDocumentID

func (d *DB) GetWebhooksByDocumentID(ctx context.Context, documentID string) ([]Webhook, error)

func (*DB) UpdateDocument

func (d *DB) UpdateDocument(ctx context.Context, documentID string, content string, language string) (Document, error)

func (*DB) UpdateWebhook

func (d *DB) UpdateWebhook(ctx context.Context, documentID string, webhookID string, secret string, newURL string, newSecret string, newEvents []string) (*Webhook, error)

type DatabaseConfig

type DatabaseConfig struct {
	Type            string        `cfg:"type"`
	Debug           bool          `cfg:"debug"`
	ExpireAfter     time.Duration `cfg:"expire_after"`
	CleanupInterval time.Duration `cfg:"cleanup_interval"`

	// SQLite
	Path string `cfg:"path"`

	// PostgreSQL
	Host     string `cfg:"host"`
	Port     int    `cfg:"port"`
	Username string `cfg:"username"`
	Password string `cfg:"password"`
	Database string `cfg:"database"`
	SSLMode  string `cfg:"ssl_mode"`
}

func (DatabaseConfig) PostgresDataSourceName

func (c DatabaseConfig) PostgresDataSourceName() string

func (DatabaseConfig) String

func (c DatabaseConfig) String() string

type DeleteResponse

type DeleteResponse struct {
	Versions int `json:"versions"`
}

type Document

type Document struct {
	ID       string `db:"id"`
	Version  int64  `db:"version"`
	Content  string `db:"content"`
	Language string `db:"language"`
}

type DocumentResponse

type DocumentResponse struct {
	Key          string `json:"key,omitempty"`
	Version      int64  `json:"version"`
	VersionLabel string `json:"version_label,omitempty"`
	VersionTime  string `json:"version_time,omitempty"`
	Data         string `json:"data,omitempty"`
	Formatted    string `json:"formatted,omitempty"`
	CSS          string `json:"css,omitempty"`
	ThemeCSS     string `json:"theme_css,omitempty"`
	Language     string `json:"language"`
	Token        string `json:"token,omitempty"`
}

type ErrorResponse

type ErrorResponse struct {
	Message   string `json:"message"`
	Status    int    `json:"status"`
	Path      string `json:"path"`
	RequestID string `json:"request_id"`
}

type LogConfig

type LogConfig struct {
	Level     slog.Level `cfg:"level"`
	Format    string     `cfg:"format"`
	AddSource bool       `cfg:"add_source"`
	NoColor   bool       `cfg:"no_color"`
}

func (LogConfig) String

func (c LogConfig) String() string

type MetricsConfig

type MetricsConfig struct {
	ListenAddr string `cfg:"listen_addr"`
}

func (MetricsConfig) String

func (c MetricsConfig) String() string

type OtelConfig

type OtelConfig struct {
	InstanceID string         `cfg:"instance_id"`
	Trace      *TraceConfig   `cfg:"trace"`
	Metrics    *MetricsConfig `cfg:"metrics"`
}

func (OtelConfig) String

func (c OtelConfig) String() string

type Permission

type Permission string
const (
	PermissionWrite   Permission = "write"
	PermissionDelete  Permission = "delete"
	PermissionShare   Permission = "share"
	PermissionWebhook Permission = "webhook"
)

func (Permission) IsValid

func (p Permission) IsValid() bool

type PreviewConfig

type PreviewConfig struct {
	InkscapePath string        `cfg:"inkscape_path"`
	MaxLines     int           `cfg:"max_lines"`
	DPI          int           `cfg:"dpi"`
	CacheSize    int           `cfg:"cache_size"`
	CacheTTL     time.Duration `cfg:"cache_ttl"`
}

func (PreviewConfig) String

func (c PreviewConfig) String() string

type RateLimitConfig

type RateLimitConfig struct {
	Requests  int           `cfg:"requests"`
	Duration  time.Duration `cfg:"duration"`
	Whitelist []string      `cfg:"whitelist"`
	Blacklist []string      `cfg:"blacklist"`
}

func (RateLimitConfig) String

func (c RateLimitConfig) String() string

type Server

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

func NewServer

func NewServer(version string, debug bool, cfg Config, db *DB, signer jose.Signer, tracer trace.Tracer, meter metric.Meter, assets http.FileSystem, htmlFormatter *html.Formatter) *Server

func (*Server) Close

func (s *Server) Close()

func (*Server) DeleteDocument

func (s *Server) DeleteDocument(w http.ResponseWriter, r *http.Request)

func (*Server) DeleteDocumentWebhook

func (s *Server) DeleteDocumentWebhook(w http.ResponseWriter, r *http.Request)

func (*Server) DocumentVersions

func (s *Server) DocumentVersions(w http.ResponseWriter, r *http.Request)

func (*Server) ExecuteWebhooks

func (s *Server) ExecuteWebhooks(ctx context.Context, event string, document WebhookDocument)

func (*Server) GetDocument

func (s *Server) GetDocument(w http.ResponseWriter, r *http.Request)

func (*Server) GetDocumentPreview

func (s *Server) GetDocumentPreview(w http.ResponseWriter, r *http.Request)

func (*Server) GetDocumentWebhook

func (s *Server) GetDocumentWebhook(w http.ResponseWriter, r *http.Request)

func (*Server) GetPrettyDocument

func (s *Server) GetPrettyDocument(w http.ResponseWriter, r *http.Request)

func (*Server) GetRawDocument

func (s *Server) GetRawDocument(w http.ResponseWriter, r *http.Request)

func (*Server) GetVersion

func (s *Server) GetVersion(w http.ResponseWriter, _ *http.Request)

func (*Server) JWTMiddleware

func (s *Server) JWTMiddleware(next http.Handler) http.Handler

func (*Server) NewToken

func (s *Server) NewToken(documentID string, permissions []Permission) (string, error)

func (*Server) PatchDocument

func (s *Server) PatchDocument(w http.ResponseWriter, r *http.Request)

func (*Server) PatchDocumentWebhook

func (s *Server) PatchDocumentWebhook(w http.ResponseWriter, r *http.Request)

func (*Server) PostDocument

func (s *Server) PostDocument(w http.ResponseWriter, r *http.Request)

func (*Server) PostDocumentShare

func (s *Server) PostDocumentShare(w http.ResponseWriter, r *http.Request)

func (*Server) PostDocumentWebhook

func (s *Server) PostDocumentWebhook(w http.ResponseWriter, r *http.Request)

func (*Server) RateLimit

func (s *Server) RateLimit(next http.Handler) http.Handler

func (*Server) Routes

func (s *Server) Routes() http.Handler

func (*Server) Start

func (s *Server) Start()

func (*Server) StyleCSS

func (s *Server) StyleCSS(w http.ResponseWriter, r *http.Request)

type ShareRequest

type ShareRequest struct {
	Permissions []Permission `json:"permissions"`
}

type ShareResponse

type ShareResponse struct {
	Token string `json:"token"`
}

type TraceConfig

type TraceConfig struct {
	Endpoint string `cfg:"endpoint"`
	Insecure bool   `cfg:"insecure"`
}

func (TraceConfig) String

func (c TraceConfig) String() string

type Webhook

type Webhook struct {
	ID         string `db:"id"`
	DocumentID string `db:"document_id"`
	URL        string `db:"url"`
	Secret     string `db:"secret"`
	Events     string `db:"events"`
}

type WebhookConfig

type WebhookConfig struct {
	Timeout       time.Duration `cfg:"timeout"`
	MaxTries      int           `cfg:"max_tries"`
	Backoff       time.Duration `cfg:"backoff"`
	BackoffFactor float64       `cfg:"backoff_factor"`
	MaxBackoff    time.Duration `cfg:"max_backoff"`
}

func (WebhookConfig) String

func (c WebhookConfig) String() string

type WebhookCreateRequest

type WebhookCreateRequest struct {
	URL    string   `json:"url"`
	Secret string   `json:"secret"`
	Events []string `json:"events"`
}

type WebhookDocument

type WebhookDocument struct {
	Key      string `json:"key"`
	Version  int64  `json:"version"`
	Language string `json:"language"`
	Data     string `json:"data"`
}

type WebhookEventRequest

type WebhookEventRequest struct {
	WebhookID string          `json:"webhook_id"`
	Event     string          `json:"event"`
	CreatedAt time.Time       `json:"created_at"`
	Document  WebhookDocument `json:"document"`
}

type WebhookResponse

type WebhookResponse struct {
	ID          string   `json:"id"`
	DocumentKey string   `json:"document_key"`
	URL         string   `json:"url"`
	Secret      string   `json:"secret"`
	Events      []string `json:"events"`
}

type WebhookUpdate

type WebhookUpdate struct {
	ID         string `db:"id"`
	DocumentID string `db:"document_id"`
	Secret     string `db:"secret"`

	NewURL    string `db:"new_url"`
	NewSecret string `db:"new_secret"`
	NewEvents string `db:"new_events"`
}

type WebhookUpdateRequest

type WebhookUpdateRequest struct {
	URL    string   `json:"url"`
	Secret string   `json:"secret"`
	Events []string `json:"events"`
}

Jump to

Keyboard shortcuts

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