httphelper

package module
v0.0.0-...-e1f1f62 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2020 License: Zlib Imports: 10 Imported by: 0

README

HTTP Helper: Make servers easy again!

HTTP Helper is a simple library to make it easier to create simple web servers that need to provide a mix of static, template, and generated content. This is not suitable for large websites, as all content is loaded and served from memory.

When the server is initialized all data files are loaded and classified. Once classification is done and all handlers are assigned their requested resources, anything left over is served as static content.

Documentation

Overview

HTTP Helper: Make servers easy again!

HTTP Helper is a simple library to make it easier to create simple web servers that need to provide a mix of static, template, and generated content. This is not suitable for large websites, as all content is loaded and served from memory.

When the server is initialized all data files are loaded and classified. Once classification is done and all handlers are assigned their requested resources, anything left over is served as static content.

Index

Constants

This section is empty.

Variables

View Source
var TagsFirst = map[string][]string{
	".go":     {"Go"},
	".static": {"Static"},
}

First/Last part tags for classifying files during load. Hardcoded Tags: Resource

View Source
var TagsLast = map[string][]string{
	".htm":  {"HTML"},
	".html": {"HTML"},
	".css":  {"StyleSheet"},
	".js":   {"JavaScript"},
}

Functions

func GetFileTags

func GetFileTags(name string) []string

GetFileTags finds the file tags for a file with the given name. The returned slice of tags is yours to keep.

Types

type File

type File struct {
	Name    string // File name.
	Source  string // File path (AXIS syntax, including loc ids).
	Content []byte
	Tags    map[string]bool
}

File is the internal representation of a loaded file.

func (*File) FullPath

func (f *File) FullPath() string

Return the full AXIS path of the file.

type HTTPErrorHandler

type HTTPErrorHandler func(w http.ResponseWriter, r *http.Request, status int)

HTTPErrorHandler is a superset of an HTTP handler that also takes a status code. Called whenever the server detects an error. Currently this is only called with 404 errors.

type Handler

type Handler interface {
	// contains filtered or unexported methods
}

Handler is a SimpleHandler, TemplateHandler, or JSONHandler.

type JSONHandler

type JSONHandler struct {
	// AXIS paths for resources assigned to this Handler. You may use other resources as well,
	// but anything listed here will be marked off the list of files to serve statically.
	Resources []string

	// Take a request, and return an object to marshal as JSON.
	Data func(w http.ResponseWriter, r *http.Request) interface{}

	Path string // The path this handler is responsible for.
}

JSONHandler is the handler type for binding a function or whatever to a path.

type Logger

type Logger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

Loggers passed into this package need to satisfy this interface.

type Server

type Server struct {
	//handlers []Handler
	Files    map[string]*File
	Handlers *http.ServeMux
	// contains filtered or unexported fields
}

Server is a convenient holder for the HTTP handlers and the loaded files generated by Initialize.

func Initialize

func Initialize(fs *axis2.FileSystem, path string, handlers []Handler, errhandler HTTPErrorHandler, log ...Logger) (error, *Server)

Initialize creates a new Server based on the given data directory and handlers.

If there is no handler for "/" one will automatically be created that simply calls the error handler with a 404.

The Loggers are optional. If you provide one logger it will be used by everything. Two will be used for info and errors. Only the first two will be used. You may pass nil for any Logger, in which case that kind of message will not be logged.

type SimpleHandler

type SimpleHandler struct {
	// AXIS paths for resources assigned to this Handler. You may use other resources as well,
	// but anything listed here will be marked off the list of files to serve statically.
	Resources []string

	// The handler logic. See also http.HandlerFunc.
	Logic http.Handler

	Path  string // The path this handler is responsible for.
	Loose bool   // If true do no automatically insert a check for path supersets.
}

SimpleHandler is the handler type for binding a function or whatever to a path.

type TemplateHandler

type TemplateHandler struct {
	// AXIS paths for resources assigned to this Handler. You may use other resources as well,
	// but anything listed here will be marked off the list of files to serve statically.
	Resources []string
	Template  string // The AXIS path to the template file (also list in Resources)

	// Return the data object the template needs to operate.
	Data func(w http.ResponseWriter, r *http.Request) interface{}

	Path string // The path this handler is responsible for.
	// contains filtered or unexported fields
}

TemplateHandler is the type for binding a template and data generator to a path.

Jump to

Keyboard shortcuts

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