prefixrouter

package
v3.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 21 Imported by: 4

README

Prefixrouter

The prefix router overrides the "serve" command and offers additional prefixrouting.

This way you can achieve that different configuration areas are loaded based on an url prefix.

Configuration

e.g. you can set the prefix in a configuration by using different configs for flamingo.router.* - for example like this:

config/config.yml:

flamingo.router.path: /en

You might then in another config area set it to a different prefix: config/de_de/config.yml:

flamingo.router.path: /de

Routing logic:

The prefixrouter runs in the following steps:

  1. primaryHandlers: Check if (optional) available "primaryHandlers" match
  2. prefixRouting: Check if the current url prefix matches one of the configured baseurls - and start routing in the matching configuration area.
  3. secondaryHandlers: Check if (optional) available "secondaryHandlers" match

Register handlers:

If you like to add your own primary or secondary handlers you can use dingo to do so:

injector.BindMulti((*prefixrouter.OptionalHandler)(nil)).AnnotatedWith("primaryHandlers").To(redirector{})

Available Handlers

This package also provides some typical primaryHandlers that may be useful for some projects - you can activate them by configuration:

RootRedirectHandler

A handler that redirects "/" to the configured target location. This is useful for the prefixrouter to redirect to a default prefix.

prefixrouter.rootRedirectHandler.enabled: true
prefixrouter.rootRedirectHandler.redirectTarget: "/en/"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FrontRouter

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

FrontRouter is a http.handler which serves multiple sites based on the host/path prefix

func NewFrontRouter

func NewFrontRouter() *FrontRouter

NewFrontRouter creates new FrontRouter

func (*FrontRouter) Add

func (fr *FrontRouter) Add(prefix string, handler routerHandler)

Add appends new Handler to Frontrouter

func (*FrontRouter) ServeHTTP

func (fr *FrontRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP gets Router for Request and lets it handle it

func (*FrontRouter) SetFallbackHandlers

func (fr *FrontRouter) SetFallbackHandlers(handlers []OptionalHandler)

SetFallbackHandlers sets list of optional fallback Handlers

func (*FrontRouter) SetFinalFallbackHandler

func (fr *FrontRouter) SetFinalFallbackHandler(handler http.Handler)

SetFinalFallbackHandler sets Fallback for undefined Handler

func (*FrontRouter) SetPrimaryHandlers

func (fr *FrontRouter) SetPrimaryHandlers(handlers []OptionalHandler)

SetPrimaryHandlers sets list of optional fallback Handlers

type Module

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

Module for core/prefix_router

func (*Module) Configure

func (m *Module) Configure(injector *dingo.Injector)

Configure DI

func (*Module) CueConfig added in v3.1.0

func (*Module) CueConfig() string

CueConfig defines the prefixrouter configuration

func (*Module) FlamingoLegacyConfigAlias added in v3.1.2

func (*Module) FlamingoLegacyConfigAlias() map[string]string

FlamingoLegacyConfigAlias legacy mapping

func (*Module) Inject

func (m *Module) Inject(
	eventRouter flamingo.EventRouter,
	logger flamingo.Logger,
	config *struct {
		EnableRootRedirectHandler bool `inject:"config:flamingo.prefixrouter.rootRedirectHandler.enabled,optional"`
		PublicEndpoint            bool `inject:"config:flamingo.opencensus.publicEndpoint,optional"`
	},
)

Inject dependencies

func (*Module) Notify

func (m *Module) Notify(ctx context.Context, event flamingo.Event)

Notify handles the app shutdown event

type OptionalHandler

type OptionalHandler interface {
	TryServeHTTP(rw http.ResponseWriter, req *http.Request) (proceed bool, err error)
}

OptionalHandler tries to handle a request

Jump to

Keyboard shortcuts

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