server

package
v0.0.0-...-fa7308e Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSitePath = "/srv/webby/"

Variables

This section is empty.

Functions

func CallOnChange

func CallOnChange(callback func(FileChangeSignal) bool, filePath string)

Watches for changes in the given file, intended for configs but anything should work. This function will report all errors through the given callback.

This function will not call the given callback more than once per detected file change and because of this file modification date changes take precedence over size changes.

Callback should return true to terminate the goroutine checking for changes and false to continue.

Types

type CustomHandler

type CustomHandler struct {
	Handler func(http.ResponseWriter, *http.Request)
}

A custom handler that may respond with special or dynamic data rather than a static file.

func (CustomHandler) ServeHTTP

func (h CustomHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type FileChangeSignal

type FileChangeSignal = uint8
const (
	ReadError FileChangeSignal = iota
	InitialReadError
	SizeChange
	TimeModifiedChange
)

type Handler

type Handler struct {
	// List of all valid web paths that this handler will respond to.
	ValidPaths []string

	// A map of URL paths to their corosponding file path.
	PathMap map[string]string
	// contains filtered or unexported fields
}

Responsible for handling HTTP requests with one of a custom response from a custom handler, or a static file, prioritized in that order.

func NewHandler

func NewHandler() *Handler

Creates a new handler that will log messages to the given log.

func (*Handler) AddDeadResponses

func (h *Handler) AddDeadResponses(paths []string)

For each path given a response that redirects the client to the same path but on itself (e.g. "http://localhost/some/dead/path") will be given. This creates a custom handler, adding another custom handler will override this dead response. If a file is mapped to the same path as this dead response then, like other custom handlers, the dead response takes priority.

func (*Handler) MapDir

func (h *Handler) MapDir(dirPath string) error

Map a directory and all subdirectories to paths on the server. All directory roots, when requested, will serve an "index.html" file from that directory.

func (*Handler) MapFile

func (h *Handler) MapFile(uriPath, filePath string) error

Maps the given request URI to a file path. Returns an error if a stat of the given file path fails.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Server

type Server struct {
	ReqHandler *Handler
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ServerOptions) (*Server, error)

Creates a new server given the specified options. Will return an error if any of the given paths could not be statted or if the program lacks read permissions. This function will map directories from the options given.

func (*Server) Start

func (s *Server) Start() error

Starts the server, if TLS is supports then it is started in another thread and regular HTTP is started in the current thread. This function will only ever return on an error. If the server is started in this fashion then it may be stopped using the `Server.Stop()` method, in which case it will return an error indicated this.

func (*Server) StartThreaded

func (s *Server) StartThreaded() chan ServerThreadCommand

Starts the server in a seperate thread and returns a channel for giving said thread commands. This method, unlike the more standard `Server.Start()` method, cannot be stopped using the `Server.Stop()` method and must instead be instructed to stop using the provided channel. This method also does not report errors except in logs.

func (*Server) Stop

func (s *Server) Stop() error

Stops a server started by the `Server.Start()` method. This method will not stop servers started using the `Server.StartThreaded()` method.

type ServerOptions

type ServerOptions struct {
	// Path to the root of the website to host. Use an empty string for default.
	// See `server.DefaultSitePath`
	Site string

	// Path to a TLS/SSL certificate. Use an empty string for no HTTPS.
	Cert string

	// Path to a TLS/SSL private key. Use an empty string for no HTTPS.
	Key string

	// The port to host on, negative numbers and zero will utilize a default (80
	// for HTTP and 443 for HTTPS).
	Port int32

	// Path to a file for logging. Use an empty string for no log file.
	Log string

	// Log level for printing to standard out. Can be "All", "None", "Error",
	// "Warning", or "Info".
	LogLevelPrint string

	// Log level for writing to file out. Can be "All", "None", "Error", "Warning",
	// or "Info".
	LogLevelRecord string

	// Whether or not to check for changes in the config or site files and reload
	// automatically.
	AutoReload bool

	// Paths that should be granted a dead response, can be used for fucking with
	// bot probing or the like. A dead response is just the name I gave to
	// redirecting a request back onto the client for the same path.
	DeadPaths []string
}

func DefaultOptions

func DefaultOptions() ServerOptions

Get the default configuration.

func LoadConfigFromPath

func LoadConfigFromPath(path string) (ServerOptions, error)

Tries to parse JSON for a `ServerOptions` with the file at the given path. Returns an error and a default configuration on failure.

func (*ServerOptions) SupportsTLS

func (opts *ServerOptions) SupportsTLS() bool

Returns true if the config has the needed fields populated to support TLS and HTTPS connections.

func (*ServerOptions) WriteToFile

func (opts *ServerOptions) WriteToFile(path string) error

type ServerThreadCommand

type ServerThreadCommand = uint8

Represents a command that may be given to a running server thread through a channel.

const (
	// Shuts off the running thread and returns.
	Shutoff ServerThreadCommand = iota

	// Will close the current server and reinstantiate it from the same options and
	// log as provided during construction.
	Restart
)

Jump to

Keyboard shortcuts

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