chatter

package
v0.0.0-...-525fc53 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2025 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MessageEvent     = "message"
	ReadMessageEvent = "read_message"
	OnlineEvent      = "online"
	OfflineEvent     = "offline"
	IsOnlineEvent    = "is_online"
	TypingEvent      = "typing"
)
View Source
const (
	AuthCookieName = "auth_token"
)

Variables

View Source
var (
	DevMode  = "dev"
	ProdMode = "prod"
)

Functions

func FormatValidationErrors

func FormatValidationErrors(err error) string

func JWTMiddleware

func JWTMiddleware(a core.AuthStore) router.Middleware

JWTMiddleware extracts the JWT token from the request and validates it and attaches the session to the request context. The session is gaurenteed to be attached to the request context if the JWT token is valid for subsequent handlers.

func SessionFromRequest

func SessionFromRequest(r *http.Request) core.Session

SessionFromRequest extracts the session from the request context. It must be called in handlers that are protected by the JWTMiddleware. It panics if the session is not found in the request context.

Types

type AddRoomMemberPayload

type AddRoomMemberPayload struct {
	Username string          `json:"username" validate:"required"`
	Role     core.MemberRole `json:"role" validate:"required"`
}

type App

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

func New

func New(ctx context.Context, config *Config, staticFS *StaticFS) *App

func (*App) AddCleanupFunc

func (app *App) AddCleanupFunc(f func(context.Context))

func (*App) IsOnlineHandler

func (app *App) IsOnlineHandler(ctx context.Context, e *core.Event) error

func (*App) MessageEventHandler

func (app *App) MessageEventHandler(ctx context.Context, e *core.Event) error

func (*App) ReadMessageHandler

func (app *App) ReadMessageHandler(ctx context.Context, e *core.Event) error

func (*App) Start

func (app *App) Start()

func (*App) TypingHandler

func (app *App) TypingHandler(ctx context.Context, e *core.Event) error

type AuthHandler

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

func NewAuthHandler

func NewAuthHandler(store core.AuthStore) *AuthHandler

func (*AuthHandler) SigninHandler

func (h *AuthHandler) SigninHandler(w http.ResponseWriter, r *http.Request) error

func (*AuthHandler) SignoutHandler

func (h *AuthHandler) SignoutHandler(w http.ResponseWriter, r *http.Request) error

type Authenticator

type Authenticator struct {
}

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(r *http.Request) (string, error)

type Base64Encoded

type Base64Encoded []byte

func (*Base64Encoded) UnmarshalText

func (b *Base64Encoded) UnmarshalText(text []byte) error

type ChatHandler

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

func NewChatHandler

func NewChatHandler(chatStore core.ChatStore) *ChatHandler

func (*ChatHandler) AddRoomMemberHandler

func (h *ChatHandler) AddRoomMemberHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) CreateRoomHandler

func (h *ChatHandler) CreateRoomHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) GetMyRoomsHandler

func (h *ChatHandler) GetMyRoomsHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) GetRoomByIDHandler

func (h *ChatHandler) GetRoomByIDHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) GetRoomMessagesHandler

func (h *ChatHandler) GetRoomMessagesHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) GetRoomUserRoomsHandler

func (h *ChatHandler) GetRoomUserRoomsHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) RemoveRoomMemberHandler

func (h *ChatHandler) RemoveRoomMemberHandler(w http.ResponseWriter, r *http.Request) error

func (*ChatHandler) SendMessageHandler

func (h *ChatHandler) SendMessageHandler(w http.ResponseWriter, r *http.Request) error

type Config

