webapp

package
v0.0.0-...-3e11f44 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Overview

Package webapp provides common functions and types for web applications.

Index

Constants

View Source
const BuildDateTimeFormat = "2006-01-02 15:04:05"

BuildDateTimeFormat defines the display format for build dates.

View Source
const HeadersPageName = "headers.html"

HeadersPageName is the name of the HTTP template to execute.

View Source
const RootPageName = "root.html"

RootPageName specifies the template file for the root page.

Variables

View Source
var (
	ErrConfigRead  = errors.New("failed to read config file")
	ErrConfigParse = errors.New("failed to parse config file")
)

Predefined errors for common configuration issues.

Functions

func ExecutableModTime

func ExecutableModTime() (time.Time, error)

ExecutableModTime returns the modification time of the current executable.

Types

type AppConfig

type AppConfig struct {
	Name        string `required:"true"` // Name of the web application.
	AssetsDir   string // Directory for static web assets.
	TmplPattern string // Glob pattern for template files.
}

AppConfig holds settings related to the web application itself.

type Config

type Config struct {
	App    AppConfig        // Web application-specific configuration.
	Server webserver.Config // HTTP server configuration.
	Log    weblog.Config    // Logging configuration.
}

Config consolidates configs, including app, server, and log settings.

func LoadConfigFromJSON

func LoadConfigFromJSON(filepath string) (*Config, error)

LoadConfigFromJSON loads app config from a specified JSON file path. It returns a populated Config or error if reading or parsing file fails.

func (*Config) MissingFields

func (c *Config) MissingFields() ([]string, error)

MissingFields identifies which required fields are absent in Config. It returns a slice of missing fields. If an error occurs during the check, an empty slice and the error are returned.

type HeaderPair

type HeaderPair struct {
	Key    string
	Values []string
}

HeaderPair represents a key-value pair in an HTTP header.

func SortHeaders

func SortHeaders(httpHeader http.Header) []HeaderPair

SortHeaders uses httpHeader to create a sorted list of HeaderPair structs. The headers are sorted alphabetically by key. If httpHeader is empty, it returns nil.

type HeadersPageData

type HeadersPageData struct {
	Title   string       // Title of the page.
	Headers []HeaderPair // Sorted list of the request headers.
}

HeadersPageData holds the data passed to the HTML template.

type Option

type Option func(*WebApp)

Option defines a function type for configuring a WebApp instance, adhering to the functional options pattern.

func WithName

func WithName(name string) Option

WithName creates an Option to set the name of the WebApp.

func WithTemplate

func WithTemplate(tmpl *template.Template) Option

WithTemplate creates an Option to set the template of the WebApp.

type RootPageData

type RootPageData struct {
	Title string // Title of the page.
}

RootPageData encapsulates data to be passed to the root page template.

type WebApp

type WebApp struct {
	Config                           // Provides embedded AppConfig.
	Tmpl          *template.Template // Tmpl holds parsed templates.
	BuildDateTime time.Time          // Time executable last modified.
}

WebApp encapsulates common web application configurations and state, including configuration settings, templates, and build information.

func New

func New(opts ...Option) (*WebApp, error)

New creates a new WebApp instance with the provided options, initializing its BuildDateTime to the executable's modification time.

It returns an error if the name is not specified or if it encounters issues determining the build time.

func (*WebApp) BuildHandlerGet

func (app *WebApp) BuildHandlerGet(w http.ResponseWriter, r *http.Request)

BuildHandlerGet responds with the application's build date and time.

func (*WebApp) HeadersHandlerGet

func (app *WebApp) HeadersHandlerGet(w http.ResponseWriter, r *http.Request)

HeadersHandlerGet shows the headers of the request in sorted order.

func (*WebApp) HelloHTMLHandlerGet

func (app *WebApp) HelloHTMLHandlerGet(w http.ResponseWriter, r *http.Request)

HelloHTMLHandlerGet responds with a hello message in HTML format.

func (*WebApp) HelloTextHandlerGet

func (app *WebApp) HelloTextHandlerGet(w http.ResponseWriter, r *http.Request)

HelloTextHandlerGet responds with a hello message in plain text format.

func (*WebApp) RootHandlerGet

func (app *WebApp) RootHandlerGet(w http.ResponseWriter, r *http.Request)

RootHandlerGet handles GET requests to the root ("/") route.

func (*WebApp) String

func (app *WebApp) String() string

String returns a string representation of WebApp.

Jump to

Keyboard shortcuts

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