gosp

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 3 Imported by: 2

README

gosp

Go Static Pages is a small library which wraps around net/http to help build static webpages. Using this library helps remove some necessary boilerplate that gets in the way of being productive.

goal

I don't think it's a big secret that many websites could probably be reduced down to a simple server-side rendered service and actually gain big points in simplicity, maintainability, and speed. Which would mean that small projects like this one could possibly help reduce on monstrously overcomplicated frontends move towards a simpler solution.

use

There are two main "Handlers" that exist in this module which satisfy http.Handler, these are PageHandler and FormHandler.

Examples of these handlers are in example/, but here is a truncated version of the page handler example:

// Your main function or something.

pageHandler := gosp.NewPageHandler[testStruct](
    // The actual handler for this page. Normally you would make a query or two here.
    func(_ *http.Request) (*testStruct, error) {
        return &testStruct{"foobar"}, nil
    },
    // Using a file with go:embed is quite useful here.
    template.Must(template.New("test").Parse("<div>{{ .Content }}</div>")),
)
	
// Using "pageHandler" with net/http.

Using FormHandler is slightly more simple because only one handler is required, and you don't have to think about templates.

some free "art"

     ()------()     ____________________
    | (.)  (.) |   |Thanks for reading! |
(( <|    uu    |>  <____________________|
    |          |      
    |.        .|      
     o--------o

Feel free to steal the above ASCII art.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FormDecoder = schema.NewDecoder() // Exposed to allow users to modify behaviour.

FormDecoder is the schema.Decoder used for every FormHandler.

Functions

func DefaultEmptyHandler

func DefaultEmptyHandler(w http.ResponseWriter, _ *http.Request)

DefaultEmptyHandler simply responds with a 404.

func DefaultErrorHandler

func DefaultErrorHandler(err error) http.Handler

DefaultErrorHandler writes the content of the error as the response and responds with a 500.

func DefaultRedirectHandler added in v1.0.0

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

DefaultRedirectHandler should redirect back to previous page.

Types

type FormHandler added in v1.0.0

type FormHandler[T any] struct {
	// Handler is the main handler of a FormHandler.
	// The original http.Request and deserialized form are given as parameters.
	Handler func(*http.Request, *T) error

	// ErrorHandler is used whenever Handler returns an error.
	// The default value is DefaultErrorHandler.
	ErrorHandler func(error) http.Handler

	// RedirectHandler is invoked once Handler succeeds, and is meant to provide functionality for redirecting
	// after a successful POST request.
	// The default value is DefaultRedirectHandler.
	RedirectHandler http.Handler
}

FormHandler is a http.Handler compatible struct which handles http.MethodPost requests with form bodies.

func NewFormHandler added in v1.0.0

func NewFormHandler[T any](handler func(*http.Request, *T) error, options ...FormHandlerOption[T]) *FormHandler[T]

NewFormHandler initializes a new FormHandler, and only the FormHandler.Handler is required. All other fields have default values.

func (FormHandler[T]) ServeHTTP added in v1.0.0

func (f FormHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP delegates all logic to the handler methods of FormHandler.

type FormHandlerOption added in v1.0.0

type FormHandlerOption[T any] func(p *FormHandler[T])

type PageHandler

type PageHandler[T any] struct {
	// Handler determines what data is to be filled in Template.
	Handler func(*http.Request) (*T, error)

	// Template is the HTML template for our page.
	// The fields of this Template are filled by the result of Handler.
	Template *template.Template

	// ErrorHandler is used whenever Handler returns an error.
	// The default value is DefaultErrorHandler.
	ErrorHandler func(error) http.Handler

	// EmptyHandler is used whenever Handler returns a nil.
	// The default value is DefaultEmptyHandler.
	EmptyHandler http.Handler
}

PageHandler represent the content to be served as the result of some logic being performed in the MainPageHandler. The template can be seen a structure for this data.

func NewPageHandler

func NewPageHandler[T any](handler func(*http.Request) (*T, error), template *template.Template, options ...PageHandlerOption[T]) *PageHandler[T]

NewPageHandler requires only PageHandler.Handler and PageHandler.Template fields be provided. Everything else has a default.

func (*PageHandler[T]) ServeHTTP

func (p *PageHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP delegates all logic to the handler methods of PageHandler.

type PageHandlerOption

type PageHandlerOption[T any] func(p *PageHandler[T])

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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