web

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SessionDuration is the duration for which a session remains valid
	SessionDuration = 24 * time.Hour
	// MinPasswordLength is the minimum required length for passwords
	MinPasswordLength = 8
	// XMLHttpRequestHeader is the standard header value for AJAX requests
	XMLHttpRequestHeader = "XMLHttpRequest"
)
View Source
const (
	// StatusFound is the HTTP status code for redirects
	StatusFound = http.StatusFound // 302
)

Variables

View Source
var Module = fx.Options(

	fx.Provide(
		fx.Annotate(
			func(
				logger logging.Logger,
				cfg *config.Config,
				sessionManager *session.Manager,
				middlewareManager *middleware.Manager,
				renderer view.Renderer,
				userService user.Service,
				formService form.Service,
			) *HandlerDeps {
				return &HandlerDeps{
					Logger:            logger,
					Config:            cfg,
					SessionManager:    sessionManager,
					MiddlewareManager: middlewareManager,
					Renderer:          renderer,
					UserService:       userService,
					FormService:       formService,
				}
			},
		),
	),

	fx.Provide(

		fx.Annotate(
			func(deps *HandlerDeps) (Handler, error) {
				return NewAuthHandler(*deps)
			},
			fx.ResultTags(`group:"handlers"`),
		),

		fx.Annotate(
			func(deps *HandlerDeps) (Handler, error) {
				return NewWebHandler(*deps)
			},
			fx.ResultTags(`group:"handlers"`),
		),

		fx.Annotate(
			func(deps *HandlerDeps, accessManager *access.AccessManager) (Handler, error) {
				return NewFormHandler(*deps, deps.FormService, accessManager), nil
			},
			fx.ResultTags(`group:"handlers"`),
		),

		fx.Annotate(
			func(deps *HandlerDeps, accessManager *access.AccessManager) (Handler, error) {
				return NewDashboardHandler(*deps, accessManager), nil
			},
			fx.ResultTags(`group:"handlers"`),
		),
	),

	fx.Invoke(fx.Annotate(
		func(lc fx.Lifecycle, handlers []Handler, logger logging.Logger) {
			lc.Append(fx.Hook{
				OnStart: func(ctx context.Context) error {
					for _, h := range handlers {
						if err := h.Start(ctx); err != nil {
							logger.Error("failed to start handler", "error", err)
							return err
						}
					}
					return nil
				},
				OnStop: func(ctx context.Context) error {
					for _, h := range handlers {
						if err := h.Stop(ctx); err != nil {
							logger.Error("failed to stop handler", "error", err)
							return err
						}
					}
					return nil
				},
			})
		},
		fx.ParamTags(``, `group:"handlers"`),
	)),
)

Module provides web handler dependencies

Functions

func RegisterHandlers

func RegisterHandlers(
	e *echo.Echo,
	handlers []Handler,
	accessManager *access.AccessManager,
	logger logging.Logger,
)

RegisterHandlers registers all handlers with the Echo instance

Types

type AuthHandler

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

AuthHandler handles authentication-related requests

func NewAuthHandler

func NewAuthHandler(deps HandlerDeps) (*AuthHandler, error)

NewAuthHandler creates a new auth handler

func (*AuthHandler) Login

func (h *AuthHandler) Login(c echo.Context) error

Login handles GET /login - displays the login form

func (*AuthHandler) LoginPost

func (h *AuthHandler) LoginPost(c echo.Context) error

*

  • LoginPost handles POST /login - processes the login form *
  • This handler:
  • 1. Validates user credentials
  • 2. Creates a new session on success
  • 3. Sets session cookie
  • 4. Returns appropriate response based on request type:
  • - JSON response for API requests
  • - HTML response with error for regular requests
  • - Redirect to dashboard on success

func (*AuthHandler) LoginValidation

func (h *AuthHandler) LoginValidation(c echo.Context) error

LoginValidation handles the login form validation schema request

func (*AuthHandler) Logout

func (h *AuthHandler) Logout(c echo.Context) error

Logout handles POST /logout - processes the logout request

func (*AuthHandler) Register

func (h *AuthHandler) Register(e *echo.Echo)

Register registers the auth handler routes

func (*AuthHandler) Signup

func (h *AuthHandler) Signup(c echo.Context) error

Signup handles GET /signup - displays the signup form

func (*AuthHandler) SignupPost

func (h *AuthHandler) SignupPost(c echo.Context) error

SignupPost handles POST /signup - processes the signup form

func (*AuthHandler) SignupValidation

func (h *AuthHandler) SignupValidation(c echo.Context) error

SignupValidation returns the validation schema for the signup form

func (*AuthHandler) Start

func (h *AuthHandler) Start(ctx context.Context) error

Start initializes the auth handler. This is called during application startup.

func (*AuthHandler) Stop

func (h *AuthHandler) Stop(ctx context.Context) error

Stop cleans up any resources used by the auth handler. This is called during application shutdown.

type DashboardHandler

type DashboardHandler struct {
	HandlerDeps
	AccessManager *access.AccessManager
}

