greener

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 26 Imported by: 3

README

Go Greener

Copy greener to ~/go/pkg/github.com/thejimmyg/greener then run:

go run cmd/hello/main.go
go run cmd/simple/main.go

Benchmark:

wrk -t 8 -c 128 http://localhost:8000/
Running 10s test @ http://localhost:8000/
  8 threads and 128 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   428.71us    1.80ms 110.85ms   99.73%
    Req/Sec    55.58k     5.15k   85.01k    76.65%
  4452080 requests in 10.10s, 3.68GB read
Requests/sec: 440814.53
Transfer/sec:    372.89MB

Documentation

Overview

Package greener provides more efficient ways of building web applications

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateETag

func GenerateETag(content []byte) string

func HTMLPrintf

func HTMLPrintf(h string, hws ...template.HTML) template.HTML

HTMLPrintf takes a string containing %s characters and a set of template.HTML strings and returns an template.HTML with the placeholders substituted. This is faster than using template/html Template objects by about 8x but less safe in that no context specific checks about where you are substituing things are made.

func NewCompressedFileHandler

func NewCompressedFileHandler(root http.FileSystem) http.Handler

func StaticContentHandler

func StaticContentHandler(logger Logger, content []byte, contentType string) http.HandlerFunc

func StaticFaviconHandler

func StaticFaviconHandler(logger Logger, icon512 *image.Image) http.HandlerFunc

func StaticIconHandler

func StaticIconHandler(logger Logger, icon512 *image.Image, etag string, sizes []int) http.HandlerFunc

func Text

func Text(t string) template.HTML

Text escapes some specical characters and returns a template.HTML which the html/template package will treat as HTML without further escaping.

Types

type ContentHandler added in v0.1.1

type ContentHandler interface {
	Hash() string
	ServeHTTP(http.ResponseWriter, *http.Request)
}

ContentHandler handles brotli and gzip compression of content as well as generating a hash so that smallet content possible can be served to the client based on the request Content-Encoding. The response is set with 1 year cache max age with the intention that the URL the handler is registered with includes the content hash so that if the content changes, so would the URL.

func NewContentHandler added in v0.1.1

func NewContentHandler(logger Logger, content []byte, contentType, salt string) ContentHandler

NewContentHandler returns a struct containing a hash of the content as well as gzip and brotli compressed content encodings. It implements http.Handler for serving the most appropriate content encoding based on the request.

type DefaultApp

type DefaultApp struct {
	ServeConfigProvider
	Logger
	EmptyPageProvider
	// contains filtered or unexported fields
}

DefaultApp implements Server in such a way that the style, script and service worker content are only generated once. If there is any service worker then code is added to the script to register the service worker. Handlers for /script.js, /style.css and /service-worker.js are all added if needed. The server will serve from either a host and port or a UNIX domain socket based on the ServeConfigProvider.

func NewDefaultApp

func NewDefaultApp(serveConfigProvider ServeConfigProvider, logger Logger, emptyPageProvider EmptyPageProvider) *DefaultApp

func (*DefaultApp) Handle

func (app *DefaultApp) Handle(path string, handler http.Handler)

func (*DefaultApp) HandleFunc

func (app *DefaultApp) HandleFunc(path string, handler http.HandlerFunc)

func (*DefaultApp) HandleWithServices added in v0.1.1

func (app *DefaultApp) HandleWithServices(path string, handler func(Services))

func (*DefaultApp) Handler

func (app *DefaultApp) Handler() http.Handler

func (*DefaultApp) NewServices

func (app *DefaultApp) NewServices(w http.ResponseWriter, r *http.Request) Services

func (*DefaultApp) Serve

func (app *DefaultApp) Serve(ctx context.Context)

type DefaultEmptyPageProvider

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

Injectors prepares an HTML page string (to be used with HTMLPrintf) from a slice of Injector.

func NewDefaultEmptyPageProvider

func NewDefaultEmptyPageProvider(injectors []Injector) *DefaultEmptyPageProvider

func (*DefaultEmptyPageProvider) Page

func (*DefaultEmptyPageProvider) PerformInjections

func (d *DefaultEmptyPageProvider) PerformInjections(app App)

type DefaultIconsInjector

type DefaultIconsInjector struct {
	Logger
	// contains filtered or unexported fields
}

func NewDefaultIconsInjector

func NewDefaultIconsInjector(logger Logger, wwwFS fs.FS) *DefaultIconsInjector

func (*DefaultIconsInjector) Inject

func (d *DefaultIconsInjector) Inject(app App) (template.HTML, template.HTML)

type DefaultLogger

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

DefaultLogger implements Logger.

func NewDefaultLogger

func NewDefaultLogger(logf func(string, ...interface{})) *DefaultLogger

func (*DefaultLogger) Errorf

func (cl *DefaultLogger) Errorf(m string, a ...interface{})

func (*DefaultLogger) Logf

func (cl *DefaultLogger) Logf(m string, a ...interface{})

type DefaultManifestInjector

type DefaultManifestInjector struct {
	Logger
	// contains filtered or unexported fields
}

func NewDefaultManifestInjector

func NewDefaultManifestInjector(logger Logger, appShortName string) *DefaultManifestInjector

func (*DefaultManifestInjector) Inject

type DefaultRequestProvider

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

DefaultRequestProvider implements RequestProvider

func NewDefaultRequestProvider

func NewDefaultRequestProvider(r *http.Request) *DefaultRequestProvider

func (*DefaultRequestProvider) R

type DefaultResponseWriterProvider

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

DefaultResponseWriterProvider implements ResponseWriterProvider

