web

package
v0.0.0-...-7665913 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: BSD-3-Clause Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CacheControlHeader = "Cache-Control"
	ContentTypeHeader  = "Content-Type"
	ETagHeader         = "ETag"
	LocationHeader     = "Location"

	ContentTypeAtom        = "application/atom+xml"
	ContentTypeHTML        = "text/html"
	ContentTypeJSON        = "application/json"
	ContentTypeText        = "text/plain; charset=UTF-8"
	ContentTypeOctetStream = "application/octet-stream"
	ContentTypeJavascript  = "text/javascript"
)
View Source
const CacheDurationImmutable = -1
View Source
const (
	LoginPath = "/login.html"
)

Variables

View Source
var (
	ErrUnauthenticated = errors.New("no authentication found")
)

Functions

func BaseMiddleware

func BaseMiddleware(next http.Handler) http.Handler

func CachingMiddleware

func CachingMiddleware(maxAge int) func(next http.Handler) http.Handler

CachingMiddleware returns a handler that sets the Cache-Control header with the specified max-age. If the given value is negative, max-age will be set to one year and 'immutable' will be added to the Cache-Control value.

func ContentTypeMiddleware

func ContentTypeMiddleware(contentType string) func(next http.Handler) http.Handler

func SetAuth

func SetAuth(ctx context.Context, auth Auth) context.Context

func StaticContentMiddleware

func StaticContentMiddleware(next http.Handler) http.Handler

Types

type Auth

type Auth interface {
	UserID() UserID
}

func GetAuth

func GetAuth(ctx context.Context) (Auth, error)

type AuthRepository

type AuthRepository interface {
	StoreToken(Token) error
	FindToken(TokenID) (Token, error)
	ResolveUserID(mail.Address) (UserID, error)
	StoreSession(Session) error
	FindSession(SessionID) (Session, error)
}

type Authenticator

type Authenticator struct {
	TokenCallback  func(mail.Address, TokenID) error
	RedirectTarget string
	// contains filtered or unexported fields
}

func NewAuthenticator

func NewAuthenticator(domain string,
	redirectTarget string,
	repo AuthRepository,
	templateFS,
	assetsFS fs.FS) *Authenticator

func (*Authenticator) Controller

func (a *Authenticator) Controller() *Controller

func (*Authenticator) GenerateToken

func (a *Authenticator) GenerateToken(length int) (TokenID, error)

func (*Authenticator) Middleware

func (a *Authenticator) Middleware(next http.Handler) http.Handler

type BBoltAuthRepository

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

func NewBBoltAuthRepository

func NewBBoltAuthRepository(db *bbolt.DB) *BBoltAuthRepository

TODO: Rather return the errors here?

func (*BBoltAuthRepository) FindSession

func (r *BBoltAuthRepository) FindSession(id SessionID) (Session, error)

func (*BBoltAuthRepository) FindToken

func (r *BBoltAuthRepository) FindToken(id TokenID) (Token, error)

func (*BBoltAuthRepository) ResolveUserID

func (r *BBoltAuthRepository) ResolveUserID(email mail.Address) (UserID, error)

func (*BBoltAuthRepository) StoreSession

func (r *BBoltAuthRepository) StoreSession(s Session) error

func (*BBoltAuthRepository) StoreToken

func (r *BBoltAuthRepository) StoreToken(t Token) error

type BoltStatisticsRepository

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

func NewBoltStatisticsRepository

func NewBoltStatisticsRepository(db *bbolt.DB) (*BoltStatisticsRepository, error)

func (*BoltStatisticsRepository) AddView

func (r *BoltStatisticsRepository) AddView(page string) error

func (*BoltStatisticsRepository) GetViews

func (r *BoltStatisticsRepository) GetViews() (map[string]uint, error)

type CloseHandler

type CloseHandler func()

type Controller

type Controller struct {
	BasePath     string
	Handlers     map[Endpoint]Handler
	Middleware   []Middleware
	ErrorHandler ErrorHandler
}

func NewAssetController

func NewAssetController(basePath string, fileSystem fs.FS) *Controller

func NewCacheBustingAssetController

func NewCacheBustingAssetController(basePath string, fileSystem fs.FS) *Controller

func (*Controller) AddMiddleware

func (c *Controller) AddMiddleware(m Middleware) *Controller

func (*Controller) Register

func (c *Controller) Register(router chi.Router)

type Controller2

type Controller2 struct {
	ErrorHandler func(handler Handler) http.Handler
	// contains filtered or unexported fields
}

func NewController

func NewController() *Controller2

func (*Controller2) AddMiddleware

func (c *Controller2) AddMiddleware(m Middleware)

func (*Controller2) GET

func (c *Controller2) GET(path string, handler Handler)

func (*Controller2) Handler

func (c *Controller2) Handler() http.Handler

func (*Controller2) POST

func (c *Controller2) POST(path string, handler Handler)

type Endpoint

type Endpoint struct {
	Method string
	Path   string
}

type ErrorHandler

