static: 4d63.com/static Index | Examples | Files

package static

import "4d63.com/static"

Package static generates websites from any Go web app that uses `net/http`.

Index

Examples

Package Files

build.go build_error.go doc.go event.go event_handler.go options.go response_writer.go

Variables

var DefaultOptions = Options{
    OutputDir:   "build",
    Concurrency: 50,
    DirFilename: "index.html",
}

DefaultOptions contain the default recommended Options.

func Build Uses

func Build(o Options, h http.Handler, paths []string, eh EventHandler)

Build the paths. Uses the http.Handler to get the response for each path, and writes that response to a file with it's respective path in the OutputDir specified in the Options. Does so concurrently as defined in the Options, and calls the EventHandler for every path with an Event that states that the path was built and if an error occurred. EventHandler may be nil.

Code:

handler := http.NewServeMux()
paths := []string{}

handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello %s!", path.Base(r.URL.Path))
})

paths = append(paths, "/world")

static.Build(static.DefaultOptions, handler, paths, func(e static.Event) {
    fmt.Println(e)
})

Output:

Action: build, Path: /world, StatusCode: 200, OutputPath: build/world

func BuildSingle Uses

func BuildSingle(o Options, h http.Handler, path string) (statusCode int, outputPath string, err error)

BuildSingle builds a single path. It uses the http.Handler to get the response for each path, and writes that response to a file with it's respective path in the OutputDir specified in the Options. Returns the HTTP status code returned by the handler, the output path written to and an error if one occurs.

Code:

handler := http.NewServeMux()

handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello %s!", path.Base(r.URL.Path))
})

statusCode, outputPath, err := static.BuildSingle(static.DefaultOptions, handler, "/world")
fmt.Printf("Built: /world, StatusCode: %d, OutputPath: %v, Error: %v", statusCode, outputPath, err)

Output:

Built: /world, StatusCode: 200, OutputPath: build/world, Error: <nil>

type Action Uses

type Action string

Action is something taken place, captured in an Event.

const (
    // BUILD is the building of a path.
    BUILD Action = "build"
)

type Event Uses

type Event struct {
    // The action taken place on the path.
    Action Action
    // The path the action has taken place on.
    Path string
    // The HTTP status code returned when the path was built.
    StatusCode int
    // The output path where the output of the action was written to.
    OutputPath string
    // An error if an error occurred while performing the action, otherwise nil.
    Error error
}

Event represents action has taken place for a path in the build process, and includes an error if an error occurred while the action took place.

func (Event) String Uses

func (e Event) String() string

A simple string representation of an Event in the format:

Action: build, Path: <path>, StatusCode: 200|404|etc, OutputPath: <output-path>

And when the Event has an error:

Action: build, Path: <path>, StatusCode: 200|404|etc, OutputPath: <output-path>, Error: <error>

type EventHandler Uses

type EventHandler func(event Event)

EventHandler is a function that will handle Event's generated by the Build process.

type Options Uses

type Options struct {
    // The directory where files will be written when building.
    OutputDir string
    // The number of files that will be built concurrently.
    Concurrency int
    // The filename to use when saving directory paths. e.g. index.html
    DirFilename string
}

Options for configuring the behavior of the Build functions. Get the default options with DefaultOptions.

Package static imports 7 packages (graph). Updated 2018-02-21. Refresh now. Tools for package owners.