host

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProxyHandler

func ProxyHandler(target *url.URL) *httputil.ReverseProxy

ProxyHandler returns a new ReverseProxy that rewrites URLs to the scheme, host, and base path provided in target. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir.

Relative to httputil.NewSingleHostReverseProxy with some additions.

Types

type FlowController

type FlowController interface {
	DeferFlow()
	RestoreFlow()
}

FlowController exports the `DeferFlow` and `RestoreFlow` capabilities. Read more at Supervisor.

type OnInterrupt

type OnInterrupt TaskRunnerFunc

OnInterrupt is a built'n supervisor task type which fires its value(Task) when an OS interrupt/kill signal received.

func (OnInterrupt) Run

func (t OnInterrupt) Run(proc TaskProcess)

Run runs the interrupt task and completes the TaskRunner interface.

type Scheduler

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

Scheduler is a type of an event emmiter. Can register a specific task for a specific event when host is starting the server or host is interrupted by CTRL+C/CMD+C. It's being used internally on host supervisor.

func (*Scheduler) CancelOnInterruptTasks

func (s *Scheduler) CancelOnInterruptTasks()

CancelOnInterruptTasks cancels all tasks that are scheduled to run when host is being interrupted by CTRL+C/CMD+C, when the server is alive and online as well.

func (*Scheduler) CancelOnServeTasks

func (s *Scheduler) CancelOnServeTasks()

CancelOnServeTasks cancels all tasks that are scheduled to run when host is starting the server, when the server is alive and online.

func (*Scheduler) CopyTo

func (s *Scheduler) CopyTo(to *Scheduler)

CopyTo copies all tasks from "s" to "to" Scheduler. It doesn't care about anything else.

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(runner TaskRunner) TaskCancelFunc

Schedule schedule/registers a Task, it will be executed/run to when host starts the server or when host is interrupted by CTRL+C/CMD+C based on the TaskRunner type.

See `OnInterrupt` and `ScheduleFunc` too.

func (*Scheduler) ScheduleFunc

func (s *Scheduler) ScheduleFunc(runner func(TaskProcess)) TaskCancelFunc

ScheduleFunc schedule/registers a task function, it will be executed/run to when host starts the server or when host is interrupted by CTRL+C/CMD+C based on the TaskRunner type.

See `OnInterrupt` and `ScheduleFunc` too.

type Supervisor

type Supervisor struct {
	Scheduler
	// contains filtered or unexported fields
}

Supervisor is the wrapper and the manager for a compatible server and it's relative actions, called Tasks.

Interfaces are separated to return relative functionality to them.

func New

func New(srv *http.Server) *Supervisor

New returns a new host supervisor based on a native net/http "srv".

It contains all native net/http's Server methods. Plus you can add tasks on specific events. It has its own flow, which means that you can prevent to return and exit and restore the flow too.

func NewProxy

func NewProxy(hostAddr string, target *url.URL) *Supervisor

NewProxy returns a new host (server supervisor) which redirects all requests to the target. It uses the httputil.NewSingleHostReverseProxy.

Usage: target, _ := url.Parse("https://mydomain.com") proxy := NewProxy("mydomain.com:80", target) proxy.ListenAndServe() // use of proxy.Shutdown to close the proxy server.

func (*Supervisor) DeferFlow

func (su *Supervisor) DeferFlow()

DeferFlow defers the flow of the exeuction, i.e: when server should return error and exit from app, a DeferFlow call inside a Task can wait for a `RestoreFlow` to exit or not exit if host's server is "fixed".

See `RestoreFlow` too.

func (*Supervisor) Done

func (su *Supervisor) Done() <-chan struct{}

Done is being received when in server Shutdown. This can be used to gracefully shutdown connections that have undergone NPN/ALPN protocol upgrade or that have been hijacked. This function should start protocol-specific graceful shutdown, but should not wait for shutdown to complete.

func (*Supervisor) Err

func (su *Supervisor) Err() <-chan error

Err refences to the return value of Server's .Serve, not the server's specific error logger.

func (*Supervisor) ListenAndServe

func (su *Supervisor) ListenAndServe() error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

func (*Supervisor) ListenAndServeAutoTLS

func (su *Supervisor) ListenAndServeAutoTLS() error

ListenAndServeAutoTLS acts identically to ListenAndServe, except that it expects HTTPS connections. server's certificates are auto generated from LETSENCRYPT using the golang/x/net/autocert package.