type Config struct {
	// Mode is the mode of the server. The default is dev.jk
	Mode string `validate:"required,oneof=dev prod"`
	TLS  struct {
		// Key is the path to the TLS private key file.
		// The key must in encoded in PEM format.
		Key string `validate:"required_if=Mode prod"`
		// Crt is the path to the TLS certificate file.
		// The certificate must be in encoded in PEM format.
		Crt string `validate:"required_if=Mode prod"`
	}
	// Port is the Port number to listen on.
	// If the Mode is dev, the default is 8080.
	// If the Mode is prod, the default is 443.
	Port int `validate:"required,port"`
	// Hostname is the Hostname to listen on. The default is 0.0.0.0.
	Hostname string `validate:"required"`
	Auth     struct {
		// Secret is the Secret key used to sign JWT tokens.
		// The secret must be a base64 encoded string. The default is a random 32 byte string.
		Secret Base64Encoded `validate:"required"`
	}
	SQLite struct {
		// File is the path to the SQLite database file.
		File string `validate:"required" `
		// Migrations is the path to the directory that the migration files reside.
		Migrations string `validate:"required" `
	}
	// AllowedOrigins is a list of origins that are allowed to connect to the server.
	// The default is ["*"].
	// If the Mode is prod, default to [].
	AllowedOrigins []string
	// contains filtered or unexported fields
}

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads the configuration from the config file and environment variables. Any invalid configuration will not be loaded, and the error wil be cought in the validation step.

func (*Config) Validate

func (c *Config) Validate() error

type CreateRoomPayload

type CreateRoomPayload struct {
	Name string `json:"name"`
}

type CreateRoomResponse

type CreateRoomResponse struct {
	ID string `json:"id"`
}

type IsInRoomResponse

type IsInRoomResponse struct {
	OK   bool            `json:"ok"`
	Role core.MemberRole `json:"role"`
}

type IsOnlineEventPayload

type IsOnlineEventPayload struct {
	Username string `json:"username"`
}

type MessageEventPayload

type MessageEventPayload struct {
	ID     int       `json:"id"`
	RoomID string    `json:"room_id"`
	Type   int       `json:"type"`
	Data   string    `json:"data"`
	Sender string    `json:"sender"`
	SentAt time.Time `json:"sent_at"`
}

type OfflineEventPayload

type OfflineEventPayload struct {
	Username string `json:"username"`
}

type OnlineEventPayload

type OnlineEventPayload struct {
	Username string `json:"username"`
}

type ReadMessageEventPayload

type ReadMessageEventPayload struct {
	RoomID          string    `json:"room_id"`
	ReadAt          time.Time `json:"read_at"`
	ReadBy          string    `json:"read_by"`
	LastReadMessage int       `json:"last_read_message"`
}

type SigninPayload

type SigninPayload struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type StaticFS

type StaticFS struct {
	http.FileSystem
	// contains filtered or unexported fields
}

StatisFS is a wrapper around http.FileSystem that adds etag support, cache control support and fallback file. It can be used to serve React build files.

func NewStaticFS

func NewStaticFS(fs fs.FS, fallback string, cacheControl map[string]string) (*StaticFS, error)

NewStaticFS returns a new StaticFS

func (StaticFS) EtagMiddleware

func (fs StaticFS) EtagMiddleware() func(http.Handler) http.Handler

func (StaticFS) Open

func (fs StaticFS) Open(name string) (http.File, error)

Open returns the file if found. Otherwise, it returns index.html.

type TypingEventPayload

type TypingEventPayload struct {
	Typing   bool   `json:"typing"`
	Username string `json:"username"`
	RoomID   string `json:"room_id"`
}

type UserHandler

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

func NewUserHandler

func NewUserHandler(store core.UserStore) *UserHandler

func (*UserHandler) GetUserByUsernameHandler

func (h *UserHandler) GetUserByUsernameHandler(w http.ResponseWriter, r *http.Request) error

func (*UserHandler) MeHandler

func (h *UserHandler) MeHandler(w http.ResponseWriter, r *http.Request) error

func (*UserHandler) RegisterUserHandler

func (h *UserHandler) RegisterUserHandler(w http.ResponseWriter, r *http.Request) error

Jump to

Keyboard shortcuts

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