nstatic

package module
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: MIT Imports: 20 Imported by: 0

README

nstatic

Not a static website framework

Example

package main

import (
    "net/http"
    "log"

    "github.com/tidwall/nstatic"
)

func main() {
    handler, err := nstatic.NewHandlerFunc("website_root", nil)
    if err != nil {
        log.Fatal(err)
    }
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandlerFunc

func NewHandlerFunc(path string, opts *Options) (http.HandlerFunc, error)

NewHandlerFunc returns an http.HandlerFunc that does very simple pages serving from the specified path. The pageData function can be used to return template data.

Types

type FS added in v0.12.0

type FS interface {
	Open(path string) (fs.File, error)
	ReadFile(path string) ([]byte, error)
}

type Options

type Options struct {
	LogOutput    io.Writer
	PageData     func(page *Page) error
	FuncMap      map[string]interface{}
	AllowGzip    bool
	RedirectHost string
	// APIEndpoints are all the endpoints that are not backed by an html
	// template.
	APIEndpoints []string
	EmbeddedFS   FS
}

Options for the handler

type Page

type Page struct {
	Input struct {
		Error       error
		LocalPath   string
		ContentType string
		Request     *http.Request
	}
	Output struct {
		Cookies []*http.Cookie
		Data    interface{}
	}
}

A Page is passed to the pageData function

func (*Page) Override added in v0.9.0

func (p *Page) Override(statusCode int, contentType string, body []byte)

Override the HTTP response with the provide content.

func (*Page) Redirect

func (p *Page) Redirect(url string)

Redirect will send an HTTP 302 with url as the Location.

func (*Page) SetCookie

func (p *Page) SetCookie(cookie *http.Cookie)

SetCookie adds a cookie to the HTTP response

Jump to

Keyboard shortcuts

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