func (*DefaultResponseWriterProvider) W

type DefaultScriptInjector

type DefaultScriptInjector struct {
	Logger
	// contains filtered or unexported fields
}

func NewDefaultScriptInjector

func NewDefaultScriptInjector(logger Logger, uiSupports []UISupport) *DefaultScriptInjector

func (*DefaultScriptInjector) Inject

func (d *DefaultScriptInjector) Inject(app App) (template.HTML, template.HTML)

type DefaultScriptProvider

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

DefaultScriptProvider implements ScriptProvider

func (*DefaultScriptProvider) Script

func (dsp *DefaultScriptProvider) Script() string

type DefaultServeConfigProvider

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

DefaultServeConfigProvider implments ServeConfigProvider for returning the configuration needed for serving the app

func NewDefaultServeConfigProviderFromEnvironment

func NewDefaultServeConfigProviderFromEnvironment() *DefaultServeConfigProvider

func (*DefaultServeConfigProvider) Host

func (dscp *DefaultServeConfigProvider) Host() string

func (*DefaultServeConfigProvider) Port

func (dscp *DefaultServeConfigProvider) Port() int

func (*DefaultServeConfigProvider) UDS

func (dscp *DefaultServeConfigProvider) UDS() string

type DefaultServiceWorkerInjector

type DefaultServiceWorkerInjector struct {
	Logger
	// contains filtered or unexported fields
}

func NewDefaultServiceWorkerInjector

func NewDefaultServiceWorkerInjector(logger Logger, uiSupports []UISupport) *DefaultServiceWorkerInjector

func (*DefaultServiceWorkerInjector) Inject

type DefaultServiceWorkerProvider

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

DefaultServiceWorkerProvider implements ServiceWorkerProvider

func (*DefaultServiceWorkerProvider) ServiceWorker

func (dsp *DefaultServiceWorkerProvider) ServiceWorker() string

type DefaultServices

DefaultServices embeds other interfaces for providing services to a request handler

type DefaultStyleInjector

type DefaultStyleInjector struct {
	Logger
	// contains filtered or unexported fields
}

func NewDefaultStyleInjector

func NewDefaultStyleInjector(logger Logger, uiSupports []UISupport) *DefaultStyleInjector

func (*DefaultStyleInjector) Inject

func (d *DefaultStyleInjector) Inject(app App) (template.HTML, template.HTML)

type DefaultStyleProvider

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

DefaultStyleProvider implements StyleProvider

func (*DefaultStyleProvider) Style

func (dsp *DefaultStyleProvider) Style() string

type DefaultThemeColorInjector

type DefaultThemeColorInjector struct {
	Logger
	// contains filtered or unexported fields
}

func NewDefaultThemeColorInjector

func NewDefaultThemeColorInjector(logger Logger, themeColor string) *DefaultThemeColorInjector

func (*DefaultThemeColorInjector) Inject

type DefaultUISupport

type DefaultUISupport struct {
	StyleProvider
	ScriptProvider
	ServiceWorkerProvider
}

DefaultUISupport implements UISupport by embedding StyleProvider ScriptProvider and ServiceWorkerProvider

func NewDefaultUISupport

func NewDefaultUISupport(style, script, serviceWorker string) *DefaultUISupport

NewDefaultUISupport creates a DefaultUISupport from strings representing the style, the script and the serviceworker fragments for the component. Each can be "" to indicate the conponent doesn't need them.

type EmptyPageProvider

type EmptyPageProvider interface {
	PerformInjections(App)
	Page(title string, body template.HTML) template.HTML
}

EmptyPageProvider

type HandleFuncProvider

type HandleFuncProvider interface {
	HandleFunc(string, http.HandlerFunc)
}

Server interface

type HandleProvider

type HandleProvider interface {
	Handle(string, http.Handler)
}

type HandleWithServicesProvider added in v0.1.1

type HandleWithServicesProvider interface {
	HandleWithServices(string, func(Services))
}

type ImageData

type ImageData struct {
	Image image.Image
	ETag  string
}

ImageData holds the resized image and its ETag

type Injector

type Injector interface {
	Inject(App) (template.HTML, template.HTML)
}

Injector

type Logger

type Logger interface {
	Logf(string, ...interface{})
	Errorf(string, ...interface{})
}

Logger interface

type NewServicesProvider

type NewServicesProvider interface {
	NewServices(http.ResponseWriter, *http.Request) Services
}

type RequestProvider

type RequestProvider interface {
	R() *http.Request
}

RequestProvider interface

type ResponseWriterProvider

type ResponseWriterProvider interface {
	W() http.ResponseWriter
}

ResponseWriterProvider interface

type ScriptProvider

type ScriptProvider interface {
	Script() string
}

type ServeConfigProvider

type ServeConfigProvider interface {
	Host() string
	Port() int
	UDS() string
}

ServeConfigProvider interface for server configuration handling

type Server

type Server interface {
	ServeConfigProvider
	Serve(context.Context)
	Handler() http.Handler
}

type ServiceWorkerProvider

type ServiceWorkerProvider interface {
	ServiceWorker() string
}

type Services

type Services interface {
	Logger
	ResponseWriterProvider
	RequestProvider
}

func NewDefaultServices

func NewDefaultServices(
	serveConfigProvider ServeConfigProvider,
	logger Logger,
	responseWriterProvider ResponseWriterProvider,
	requestProvider RequestProvider,
) Services

type StyleProvider

type StyleProvider interface {
	Style() string
}

type UISupport

type UISupport interface {
	StyleProvider
	ScriptProvider
	ServiceWorkerProvider
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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