pages

package
v2.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package pages handles parsing the HTML templates used to build the GUI and returning these templates when requested.

Most pages in the app are just a basic shell, with data being injected by API calls. This was done so that pages are more reactive to filters and user activity. The API calls are done via js/ts and Vue objects.

Most pages are returned via the App() or AppMapped() funcs. If a page needs to use, validate, or handle a specific URL query parameter, inject some custom data, etc. then a separate func in this package is defined. Since most pages are just HTML shells with Vue/API calls populating pages, most pages use App() and AppMapped().

***

The below App and AppMapped funcs were defined to clean up having to define a separate handler func for each basic page endpoint. For the pages that use App or AppMapped, the handler funcs were near identical except the HTML template to show or the HTTP router endpoint used. This resulted in a LOT of duplicate code defining handler funcs all just to serve an HTML template. These funcs clean this up a lot.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func App

func App(w http.ResponseWriter, r *http.Request)

App serves a template based upon the last piece of the path exactly matching an HTML template's filename. This is used for serving basic pages where no computations or functionality needs to be performed prior to showing the page.

You would not use this when you need to provide more data to an HTML template, need to perform some kind of computation or validation prior to showing a template, or if you are serving the filename on a different URL path.

func AppMapped

func AppMapped(w http.ResponseWriter, r *http.Request)

AppMapped serves a template from the "app" subdirectory of templates by matching up the full request URL path to an HTML template's filename. This is used for serving basic pages where no computations or functionality needs to be performed prior to showing the page, but where the last element in the path does not match the template's filename exactly (due to more complex path, url vs filename differences, etc.)

For example: request is sent to "/licenses/reports/some-page/" but the HTML template is named "some-license-report-page". The path last element and name does not match because each fits and organization method based upon its use (HTTP router vs filesystem).

func Diagnostics

func Diagnostics(w http.ResponseWriter, r *http.Request)

Diagnostics shows the diagnostic page. We use an actual page for this instead of just using w.Write() so that we can display diagnostic info from js and css.

func Help

func Help(w http.ResponseWriter, r *http.Request)

Help shows a help documentation page. This serves any help page by using the end of the url as the document name. This is useful since we don't need to create a func or route for each help page we create.

func HelpTableOfContents

func HelpTableOfContents(w http.ResponseWriter, r *http.Request)

HelpTableOfContents shows the page listing help documents

func License

func License(w http.ResponseWriter, r *http.Request)

License shows the data for a specific license.

func Login

func Login(w http.ResponseWriter, r *http.Request)

Login shows the login page to the app. This also checks if the user is already logged in and redirects the user to the main logged in page if so.

func PrintFSFileList added in v2.1.0

func PrintFSFileList(e fs.FS)

PrintFSFileList prints out the list of files in an fs.FS.

This is used ONLY for diagnostic/debug/development purposes and it typically paired with embedded files (go:embed).

func Show added in v2.1.0

func Show(w http.ResponseWriter, subdir, filename string, injectedData any)

Show renders a template as HTML and writes it to w. This works by taking a subdirectory's name and the name of a template file, looks up the template that was parsed earlier in Build(), and returns it with any injected data available at {{.Data}} in HTML templates.

This does not work with {{defined}}{{end}} named templates. Templates must be files. However, a named template can include {{defined}}{{end}} and {{template}}{{end}} blocks.

InjectedData is any custom data you want to return to use in the template, but is typically PageData{}. We allow for any type of data to be used though since it is more flexible.

To serve files stored at the root of the templates fs.FS, use "" as the subdir.

func ShowError

func ShowError(w http.ResponseWriter, r *http.Request, ep ErrorPage)

ShowError shows the error page with injected data. This this func cleans up the code whereever an error page is called/shown since it is common throughout the app. The first part of this func is similar to getPageConfigData but allows for skipping the user data since we do not use it on the error page.

func UserProfile added in v2.1.0

func UserProfile(w http.ResponseWriter, r *http.Request)

UserProfile shows the page where a user can view and manage their own user account or profile. For now, user's can only change their passwords since we do not want them to be able to change permissions (for obvious reasons) or alerts (so they get alerts for what admin's deem important).

This does not use App() since we need to get the minimum password length to build the GUI with.

func Users

func Users(w http.ResponseWriter, r *http.Request)

Users shows the page to manage users.

Types

type Config added in v2.1.0

type Config struct {
	//Development is passed to each template when rendering the HTML to be sent to
	//the user so that the HTML can be altered based on if you are running your app
	//in a development mode/enviroment. Typically this is used to show a banner on
	//the page, loads extra diagnostic libraries or tools, and uses non-cache busted
	//static files.
	Development bool

	//UseLocalFiles is passed to each template when rendering the HTML to be sent to
	//the user so that the HTML can be altered to use locally hosted third party
	//libraries (JS, CSS) versus libraries retrieve from the internet.
	UseLocalFiles bool

	//Extension is the extension you use for your template files. The default is
	//"html".
	Extension string

	//TemplateFiles is the fs.FS filesystem that holds the HTML templates to be read
	//and used.
	TemplateFiles fs.FS

	//StaticFiles is the list of static files used to build the GUI (js, css, images).
	//This is a separate FS that is used for cache busting purposes. See the static()
	//func for more info.
	StaticFiles *hashfs.HFS

	//Debug prints out debugging information if true.
	Debug bool
	// contains filtered or unexported fields
}

Config is the set of configuration settings for working with HTML templates. This also stores the parsed HTML templates after Build() is called and that can be shown with Show().

func (*Config) Build added in v2.1.0

func (c *Config) Build() (err error)

Build parses each of the HTML files into a golang template, saves the parsed templates for future use, and saves the config. Build() must be called before Show() is called.

Templates are build for the root directory in the given FS, as well as each sub- directory of the root (only one tier of subdirectories is built). Files from the root directory are inherited into each subdirectory, to allow for common files/partials (header, footer, html head, etc.) to be reused between subdirectories.

type ErrorPage

type ErrorPage struct {
	PageTitle string //card header

	//These may simply be concatted in the gui but makes code cleaner and more easily reused.
	Topic    string //general topic of the error
	Message  string //what error occured
	Solution string //how to fix or work around the error

	ShowLinkBtn bool   //whether or not a footer button with link is shown
	Link        string //url to use for link, if blank will default to /app/ (main page).
	LinkText    string //text to show on button, if blank will default to Main Menu.
}

ErrorPage is the data used to show messages when an error or unexpected event occurs this stuff is injected into a template to describe to user what occured and how to fix it.

type PageData

type PageData struct {
	UserData    any //username, permissions, etc.
	AppSettings any //whether certain features are used or enabled.
	Data        any //misc. actual data we want to show in the gui.
}

PageData is the format of our data we inject into a template to render it or show data. This struct is used so we always have a consistent format of data being injected into templates for easy parsing.

This is provided to the injectedData field of Show().

Any is used, instead of defined database types, so that we don't have import loops for ease of use.

Jump to

Keyboard shortcuts

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