server

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: MIT Imports: 18 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLogLayout = errors.New("Invalid log layout")

Functions

func Error

func Error(w http.ResponseWriter, code int) int64

A convenient way to call ErrorEx()

func ErrorEx

func ErrorEx(w http.ResponseWriter, code int, title, msg string) int64

ErrorEx writes http error, accrording status code and msg, return number of bytes write to ResponseWriter. Parameter msg is a string contains html, could be ignored.

func ErrorFile404

func ErrorFile404(w http.ResponseWriter, abspath string) (int64, error)

ErrorFile404 writes 404 file to client. abspath is path of 404 file.

Types

type Auth

type Auth struct {
	Username string
	Password string

	// paths which use password to protect, relative to "/".
	// if Paths is empty, all paths are protected.
	// not used currently
	Paths []string

	Method AuthMethod
}

type AuthMethod

type AuthMethod string
const BasicMethod AuthMethod = "basic"
const DigestMethod AuthMethod = "digest"

type Config

type Config struct {
	Root    string         // Root path of the website. Default is current working directory.
	Path404 *ErrorFilePath // Path of custom 404 file, under directory of Root.
	// When a 404 not found error occurs, the file's content will be send to client.
	// nil means do not use 404 file.
	Path401 *ErrorFilePath // Path of custom 401 file, under directory of Root.
	// When a 401 unauthorized error occurs, the file's content will be send to client.
	// nil means do not use 401 file.
	IndexName Index // File name of index, priority depends on the order of values.
	// Default is []string{"index.html", "index.htm"}.
	ListDir bool // If no index file provide, show file list of the directory.
	// Default is false.
	Gzip     bool  // If turn on gzip compression, default is true.
	NoCache  bool  // If true, ran will write some no-cache headers to the response. Default is false.
	CORS     bool  // If true, ran will write some CORS headers to the response. Default is false.
	Auth     *Auth // If not nil, turn on authentication.
	ServeAll bool  // If is false, path start with dot will not be served, that means a 404 error will be returned.
}

type ErrorFilePath added in v0.1.5

type ErrorFilePath struct {
	Abs string // Absolute path of error file, e.g. /data/wwwroot/404.html
	Rel string // Path of error file, relative to the root, e.g. /404.html
}

ErrorFilePath describe path of a 401/404 file which is under directory of Root.

type Header http.Header

func (Header) String

func (this Header) String() string

type Index

type Index []string

func (*Index) Set

func (this *Index) Set(value string) error

func (*Index) String

func (this *Index) String() string

type LogLayout

type LogLayout string

LogLayout indicate what information will be present in access log. LogLayout is a string contains format specifiers. The format specifiers is a tag start with a percent sign and followd by a letter. The format specifiers will be replaced by corresponding values when the log is created.

Below are format specifiers and there meanings:

%% Percent sign (%) %i Request id %s Response status code %h Host %a Client ip address %m Request method %l Request url %r Referer %u User agent %n Number of bytes transferred %t Response time %c Compression status (gzip / none) %S Scheme (http or https)

var LogLayoutMin LogLayout = `Access #%i: [%s] [%a] [%m] [%l] [%n]`
var LogLayoutNormal LogLayout = `` /* 143-byte string literal not displayed */
var LogLayoutShort LogLayout = `Access #%i: [%s] [%h] [%a] [%m] [%S] [%l] [%r] [%u] [%n] [%t] [%c]`

func (*LogLayout) IsLegal

func (this *LogLayout) IsLegal() bool

IsLegal checks if a log layout is legal.

type RanServer

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

func NewRanServer

func NewRanServer(c Config, logger *log.Logger) *RanServer

func (*RanServer) RedirectToHTTPS

func (this *RanServer) RedirectToHTTPS(port uint) http.HandlerFunc

redirect to https page

func (*RanServer) Serve

func (this *RanServer) Serve() http.HandlerFunc

make the request handler chain: log -> authentication -> gzip -> original handler TODO: add ip filter: log -> [ip filter] -> authentication -> gzip -> original handler

Jump to

Keyboard shortcuts

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