Documentation ¶
Index ¶
- Constants
- Variables
- func GenerateHashString(data []byte) string
- func ParseCommandArgs(args ...string) []string
- func RandStr(strSize int, randType string) string
- func ReadJSON[T any](route *Route) (value T, err error)
- func WriteLogClosure(t time.Time) string
- func WriteLogStart(t time.Time) string
- type BGTimer
- type Certificate
- type Config
- type Domain
- func (d *Domain) DefaultSubdomain() *Subdomain
- func (d *Domain) DisableSubdomain(name string) *Domain
- func (d *Domain) EnableSubdomain(name string) *Domain
- func (d *Domain) Header() http.Header
- func (d *Domain) RegisterDefaultSubdomain(c SubdomainConfig) *Subdomain
- func (d *Domain) RegisterSubdomain(subdomain string, c SubdomainConfig) *Subdomain
- func (d *Domain) RemoveHeader(name string) *Domain
- func (d *Domain) RemoveSubdomain(name string) *Domain
- func (d *Domain) SetErrorTemplate(content string) error
- func (d *Domain) SetHeader(name, value string) *Domain
- func (d *Domain) SetHeaders(headers [][2]string) *Domain
- func (d *Domain) Subdomain(subdomain string) *Subdomain
- type InitCloseFunction
- type LogLevel
- type Mutex
- type Process
- type ResponseWriter
- type Route
- func (route *Route) DecodeCookie(name string, value interface{}) (bool, error)
- func (route *Route) DecodeCookiePerm(name string, value interface{}) (bool, error)
- func (route *Route) DeleteCookie(name string)
- func (route *Route) Error(statusCode int, message string, a ...any)
- func (route *Route) IsInternalConn() bool
- func (route *Route) Log(level LogLevel, a ...any)
- func (route *Route) Logf(level LogLevel, format string, a ...any)
- func (route *Route) Logln(level LogLevel, a ...any)
- func (route *Route) Print(a ...any)
- func (route *Route) Printf(format string, a ...any)
- func (route *Route) Println(a ...any)
- func (route *Route) ReverseProxy(URL string) error
- func (route *Route) ServeCustomFile(name string, data []byte)
- func (route *Route) ServeCustomFileWithTime(name string, data []byte, t time.Time)
- func (route *Route) ServeData(data []byte)
- func (route *Route) ServeFile(path string)
- func (route *Route) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (route *Route) ServeRootedFile(path string)
- func (route *Route) ServeText(text string)
- func (route *Route) SetCookie(name string, value interface{}, maxAge int) error
- func (route *Route) SetCookiePerm(name string, value interface{}, maxAge int) error
- func (route *Route) StaticServe(serveHTML bool)
- type Router
- func (router *Router) ClearLogs()
- func (router *Router) ExecIsRunning(name string) (bool, error)
- func (router *Router) Log(level LogLevel, a ...any)
- func (router *Router) Logf(level LogLevel, format string, a ...any)
- func (router *Router) Logln(level LogLevel, a ...any)
- func (router *Router) NewServer(cfg Config) (srv *Server, err error)
- func (router *Router) PlainPrintf(format string, a ...any)
- func (router *Router) Print(a ...any)
- func (router *Router) Printf(format string, a ...any)
- func (router *Router) Println(a ...any)
- func (router *Router) RegisterBackgroundTask(task *Task, timer BGTimer) error
- func (router *Router) RegisterExec(name, dir string, redirect bool, execName string, args ...string) error
- func (router *Router) RegisterExecAndStart(name, dir string, redirect bool, execName string, args ...string) error
- func (router *Router) RestartExec(name string) error
- func (router *Router) Server(port int) *Server
- func (router *Router) SetBackgroundTaskState(name string, timer BGTimer) error
- func (router *Router) SetInternalConnFilter(f func(remoteAddress string) bool) *Router
- func (router *Router) Start()
- func (router *Router) StartExec(name string) error
- func (router *Router) StartProgram(name string, args []string, dir string, wait bool, ...) (exitCode int, err error)
- func (router *Router) Stop()
- func (router *Router) StopAllExecs()
- func (router *Router) StopExec(name string) error
- func (router *Router) StopServer(port int) error
- type ServeFunction
- type Server
- func (srv *Server) DefaultDomain() *Domain
- func (srv *Server) Domain(domain string) *Domain
- func (srv *Server) Header() http.Header
- func (srv *Server) Log(level LogLevel, a ...any)
- func (srv *Server) Logf(level LogLevel, format string, a ...any)
- func (srv *Server) Logln(level LogLevel, a ...any)
- func (srv *Server) Print(a ...any)
- func (srv *Server) Printf(format string, a ...any)
- func (srv *Server) Println(a ...any)
- func (srv *Server) ReadFileConcurrent(filePath string) ([]byte, error)
- func (srv *Server) RegisterDefaultDomain(displayName string) *Domain
- func (srv *Server) RegisterDefaultRoute(displayName string, c SubdomainConfig) (*Domain, *Subdomain)
- func (srv *Server) RegisterDomain(displayName, domain string) *Domain
- func (srv *Server) RemoveHeader(name string) *Server
- func (srv *Server) SetErrorTemplate(content string) error
- func (srv *Server) SetHeader(name, value string) *Server
- func (srv *Server) SetHeaders(headers [][2]string) *Server
- func (srv *Server) Shutdown()
- func (srv *Server) Start()
- type Subdomain
- func (sd *Subdomain) Disable() *Subdomain
- func (sd *Subdomain) Enable() *Subdomain
- func (sd *Subdomain) Header() http.Header
- func (sd *Subdomain) RemoveHeader(name string) *Subdomain
- func (sd *Subdomain) SetErrorTemplate(content string) error
- func (sd *Subdomain) SetHeader(name, value string) *Subdomain
- func (sd *Subdomain) SetHeaders(headers [][2]string) *Subdomain
- type SubdomainConfig
- type Task
- type TaskFunc
- type TaskInitFunc
- type Website
Constants ¶
const ( LOG_LEVEL_INFO = 4 LOG_LEVEL_DEBUG = 3 LOG_LEVEL_WARNING = 2 LOG_LEVEL_ERROR = 1 LOG_LEVEL_FATAL = 0 )
const ( ErrNoErr = iota ErrBadURL ErrServerOffline ErrWebsiteOffline ErrDomainNotFound ErrSubdomainNotFound )
const LogFormat string = "02/Jan/2006:15:04:05"
Variables ¶
var ( HashKeyString = "NixPare Server" BlockKeyString = "github.com/alessio-pareto/server" )
Functions ¶
func GenerateHashString ¶ added in v1.1.1
GenerateHashString generate an hash with sha256 from data
func ParseCommandArgs ¶
func WriteLogClosure ¶ added in v1.1.1
func WriteLogStart ¶ added in v1.1.1
Types ¶
type BGTimer ¶
type BGTimer int
BGTimer is the time unit that specify how often the task will be called, defining its interval
type Certificate ¶
type Config ¶
type Config struct { Port int Secure bool Certs []Certificate }
type Domain ¶
type Domain struct { Name string // contains filtered or unexported fields }
Domain rapresents a website domain with all its subdomains. It's possible to set:
- global headers, that will be applied in every connection
- an error template, that will be used in case your logic will throw any error, so you will have a constant look
func (*Domain) DefaultSubdomain ¶
Subdomain returns the default subdomain, if set
func (*Domain) DisableSubdomain ¶
DisableSubdomain disables a subdomain
func (*Domain) EnableSubdomain ¶
EnableSubdomain enables a subdomain
func (*Domain) RegisterDefaultSubdomain ¶
func (d *Domain) RegisterDefaultSubdomain(c SubdomainConfig) *Subdomain
RegisterDefaultSubdomain registers a subdomain that is called if no other one matches perfectly the incoming connection for the same domain
func (*Domain) RegisterSubdomain ¶
func (d *Domain) RegisterSubdomain(subdomain string, c SubdomainConfig) *Subdomain
RegisterSubdomain registers a subdomain in the domain. It's asked to specify the subdomain name (with or without trailing dot) and its configuration. It the Website Dir field is empty it will be used the default value of "<srv.ServerPath>/public", instead if it's not absolute it will be relative to the srv.ServerPath
func (*Domain) RemoveHeader ¶
RemoveHeader removes a header with the given name
func (*Domain) RemoveSubdomain ¶
RemoveSubdomain unregisters a subdomain
func (*Domain) SetErrorTemplate ¶
SetErrorTemplate sets the error template used server-wise. It's required an html that contains two specific fields, a .Code one and a .Message one, for example like so:
<h2>Error {{ .Code }}</h2> <p>{{ .Message }}</p>
func (*Domain) SetHeader ¶
SetHeader adds a header to the collection of headers used in every connection
func (*Domain) SetHeaders ¶
SetHeaders adds headers to the collection of headers used in every connection. This is a faster way to set multiple headers at the same time, instead of using domain.SetHeader. The headers must be provided in this way:
headers := [][2]string { { "name1", "value1" }, { "name2", "value2" }, } d.SetHeaders(headers)
type InitCloseFunction ¶
type Mutex ¶
type Mutex struct {
// contains filtered or unexported fields
}
func (*Mutex) CreateJobs ¶
func (*Mutex) ListenForSignal ¶
func (m *Mutex) ListenForSignal()
func (*Mutex) SendSignal ¶
func (m *Mutex) SendSignal()
type ResponseWriter ¶
type ResponseWriter struct {
// contains filtered or unexported fields
}
func (*ResponseWriter) Header ¶
func (w *ResponseWriter) Header() http.Header
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(statusCode int)
type Route ¶
type Route struct { W *ResponseWriter R *http.Request Srv *Server Router *Router Secure bool Host string RemoteAddress string Website *Website DomainName string SubdomainName string Domain *Domain Subdomain *Subdomain RequestURI string Method string QueryMap map[string]string ConnectionTime time.Time AvoidLogging bool // contains filtered or unexported fields }
func (*Route) DecodeCookie ¶
func (*Route) DecodeCookiePerm ¶
func (*Route) DeleteCookie ¶
func (*Route) IsInternalConn ¶
IsInternalConn tells wheather the incoming connection should be treated as a local connection. The user can add a filter that can extend this selection to match their needs
func (*Route) ReverseProxy ¶
func (*Route) ServeCustomFile ¶ added in v1.5.0
func (*Route) ServeCustomFileWithTime ¶ added in v1.5.0
func (*Route) ServeRootedFile ¶
func (*Route) SetCookiePerm ¶
func (*Route) StaticServe ¶
type Router ¶ added in v1.1.1
type Router struct { CleanupF func() error ServerPath string // contains filtered or unexported fields }
func (*Router) ExecIsRunning ¶ added in v1.1.1
func (*Router) PlainPrintf ¶ added in v1.3.0
func (*Router) RegisterBackgroundTask ¶ added in v1.1.1
RegisterBackgroundTask registers the task with the timer. The timer however can be changed (only by the user) and its not dependent only on the task, but it belongs to the server, so it might happen that the first execution can happen as soon as the Task is registered
func (*Router) RegisterExec ¶ added in v1.1.1
func (*Router) RegisterExecAndStart ¶ added in v1.1.1
func (*Router) RestartExec ¶ added in v1.1.1
func (*Router) SetBackgroundTaskState ¶ added in v1.1.1
SetBackgroundTaskState changes the timer of a task with the given name, if found
func (*Router) SetInternalConnFilter ¶ added in v1.1.1
func (*Router) StartProgram ¶ added in v1.1.1
func (router *Router) StartProgram(name string, args []string, dir string, wait bool, stdin, stdout, stderr string) (exitCode int, err error)
StartProgram executes a program with the given args and working directory. It's possible to specify if to wait for the process to exit or not: in the latter case you should not consider the exitCode result. For the input, output and error files, theese are the cases:
- if you pass an empty string, you will have a null file
- if you pass the string "INHERIT", it will inherit the log file of the server (for stdin it will be a null file)
- otherwise, it will open the file specified, if found
func (*Router) StopAllExecs ¶ added in v1.1.1
func (router *Router) StopAllExecs()
func (*Router) StopServer ¶ added in v1.1.1
type ServeFunction ¶
type ServeFunction func(route *Route)
type Server ¶
type Server struct { Secure bool Running bool Online bool OnlineTime time.Time Server *http.Server Router *Router ServerPath string // contains filtered or unexported fields }
func (*Server) DefaultDomain ¶
DefaultDomain returns the default domain, if set
func (*Server) Domain ¶
Domain returns the domain with the given name registered in the server, if found
func (*Server) ReadFileConcurrent ¶
func (*Server) RegisterDefaultDomain ¶
RegisterDefaultDomain registers a domain that is called if no other domain matches perfectly the incoming connection
func (*Server) RegisterDefaultRoute ¶
func (srv *Server) RegisterDefaultRoute(displayName string, c SubdomainConfig) (*Domain, *Subdomain)
This is a shortcut for registering the default logic applied for every connection not matching any other specific domain and subdomain. It's the combination of srv.RegisterDefaultDomain(displayName).RegisterDefaultSubdomain(c)
func (*Server) RegisterDomain ¶
RegisterDomain registers a domain in the server. It's asked to specify a display name used in the logs and the effective URL of the domain (do not specify any protocol or port). If the domain name is an empy string it will be treated as the default domain (see srv.RegisterDefaultDomain)
func (*Server) RemoveHeader ¶
func (*Server) SetErrorTemplate ¶
SetErrorTemplate sets the error template used server-wise. It's required an html that contains two specific fields, a .Code one and a .Message one, for example like so:
<h2>Error {{ .Code }}</h2> <p>{{ .Message }}</p>
func (*Server) SetHeaders ¶
type Subdomain ¶
type Subdomain struct { Name string // contains filtered or unexported fields }
Subdomain rapresents a particular subdomain in a domain with all the logic. It's required a serve function, which will determine the logic of the website, and a Website, with all its options. It's possible to set:
- default headers, that will be applied in every connection
- an error template, that will be used in case your logic will throw any error, so you will have a constant look
- the subdomain to be offline, can be reverted
- an initializer function, called when the server is starting up
- a cleanup function, called when the server is shutting down
func (*Subdomain) RemoveHeader ¶
RemoveHeader removes a header with the given name
func (*Subdomain) SetErrorTemplate ¶
SetErrorTemplate sets the error template used server-wise. It's required an html that contains two specific fields, a .Code one and a .Message one, for example like so:
<h2>Error {{ .Code }}</h2> <p>{{ .Message }}</p>
func (*Subdomain) SetHeader ¶
SetHeader adds a header to the collection of headers used in every connection
func (*Subdomain) SetHeaders ¶
SetHeaders adds headers to the collection of headers used in every connection. This is a faster way to set multiple headers at the same time, instead of using subdomain.SetHeader. The headers must be provided in this way:
headers := [][2]string { { "name1", "value1" }, { "name2", "value2" }, } d.SetHeaders(headers)
type SubdomainConfig ¶
type SubdomainConfig struct { Website Website ServeF ServeFunction InitF InitCloseFunction CloseF InitCloseFunction }
SubdomainConfig is used to create a Subdomain. The Website and ServeF fields must not be nil, instead InitF and CloseF are optional
type Task ¶
type Task struct { StartupF TaskFunc ExecF TaskFunc CleanupF TaskFunc // contains filtered or unexported fields }
Task is composed of a name set upon creation and of 3 functions necessary of the correct execution of a kind of program. Every function is panic-protected, this means that the entire server will not crash when some parts of the task fails badly; this does not mean that you can't handle panics by yourself, but if they are not handled its like catching them and returning their message as an error. If a function returns an error, this will be logged, providing the task name and which function called it automatically (the task will be disabled if the one failing is the startup one or but you can do it manually, see router.SetBackgroundTaskState)
func NewTask ¶ added in v1.1.0
func NewTask(name string, f TaskInitFunc) *Task
NewTask creates a new task with the given name. The name is immutable. Creating a new Task is not enough for making it executable and active: for that you have to call router.RegisterBackgroundTask function
func (Task) ListenForExit ¶ added in v1.1.1
func (t Task) ListenForExit()
ListenForExit waits until the exit signal is received from the manager. This signal is sent when the server is shutting down: the manager will wait for 10 seconds and then it will call the cleanup function
type TaskFunc ¶
TaskFunc is the executable part of the program. The manager will provide, upon call, the router (and thus the server) and the task itself; changed to the task are allowed (exept for the name): you can modify through the router the timer of the task and also the functions themself! See TaskInitFunc, NewTask and router.RegisterBackgroundTask for the creation of a Task
type TaskInitFunc ¶ added in v1.1.0
type TaskInitFunc func() (startupF, execF, cleanupF TaskFunc)
TaskInitFunc is called when creating a task and its provided by the user. This function need to return 3 TaskFunc (they can be nil) and they will be set to the created task. The kind of functions are:
- the startup function: called only upon creation, if it fails (panics or returns an error) the task will be disabled automatically
- the exec function: called every time, could be interrupted if the server is shutting down; in this case, you will receive a signal on Task.ListenForExit, after that you will have 10 seconds before the server will call the cleanup function and exit
- the cleanup function: called when the server is shutting down, this must not be potentially blocking (must end in a reasonable time)