func (*Supervisor) ListenAndServeTLS

func (su *Supervisor) ListenAndServeTLS(certFile string, keyFile string) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func (*Supervisor) RestoreFlow

func (su *Supervisor) RestoreFlow()

RestoreFlow restores the flow of the execution, if called without a `DeferFlow` call before then it does nothing. See tests to understand how that can be useful on specific cases.

See `DeferFlow` too.

func (*Supervisor) Serve

func (su *Supervisor) Serve(l net.Listener) error

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call su.server.Handler to reply to them.

For HTTP/2 support, server.TLSConfig should be initialized to the provided listener's TLS Config before calling Serve. If server.TLSConfig is non-nil and doesn't include the string "h2" in Config.NextProtos, HTTP/2 support is not enabled.

Serve always returns a non-nil error. After Shutdown or Close, the returned error is http.ErrServerClosed.

func (*Supervisor) Shutdown

func (su *Supervisor) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, then the context's error is returned.

Shutdown does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Shutdown should separately notify such long-lived connections of shutdown and wait for them to close, if desired.

type TaskCancelFunc

type TaskCancelFunc func()

TaskCancelFunc cancels a Task when called.

type TaskHost

type TaskHost struct {

	// Supervisor with access fields when server is running, i.e restrict access to "Schedule"
	// Server that running, is active and open
	// Flow controller
	FlowController
	// contains filtered or unexported fields
}

TaskHost contains all the necessary information about the host supervisor, its server and the exports the whole flow controller of it.

func (TaskHost) Done

func (h TaskHost) Done() <-chan struct{}

Done filled when server was shutdown.

func (TaskHost) Err

func (h TaskHost) Err() <-chan error

Err filled when server received an error.

func (TaskHost) HostURL

func (h TaskHost) HostURL() string

HostURL returns the listening full url (scheme+host) based on the supervisor's server's address.

func (TaskHost) Hostname

func (h TaskHost) Hostname() string

Hostname returns the underline server's hostname.

func (TaskHost) Serve

func (h TaskHost) Serve() error

Serve can (re)run the server with the latest known configuration.

func (TaskHost) Shutdown

func (h TaskHost) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, then the context's error is returned.

Shutdown does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Shutdown should separately notify such long-lived connections of shutdown and wait for them to close, if desired.

type TaskProcess

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

TaskProcess is the context of the Task runner. Contains the host's information and actions and its self cancelation emmiter.

func (TaskProcess) Done

func (p TaskProcess) Done() <-chan struct{}

Done filled when this task is canceled.

func (TaskProcess) Host

func (p TaskProcess) Host() TaskHost

Host returns the TaskHost.

TaskHost contains all the necessary information about the host supervisor, its server and the exports the whole flow controller of it.

type TaskRunner

type TaskRunner interface {
	// Run runs the task based on its TaskProcess which contains
	// all the necessary information and actions to control the host supervisor
	// and its server.
	Run(TaskProcess)
}

A TaskRunner is an independent stream of instructions in a Supervisor. A routine is similar to a sequential program. However, a routine itself is not a program, it can't run on its own, instead it runs within a Supervisor's context.

The real usage of a routine is not about a single sequential thread, but rather using multiple tasks in a single Supervisor. Multiple tasks running at the same time and performing various tasks is referred as Multithreading. A Task is considered to be a lightweight process because it runs within the context of a Supervisor and takes advantage of resources allocated for that Supervisor and its Server.

func ShutdownOnInterruptTask

func ShutdownOnInterruptTask(shutdownTimeout time.Duration) TaskRunner

ShutdownOnInterruptTask returns a supervisor's built'n task which shutdowns the server when InterruptSignalTask fire this task.

type TaskRunnerFunc

type TaskRunnerFunc func(TaskProcess)

TaskRunnerFunc "converts" a func(TaskProcess) to a complete TaskRunner. Its functionality is exactly the same as TaskRunner.

See `TaskRunner` too.

func WriteBannerTask

func WriteBannerTask(w io.Writer, banner string) TaskRunnerFunc

WriteBannerTask is a task which accepts a logger(io.Writer) and a "banner" text to write to following by a generated message based on the host supervisor's server and writes it to the "w". This task runs on serve.

func (TaskRunnerFunc) Run

func (s TaskRunnerFunc) Run(proc TaskProcess)

Run runs the task based on its TaskProcess which contains all the necessary information and actions to control the host supervisor and its server.

Jump to

Keyboard shortcuts

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