webutil

package
v4.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package webutil provides functions for making developing a website easier.

Index

Constants

View Source
const (
	CDNMirrorMinifyJS  = "js"
	CDNMirrorMinifyCSS = "css"
)

Variables

This section is empty.

Functions

func AdminAPIListenAndServe

func AdminAPIListenAndServe(ctx context.Context, healthy ...func() error)

func AuthMiddleware

func AuthMiddleware(creds AuthConfig, teams ...string) func(http.Handler) http.Handler

AuthMiddleware is an HTTP request middleware that adds login endpoints. The request makes use of sessions, therefore the SessionMiddleware is required.

The teams argument contains a whitelist of team names, that are copied into the AuthInfo, if the user is in those teams. It is desirable to copy only the needed subset of teams into the AuthInfo, because this data is carried in the session cookie.

Endpoint "/auth/login" initiates the user login and redirects them to the GitHub OAuth page.

Endpoint "/auth/callback" gets called by the user after being redirected from GitHub after a successful login.

func AuthTemplateFunctions

func AuthTemplateFunctions(r *http.Request) template.FuncMap

AuthTemplateFunctions returns auth related template functions. These can then directly be used in the templates without having to add the auth info manually to the template data.

Function `func AuthIsAuthenticated() bool` returns true, if the user is logged in.

Function `func AuthInfo() *AuthInfo` returns the AuthInfo, if the user is logged and `nil` otherwise.

Example:

{{ if AuthIsAuthenticated }}
  <span class="navbar-text">Hello, <em>{{ AuthInfo.Name }}</em>!</span>
{{ else }}
  <a class="nav-link" href="/auth/login">Login</span></a>
{{ end }}

func ListenAndServeWithContext

func ListenAndServeWithContext(ctx context.Context, addr string, handler http.Handler) error

ListenAndServeWithContext does the same as http.ListenAndServe with the difference that is properly utilises the context. This means it does a graceful shutdown when the context is done and a context cancellation gets propagated down to the actual request context.

func SessionFromContext

func SessionFromContext(ctx context.Context) (*sessions.Session, error)

SessionFromContext extracts the Session store from the given context. The session store is injected into the request via the SessionMiddleware. Therefore it is required to use this middleware to be able to get the store.

func SessionFromRequest

func SessionFromRequest(r *http.Request) (*sessions.Session, error)

SessionFromRequest returns the results of SessionFromContext for the context of the given request.

func SessionMiddleware

func SessionMiddleware(secret SessionSecret, opts ...SessionMiddlewareOption) func(http.Handler) http.Handler

SessionMiddleware inizializes the session store and injects it into the context of the requests.

func SessionSecretSourceRedis

func SessionSecretSourceRedis(ctx context.Context, client RedisSessioner, prefix string) ([]byte, error)

SessionSecretSourceRedis stores the session secrets in Redis. If the key does not exist yet, it will create a new one.

func SessionSecretSourceVolatile

func SessionSecretSourceVolatile() []byte

SessionSecretSourceVolatile creates a session secret that is stored in memory only. This implies, that all session data is lost after an application restart and an application cannot have more than one replicas.

Types

type AuthConfig

type AuthConfig struct {
	ClientID     string
	ClientSecret string
}

type AuthInfo

type AuthInfo struct {
	GitHubToken string
	UpdatedAt   time.Time

	Login string
	Name  string
	Teams []string
}

AuthInfo contains data about the currently logged in user.

func AuthInfoFromContext

func AuthInfoFromContext(ctx context.Context) (*AuthInfo, error)

AuthInfoFromContext extracts the AuthInfo from the given context. The AuthInfo is injected into the request via the AuthMiddleware. Therefore it is required to use this middleware to be able to get the AuthInfo.

func AuthInfoFromRequest

func AuthInfoFromRequest(r *http.Request) (*AuthInfo, error)

AuthInfoFromRequest extracts the AuthInfo from the context within the given request. The AuthInfo is injected into the request via the AuthMiddleware. Therefore it is required to use this middleware to be able to get the AuthInfo.

func (AuthInfo) InTeam

func (i AuthInfo) InTeam(want string) bool

InTeam returns true, if the user is in the given team. The team name needs to be whitelisted in the AuthMiddleware, otherwise it will return false even if the user is in the team.

type CDNMirrorMinifier added in v4.1.0

type CDNMirrorMinifier string

type CDNMirrorSource added in v4.1.0

type CDNMirrorSource struct {
	URL    string
	Target string
	Minify CDNMirrorMinifier
}

func CDNMirrorSourceBootstrap added in v4.1.0

func CDNMirrorSourceBootstrap() CDNMirrorSource

func CDNMirrorSourceBulma added in v4.1.0

func CDNMirrorSourceBulma() CDNMirrorSource

func CDNMirrorSourceFontAwesomeSprites added in v4.1.0

func CDNMirrorSourceFontAwesomeSprites() CDNMirrorSource

func CDNMirrorSourceHotwiredTurbo added in v4.1.0

func CDNMirrorSourceHotwiredTurbo() CDNMirrorSource

type RedisSessioner

type RedisSessioner interface {
	Get(context.Context, string) *redis.StringCmd
	Set(context.Context, string, interface{}, time.Duration) *redis.StatusCmd
}

type Response

type Response = http.HandlerFunc

type ResponseHandlerFunc

type ResponseHandlerFunc func(*View, *http.Request) Response

type SessionMiddlewareOption

type SessionMiddlewareOption func(c *sessionMiddlewareConfig)

func SessionMiddlewareCookieDomain

func SessionMiddlewareCookieDomain(domain string) SessionMiddlewareOption

func SessionMiddlewareCookieUnsecure

func SessionMiddlewareCookieUnsecure() SessionMiddlewareOption

type SessionSecret

type SessionSecret []byte

type TemplateFuncMap

type TemplateFuncMap func(*http.Request) template.FuncMap

func SimpleTemplateFuncMap

func SimpleTemplateFuncMap(name string, fn interface{}) TemplateFuncMap

func SimpleTemplateFuncMaps

func SimpleTemplateFuncMaps(fm template.FuncMap) TemplateFuncMap

type View

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

func (*View) Error

func (v *View) Error(status int, err error) http.HandlerFunc

func (*View) Errorf

func (v *View) Errorf(status int, text string, a ...interface{}) http.HandlerFunc

func (*View) HTML

func (v *View) HTML(status int, filename string, data any) http.HandlerFunc

func (*View) JSON

func (v *View) JSON(status int, data any) http.HandlerFunc

func (*View) Redirect

func (v *View) Redirect(status int, location string, args ...interface{}) http.HandlerFunc

type ViewHandler

type ViewHandler struct {
	FS       fs.FS
	FuncMaps []TemplateFuncMap
}

func NewViewHandler

func NewViewHandler(fs fs.FS, fms ...TemplateFuncMap) *ViewHandler

func (*ViewHandler) Render

func (h *ViewHandler) Render(filename string, r *http.Request, d interface{}) (*bytes.Buffer, error)

func (*ViewHandler) Wrap

Jump to

Keyboard shortcuts

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