func NewDashboardHandler

func NewDashboardHandler(deps HandlerDeps, accessManager *access.AccessManager) *DashboardHandler

func (*DashboardHandler) Register added in v0.2.0

func (h *DashboardHandler) Register(e *echo.Echo)

func (*DashboardHandler) Start

func (h *DashboardHandler) Start(ctx context.Context) error

Start initializes the dashboard handler. This is called during application startup.

func (*DashboardHandler) Stop

func (h *DashboardHandler) Stop(ctx context.Context) error

Stop cleans up any resources used by the dashboard handler. This is called during application shutdown.

type FormHandler added in v0.2.0

type FormHandler struct {
	HandlerDeps
	FormService   formdomain.Service
	AccessManager *access.AccessManager
}

func NewFormHandler added in v0.2.0

func NewFormHandler(
	deps HandlerDeps,
	formService formdomain.Service,
	accessManager *access.AccessManager,
) *FormHandler

NewFormHandler creates a new form handler

func (*FormHandler) Register added in v0.2.0

func (h *FormHandler) Register(e *echo.Echo)

func (*FormHandler) Start added in v0.2.0

func (h *FormHandler) Start(ctx context.Context) error

Start initializes the form handler. This is called during application startup.

func (*FormHandler) Stop added in v0.2.0

func (h *FormHandler) Stop(ctx context.Context) error

Stop cleans up any resources used by the form handler. This is called during application shutdown.

type Handler

type Handler interface {
	// Register registers the handler's routes with the Echo instance.
	// This method should be called by the RegisterHandlers function in module.go.
	// The handler should not apply middleware directly - this is handled by RegisterHandlers.
	// Routes should be grouped logically and follow RESTful patterns where appropriate.
	Register(e *echo.Echo)

	// Start initializes the handler and any required resources.
	// This is called during application startup.
	Start(ctx context.Context) error

	// Stop cleans up any resources used by the handler.
	// This is called during application shutdown.
	Stop(ctx context.Context) error
}

Handler defines the interface for web handlers. Each handler is responsible for a specific set of related routes. Handlers should: 1. Be focused on a single domain area (e.g., forms, auth, dashboard) 2. Use the HandlerDeps struct for common dependencies 3. Implement proper error handling and logging 4. Follow consistent route naming patterns 5. Use the context helpers for user data access

type HandlerDeps

type HandlerDeps struct {
	// Logger provides logging capabilities for structured logging
	Logger logging.Logger
	// Config provides application configuration and settings
	Config *config.Config
	// SessionManager handles user sessions and authentication state
	SessionManager *session.Manager
	// MiddlewareManager manages middleware configuration and setup
	MiddlewareManager *middleware.Manager
	// Renderer handles view rendering and template management
	Renderer view.Renderer
	// UserService provides user-related operations and business logic
	UserService user.Service
	// FormService provides form-related operations and business logic
	FormService form.Service
}

HandlerDeps contains dependencies for web handlers. This struct is embedded in each handler to provide access to these dependencies. All handlers should use these dependencies through this struct rather than accessing them directly or creating their own instances.

func NewHandlerDeps

func NewHandlerDeps(params HandlerParams) (*HandlerDeps, error)

NewHandlerDeps creates a new HandlerDeps instance. This factory function ensures that all dependencies are properly initialized and validated before being used by a handler.

func (*HandlerDeps) Start

func (d *HandlerDeps) Start(ctx context.Context) error

Start initializes the handler dependencies. This is called during application startup.

func (*HandlerDeps) Stop

func (d *HandlerDeps) Stop(ctx context.Context) error

Stop cleans up any resources used by the handler dependencies. This is called during application shutdown.

func (*HandlerDeps) Validate

func (d *HandlerDeps) Validate() error

Validate checks if all required dependencies are present. This should be called when creating a new handler to ensure all required dependencies are properly initialized.

type HandlerParams

type HandlerParams struct {
	UserService       user.Service
	FormService       form.Service
	Logger            logging.Logger
	Config            *config.Config
	SessionManager    *session.Manager
	MiddlewareManager *middleware.Manager
	Renderer          view.Renderer
}

HandlerParams contains parameters for creating a handler. This struct is used to pass dependencies to NewHandlerDeps in a type-safe and explicit way.

type WebHandler added in v0.2.0

type WebHandler struct {
	HandlerDeps
}

WebHandler handles web page requests

func NewWebHandler added in v0.2.0

func NewWebHandler(deps HandlerDeps) (*WebHandler, error)

NewWebHandler creates a new web handler using HandlerDeps

func (*WebHandler) Register added in v0.2.0

func (h *WebHandler) Register(e *echo.Echo)

Register registers the web routes

func (*WebHandler) Start added in v0.2.0

func (h *WebHandler) Start(ctx context.Context) error

Start initializes the web handler. This is called during application startup.

func (*WebHandler) Stop added in v0.2.0

func (h *WebHandler) Stop(ctx context.Context) error

Stop cleans up any resources used by the web handler. This is called during application shutdown.

Jump to

Keyboard shortcuts

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