httpapi

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0, MIT Imports: 42 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpapi serves WAHA's HTTP API. Each WAHA tag has its own file and registers its routes through Server.route, which authenticates, resolves the session the route acts on, checks the key's policy and, for routes that need a running session, waits for it the way WAHA does.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Environment

func Environment(worker string) waha.Environment

Environment builds the environment block for a worker.

func HashKey

func HashKey(key string) string

HashKey is how a stored key is looked up.

Types

type APIKeyDTO

type APIKeyDTO struct {
	ID       string          `json:"id"`
	Key      string          `json:"key"`
	IsActive bool            `json:"isActive"`
	IsAdmin  bool            `json:"isAdmin"`
	Session  *string         `json:"session"`
	Sessions []string        `json:"sessions"`
	Actions  json.RawMessage `json:"actions"`
}

APIKeyDTO is WAHA's ApiKeyDTO. Key is the plain key only in the answer that creates it; listings show it masked, because the store keeps only a hash (a deliberate difference from WAHA, which returns stored keys in full). Sessions extends it: a key may cover several sessions, and WAHA's Session is set only when it covers exactly one.

type Action

type Action string

Action is what a route needs from a session key (WAHA's SessionActions), plus the two server-level grants.

const (
	ActRead     Action = "read"
	ActSend     Action = "send"
	ActControl  Action = "control"
	ActSetting  Action = "setting"
	ActDelete   Action = "delete"
	ActApp      Action = "app"
	ActManage   Action = "manage"   // admin only: keys, server control, /health
	ActRetrieve Action = "retrieve" // any key: server version and status
	ActList     Action = "list"     // any key: the session list, filtered
)

The actions a route can require.

type Ctx

type Ctx struct {
	W         http.ResponseWriter
	R         *http.Request
	S         *Server
	Principal Principal
	Session   string // the session name the route acts on ("" for NoSession)
	Body      []byte // the request body, read once
	// contains filtered or unexported fields
}

Ctx is what a handler gets.

func (*Ctx) Account

func (c *Ctx) Account() (*pb.AccountRef, error)

Account is the core's reference to the route's session account.

func (*Ctx) Bind

func (c *Ctx) Bind(v any) error

Bind decodes the JSON body into v; a body that is not JSON is a 400.

func (*Ctx) Empty

func (c *Ctx) Empty(status int) error

Empty writes a status with no body, as NestJS does for a handler that returns nothing.

func (*Ctx) JSON

func (c *Ctx) JSON(status int, v any) error

JSON writes a JSON response.

func (*Ctx) Param

func (c *Ctx) Param(name string) string

Param is a path parameter.

func (*Ctx) Query

func (c *Ctx) Query(name string) string

Query is a query parameter.

func (*Ctx) Sess

func (c *Ctx) Sess() (store.Session, error)

Sess is the route's session, loaded once. A route that is not Working gets the 404 of WAHA's session routes for an unknown session.

type Error

type Error struct {
	Status int
	Body   any
	// contains filtered or unexported fields
}

Error is a response other than success. Body is written as JSON verbatim: WAHA answers some errors with NestJS's {message, error, statusCode} and others with a bare object, and clients match on both.

func ErrBadRequest

func ErrBadRequest(message any) *Error

ErrBadRequest is a 400; message is a string or a list of strings.

func ErrForbidden

func ErrForbidden() *Error

ErrForbidden is WAHA's bare 403.

func ErrInternal

func ErrInternal(err error, r *http.Request) *Error

ErrInternal is a 500 in WAHA's shape, minus the request body echo (which would hand base64 media and secrets back to any caller that triggers it).

func ErrNotFound

func ErrNotFound(message string) *Error

ErrNotFound is a 404.

func ErrNotImplemented

func ErrNotImplemented(why string) *Error

ErrNotImplemented is WAHA's "not implemented by engine" 501. why, when set, names what wamux lacks, ahead of WAHA's sentence.

func ErrSessionMissing

func ErrSessionMissing(name string) *Error

ErrSessionMissing is the 422 a working-session route gives for an unknown session.

func ErrSessionNotFound

func ErrSessionNotFound() *Error

ErrSessionNotFound is the 404 of the session CRUD routes.

func ErrSessionStatus

func ErrSessionStatus(name, status string, expected ...string) *Error

ErrSessionStatus is the 422 for a session that is not in an expected status.

func ErrUnauthorized

func ErrUnauthorized() *Error

ErrUnauthorized is WAHA's bare 401.

func ErrUnprocessable

func ErrUnprocessable(message string) *Error

ErrUnprocessable is a 422 with NestJS's string body.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Handler

type Handler func(c *Ctx) error

Handler is a route's implementation. A returned error becomes the response.

type Principal

type Principal struct {
	Admin    bool
	Sessions []string        // the sessions a non-admin key may touch
	Actions  map[string]bool // nil means WAHA's defaults
}

Principal is who a request authenticated as.

func (Principal) Allows

func (p Principal) Allows(a Action, session string) bool

Allows applies WAHA's policy for one action on one session.

func (Principal) Covers

func (p Principal) Covers(session string) bool

Covers reports whether the principal may touch the session at all.

type Route

type Route struct {
	Action  Action
	From    SessionFrom
	Working bool // needs the session in WORKING
}

Route is one route's policy.

type SentMessage

type SentMessage struct {
	ID          string          `json:"id"`
	Timestamp   int64           `json:"timestamp"`
	From        string          `json:"from"`
	FromMe      bool            `json:"fromMe"`
	Source      string          `json:"source"`
	To          *string         `json:"to"`
	Participant *string         `json:"participant"`
	Body        *string         `json:"body"`
	HasMedia    bool            `json:"hasMedia"`
	Ack         int             `json:"ack"`
	AckName     string          `json:"ackName"`
	Data        json.RawMessage `json:"_data"`
}

SentMessage is WAHA's answer to a send. GOWS answers only `id` and `_data`; WAHA's document (and its other engines) answer a WAMessage. This is both: a WAMessage, with `_data` the core's own result.

type Server

type Server struct {
	Config   *config.Config
	Store    store.Store
	Core     *core.Client
	Sessions *sessions.Manager
	Pub      *events.Publisher
	Media    *media.Service
	Tail     *events.Tail
	Log      *slog.Logger
	Started  time.Time
	Shutdown func() // POST /api/server/stop; nil disables the route

	// How long a working-session route waits for the session to exist and
	// to be WORKING before answering 422 (WAHA: 5 s and 10 s).
	WaitExists, WaitStatus time.Duration
	// contains filtered or unexported fields
}

Server is the HTTP API and everything its handlers reach.

func (*Server) Coverage

func (s *Server) Coverage() []string

Coverage lists the WAHA operations that answer 501, as "METHOD /path".

func (*Server) Router

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

Router builds the whole HTTP handler.

type SessionDTO

type SessionDTO struct {
	Name   string          `json:"name"`
	Status string          `json:"status"`
	Config json.RawMessage `json:"config"`
}

SessionDTO is what the session-changing routes answer.

type SessionFrom

type SessionFrom int

SessionFrom says where a route reads its session name.

const (
	NoSession SessionFrom = iota
	FromPath              // the {session} path parameter
	FromBody              // the "session" field of the JSON body
	FromQuery             // the "session" query parameter
)

Where a session name can come from.

type SessionInfo

type SessionInfo struct {
	Name           string          `json:"name"`
	Status         string          `json:"status"`
	Config         json.RawMessage `json:"config"`
	Me             json.RawMessage `json:"me"`
	AssignedWorker string          `json:"assignedWorker,omitempty"`
	Presence       any             `json:"presence"`
	Timestamps     Timestamps      `json:"timestamps"`
	Engine         map[string]any  `json:"engine,omitempty"`
}

SessionInfo is WAHA's SessionInfo (a session in a list or on its own).

type Timestamps

type Timestamps struct {
	Activity *int64 `json:"activity"`
}

Timestamps is SessionInfo.timestamps.

Jump to

Keyboard shortcuts

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