Documentation ¶
Overview ¶
Package webapp provides common functions and types for web applications.
Index ¶
- Constants
- Variables
- func ExecutableModTime() (time.Time, error)
- type AppConfig
- type Config
- type HeaderPair
- type HeadersPageData
- type Option
- type RootPageData
- type WebApp
- func (app *WebApp) BuildHandlerGet(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) HeadersHandlerGet(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) HelloHTMLHandlerGet(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) HelloTextHandlerGet(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) RootHandlerGet(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) String() string
Constants ¶
const BuildDateTimeFormat = "2006-01-02 15:04:05"
BuildDateTimeFormat defines the display format for build dates.
const HeadersPageName = "headers.html"
HeadersPageName is the name of the HTTP template to execute.
const RootPageName = "root.html"
RootPageName specifies the template file for the root page.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 WithTemplate ¶
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 ¶
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.