type ErrorHandler func(http.ResponseWriter, *http.Request, error) Handler

type ErrorHandler2

type ErrorHandler2 func(err error, w http.ResponseWriter, r *http.Request) (string, bool)

type ErrorHandlerChain

type ErrorHandlerChain []ErrorHandler2

func (*ErrorHandlerChain) AddErrorHandler

func (chain *ErrorHandlerChain) AddErrorHandler(handler ErrorHandler2)

func (ErrorHandlerChain) BuildErrorHandler

func (chain ErrorHandlerChain) BuildErrorHandler(errorTemplate *Template) func(Handler) http.Handler

type Handler

type Handler func(http.ResponseWriter, *http.Request) error

func RenderTemplate

func RenderTemplate(template *Template, data any) Handler

type InMemoryAuthRepository

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

func NewInMemoryAuthRepository

func NewInMemoryAuthRepository() *InMemoryAuthRepository

func (*InMemoryAuthRepository) FindSession

func (r *InMemoryAuthRepository) FindSession(id SessionID) (Session, error)

func (*InMemoryAuthRepository) FindToken

func (r *InMemoryAuthRepository) FindToken(id TokenID) (Token, error)

func (*InMemoryAuthRepository) ResolveUserID

func (r *InMemoryAuthRepository) ResolveUserID(email mail.Address) (UserID, error)

func (*InMemoryAuthRepository) StoreSession

func (r *InMemoryAuthRepository) StoreSession(s Session) error

func (*InMemoryAuthRepository) StoreToken

func (r *InMemoryAuthRepository) StoreToken(t Token) error

type Mailer

type Mailer interface {
	Send(sender, recipient mail.Address, template *Template, data any) error
}

func NewSMTPMailer

func NewSMTPMailer(host, smtpHost string, smtpPort int, user, password string) Mailer

func NewStubMailer

func NewStubMailer() Mailer

type MessageHandler

type MessageHandler func(client *WSClient, msg string) error

type Middleware

type Middleware func(http.Handler) http.Handler

type SMTPMailer

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

func (*SMTPMailer) Send

func (m *SMTPMailer) Send(sender, recipient mail.Address, template *Template, data any) error

type Session

type Session struct {
	ID    SessionID
	User  UserID
	Email mail.Address
}

TODO: Shouldn't the session expire at one point?

type SessionID

type SessionID string

type SitemapController

type SitemapController struct {
	Controller
	// contains filtered or unexported fields
}

func NewSitemapController

func NewSitemapController() *SitemapController

func (*SitemapController) AddSite

func (sc *SitemapController) AddSite(url url.URL)

type StatisticsModule

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

func NewStatisticsModule

func NewStatisticsModule(repo StatisticsRepository) *StatisticsModule

func (*StatisticsModule) Controller

func (m *StatisticsModule) Controller() *Controller

func (*StatisticsModule) Middleware

func (m *StatisticsModule) Middleware(next http.Handler) http.Handler

type StatisticsRepository

type StatisticsRepository interface {
	AddView(page string) error
	GetViews() (map[string]uint, error)
}

type StubMailer

type StubMailer struct{}

func (*StubMailer) Send

func (s *StubMailer) Send(sender, recipient mail.Address, template *Template, data any) error

type Template

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

func (*Template) Execute

func (t *Template) Execute(w io.Writer, data any) error

func (*Template) ExecuteFragment

func (t *Template) ExecuteFragment(w io.Writer, name string, data any) error

type TemplateData

type TemplateData struct {
	Msg  string
	Data any
}

type TemplateModule

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

func NewTemplateModule

func NewTemplateModule(templateFS, assetsFS fs.FS, data any) *TemplateModule

func (*TemplateModule) Controller

func (m *TemplateModule) Controller() *Controller

type Templater

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

func NewTemplater

func NewTemplater(templateFS, assetsFS fs.FS) *Templater

func (*Templater) Get

func (t *Templater) Get(patterns ...string) (*Template, error)

TODO: Don't include base.html per default.

func (*Templater) GetP

func (t *Templater) GetP(patterns ...string) *Template

func (*Templater) Write

func (t *Templater) Write(writer io.Writer, msg string, data any, patterns ...string) error

type Token

type Token struct {
	ID    TokenID
	Email mail.Address
	// Unix timestamp
	ValidUntil int64
}

func (*Token) Expired

func (t *Token) Expired() bool

type TokenID

type TokenID string

type UserAuth

type UserAuth struct {
	User  UserID
	Email mail.Address
}

func (*UserAuth) UserID

func (a *UserAuth) UserID() UserID

type UserID

type UserID struct{ uuid.UUID }

type WSClient

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

func NewWSClient

func NewWSClient(conn *websocket.Conn, onMessage MessageHandler, onClose CloseHandler) *WSClient

func (*WSClient) Close

func (c *WSClient) Close() error

func (*WSClient) WriteMessage

func (c *WSClient) WriteMessage(msg string)

Jump to

Keyboard shortcuts

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