seiteki

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package seiteki wraps around fasthttp.Server and fasthttp.FS to provide a simple to use static host serve for routed web applications like Angular or VueJS apps.

Specified source files are served via fasthttp.FS and every other request will be directed directly onto the specified index file.

Index

Constants

View Source
const (
	// Version is the seiteki package version.
	// This version specification is only for
	// the package, not the application wrapper.
	Version = "1.4.0"
)

Variables

View Source
var FileRx = regexp.MustCompile(`^.*\.(ico|css|js|svg|gif|jpe?g|png|html?)$`)

FileRx defines the regular expression used to match static files by file extension. If you really want to, just overwrite this variable to replace it.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Expose address and port
	Addr string `json:"addr"`
	// Duration how long static files will be cached
	// by setting cache control headers.
	// See here how to format this:
	// https://golang.org/pkg/time/#ParseDuration
	CacheDuration string `json:"cacheduration"`
	// Whether or not to compress static files
	Compress bool `json:"compress"`
	// Static files location to serve from
	StaticDir string `json:"staticdir"`
	// Default index file name
	IndexFile string `json:"indexfile"`

	// RouteMode used for determining if the
	// requested path is a static file or
	// SPA route. Default is "regex".
	RouteMode RouteMode `json:"routemode"`

	// SSL key file directory
	KeyFile string `json:"keyfile"`
	// SSL cert file directory
	CertFile string `json:"certfile"`
}

Config includes everything which defines preferences and parameters to configure the server.

type Logger

type Logger interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warning(args ...interface{})
	Warningf(format string, args ...interface{})
}

Logger describes a generic logger to be used for request logging

type RouteMode

type RouteMode string

RouteMode is the mode type used to determine if the accessed request path is a file or SPA route.

const (
	// Route mode using regular expression on req path.
	//
	// This is useful when you exacly know which file extensions
	// are served as static files and that they are included in
	// the match regex. This mode is especially verry fast and
	// has a less performance footprint because no file ops are
	// executed on path check.
	RouteModeRegex RouteMode = "regex"

	// Route mode using fs stat on accessed file.
	//
	// This mode can be used if you don't know exacly which file
	// extensions are served as static files or if these extensions
	// are not included in the match regex. Also this is useful if
	// you may have path missmatches for some reasons. This mode only
	// serves a file when it is actually existent on the fs, otherwise
	// the index file will be served. But keep in mind that this mode
	// executes a fs stat operation on EVERY request, which might lead
	// to poor performance.
	RouteModeStat RouteMode = "stat"

	// Route mode which bypasses the SPA matcher and tries to serve
	// all request paths as static file.
	//
	// This is especially useful if you want to use seiteki as pure
	// static file server without SPA routing.
	RouteModeStatic RouteMode = "static"
)

type Seiteki

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

Seiteki web server

func New

func New(config *Config) (*Seiteki, error)

New creates a new instance of Seiteki with the passed configuration.

func (*Seiteki) IsStaticFile

func (server *Seiteki) IsStaticFile(path []byte) bool

func (*Seiteki) ListenAndServeBlocking

func (server *Seiteki) ListenAndServeBlocking() error

ListenAndServeBlocking blocks the current go routine and starts the listening and serving routines. Depending on if the SSL config was passed properly, the server will be started using SSL, else, it will automatically use non-SSL setup.

func (*Seiteki) RequestHandler

func (server *Seiteki) RequestHandler(ctx *fasthttp.RequestCtx)

RequestHandler checks if the request destination is a file or a web route. If it is a file, serve the file via FS handler, else serve the "index.html" file.

func (*Seiteki) SetLogger

func (server *Seiteki) SetLogger(logger Logger)

SetLogger sets a logger interface as request logger

Jump to

Keyboard shortcuts

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