Documentation
¶
Overview ¶
Copyright 2025 The Joe-cli Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Copyright 2025 The Joe-cli Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. Package httpserver can host an HTTP server in the CLI app.
Copyright 2025 The Joe-cli Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Copyright 2023 The Joe-cli Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Index ¶
- Variables
- func ContextValue(s *Server) cli.Action
- func ExpandRequest(r *http.Request, ww wrapResponseWriter) expr.Expander
- func FlagsAndArgs() cli.Action
- func Handle(path string, h http.Handler) cli.Action
- func ListHandlers() cli.Action
- func NewHeaderMiddleware(name, value string) func(http.Handler) http.Handler
- func NewPingHandler() http.Handler
- func NewRequestLogger(format string, out io.Writer, next http.Handler) http.Handler
- func OpenInBrowser(c context.Context)
- func ReportListening(c context.Context)
- func RunServer(actionopt ...cli.Action) cli.Action
- func SetAccessLog(v ...string) cli.Action
- func SetAddr(s ...string) cli.Action
- func SetFileServerHandler(v ...httpclient.VirtualPath) cli.Action
- func SetHandler(v ...httpclient.VirtualPath) cli.Action
- func SetHostname(s ...string) cli.Action
- func SetIdleTimeout(d ...time.Duration) cli.Action
- func SetMaxHeaderBytes(v ...int) cli.Action
- func SetNoAccessLog(v ...bool) cli.Action
- func SetNoDirectoryListings() cli.Action
- func SetOpenInBrowser() cli.Action
- func SetPort(s ...int) cli.Action
- func SetReadHeaderTimeout(d ...time.Duration) cli.Action
- func SetReadTimeout(d ...time.Duration) cli.Action
- func SetServer(v ...string) cli.Action
- func SetShutdownTimeout(d ...time.Duration) cli.Action
- func SetStaticDirectory(f ...*cli.File) cli.Action
- func SetTLSCertFile(v ...*cli.File) cli.Action
- func SetTLSKeyFile(v ...*cli.File) cli.Action
- func SetWriteTimeout(d ...time.Duration) cli.Action
- func SourceAnnotation() (string, string)
- type HandlerSpec
- type MiddlewareFunc
- type Option
- func AddReadyFunc(ready ReadyFunc) Option
- func AddShutdownFunc(shutdown ReadyFunc) Option
- func WithAddr(addr string) Option
- func WithHandler(handler http.Handler) Option
- func WithHandlerFactory(f func(*Server) (http.Handler, error)) Option
- func WithHostname(v string) Option
- func WithMiddleware(m MiddlewareFunc) Option
- func WithPort(v int) Option
- func WithReadyFunc(ready ReadyFunc) Option
- func WithShutdownFunc(shutdown ReadyFunc) Option
- func WithShutdownTimeout(d time.Duration) Option
- type ReadyFunc
- type Server
- func (s *Server) AddMiddleware(m MiddlewareFunc)
- func (s *Server) Execute(c context.Context) error
- func (s *Server) Handle(path string, h http.Handler) error
- func (s *Server) HideDirectoryListing() bool
- func (s *Server) ListenAndServe() error
- func (s *Server) OpenInBrowser(path ...string) error
- func (s *Server) ReportListening() error
- func (s *Server) SetAccessLog(v string) error
- func (s *Server) SetAddr(addr string) error
- func (s *Server) SetHostname(name string) error
- func (s *Server) SetIdleTimeout(v time.Duration) error
- func (s *Server) SetMaxHeaderBytes(v int) error
- func (s *Server) SetNoAccessLog(v bool) error
- func (s *Server) SetNoDirectoryListings(v bool) error
- func (s *Server) SetPort(port int) error
- func (s *Server) SetReadHeaderTimeout(v time.Duration) error
- func (s *Server) SetReadTimeout(v time.Duration) error
- func (s *Server) SetServer(name string) error
- func (s *Server) SetShutdownTimeout(d time.Duration) error
- func (s *Server) SetStaticDirectory(path string) error
- func (s *Server) SetTLSCertFile(v string) error
- func (s *Server) SetTLSKeyFile(v string) error
- func (s *Server) SetWriteTimeout(v time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var ( DefaultReadyFunc = ComposeReadyFuncs( ReportListening, ) // ErrNotListening is reported when the server is not listening ErrNotListening = errors.New("server is not listening") )
DefaultReadyFunc provides the default behavior when the server starts
var HandlerRegistry = &provider.Registry{ Name: "handlers", Providers: provider.Details{ "ping": { Factory: provider.Factory(newPingHandlerWithOpts), }, "file": { Factory: provider.Factory(newFileServerHandlerWithOpts), }, "redirect": { Factory: provider.Factory(newRedirectServerHandlerWithOpts), }, }, }
HandlerRegistry provides the default handler registry.
Functions ¶
func ContextValue ¶
func ContextValue(s *Server) cli.Action
func FlagsAndArgs ¶
func FlagsAndArgs() cli.Action
FlagsAndArgs adds numerous flags that can be used to configure the server in the context. The default flags list contains all of the flag actions in this package except for SetHandler and its variants.
func ListHandlers ¶
func ListHandlers() cli.Action
ListHandlers provides an action which lists the handlers for the handler flag. When used in the Uses pipeline, also sets reasonable defaults for a flag. This handler is not included in FlagsAndArgs
func NewHeaderMiddleware ¶
NewHeaderMiddleware provides handler middleware which simply adds the given header
func NewPingHandler ¶
NewPingHandler provides a handler which simply replies with a message
func NewRequestLogger ¶
NewRequestLogger provides handler middleware to write to access log
func OpenInBrowser ¶
OpenInBrowser is a function to open the server in the browser. This function is passed as a value to WithReadyFunc
func ReportListening ¶
ReportListening is a ready func that prints a message to stderr that the server is listening
func RunServer ¶
func RunServer(actionopt ...cli.Action) cli.Action
RunServer locates the server in context and runs it until interrupt signal is detected. Optional actions run just before the server starts up, typically used to provide context-bound modifications to the server just in time.
func SetAccessLog ¶
func SetAccessLog(v ...string) cli.Action
func SetAddr ¶
func SetAddr(s ...string) cli.Action
SetAddr sets the server address, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetFileServerHandler ¶
func SetFileServerHandler(v ...httpclient.VirtualPath) cli.Action
SetFileServerHandler adds the specified file server handler to the mux. This can be called multiple times. This handler is not included in FlagsAndArgs
func SetHandler ¶
func SetHandler(v ...httpclient.VirtualPath) cli.Action
SetHandler adds the specified handler to the mux. This can be called multiple times. SetHandler only works if a Registry named "handlers" is present in the context to convert the handler spec to the correct implementation. Consider adding HandlerRegistry to the Uses pipeline.. This handler is not included in FlagsAndArgs
func SetHostname ¶
func SetHostname(s ...string) cli.Action
SetHostname sets the server address, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetIdleTimeout ¶
SetIdleTimeout sets the maximum amount of time to wait for the next request when keep-alives are enabled, which either uses the specified value or reads from the corresponding flag/arg to get the value to set. If zero is set, then the value of read time is used, unless both are zero in which case there is no timeout.
func SetMaxHeaderBytes ¶
func SetMaxHeaderBytes(v ...int) cli.Action
SetMaxHeaderBytes sets the maximum header size in bytes
func SetNoAccessLog ¶
func SetNoAccessLog(v ...bool) cli.Action
func SetNoDirectoryListings ¶
func SetNoDirectoryListings() cli.Action
SetNoDirectoryListings causes directories not to be listed
func SetOpenInBrowser ¶
func SetOpenInBrowser() cli.Action
SetOpenInBrowser causes the default Web browser to open when the server is ready
func SetPort ¶
func SetPort(s ...int) cli.Action
SetPort sets the server port, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetReadHeaderTimeout ¶
SetReadHeaderTimeout sets the amount of time allowed to read request headers, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetReadTimeout ¶
SetReadTimeout sets the maximum duration for reading the entire request, including the body, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetShutdownTimeout ¶
SetShutdownTimeout sets the maximum duration to wait for shutting down the server.
func SetStaticDirectory ¶
func SetStaticDirectory(f ...*cli.File) cli.Action
SetStaticDirectory sets the static directory to host
func SetTLSCertFile ¶
func SetTLSCertFile(v ...*cli.File) cli.Action
func SetTLSKeyFile ¶
func SetTLSKeyFile(v ...*cli.File) cli.Action
func SetWriteTimeout ¶
SetWriteTimeout sets the maximum duration before timing out writes of the response, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SourceAnnotation ¶
SourceAnnotation gets the name and value of the annotation added to the Data of all flags that are initialized from this package
Types ¶
type HandlerSpec ¶
type HandlerSpec func(context.Context, httpclient.VirtualPath) (http.Handler, error)
HandlerSpec creates a handler from a virtual path. The virtual path defines how the handler works. Typically, the physical path identifies a useful feature or the location of a file, and the options may be used for any purpose of customization.
func FileServerHandlerSpec ¶
func FileServerHandlerSpec() HandlerSpec
FileServerHandlerSpec creates a file server. The physical path in the virtual path specifies the base directory for the file server. An option named hide_directory_listing controls whether the directory listing response is served. The handler also consults the server for whether directory listings can be served.
func RegistryHandlerSpec ¶
func RegistryHandlerSpec(name string) HandlerSpec
RegistryHandlerSpec creates a handler by looking it up as a provider in the registry that is named. The physical path in the virtual path specifies the name of the provider which is used. The virtual path's options are propagated to the registry factory function.
type MiddlewareFunc ¶
MiddlewareFunc defines a function that creates a middleware wrapper around another handler
type Option ¶
type Option func(*Server)
Option is an option to configure the server Option can be used as an Action, typically within the Uses or Before pipeline.
func AddReadyFunc ¶
AddReadyFunc appends a callback for when the server is ready
func AddShutdownFunc ¶
AddShutdownFunc adds a callback for when the server is shutting down
func WithHandler ¶
WithHandler sets the handler which will run on the server
func WithHandlerFactory ¶
WithHandlerFactory sets how to create the handler which will run on the server
func WithMiddleware ¶
func WithMiddleware(m MiddlewareFunc) Option
WithMiddleware adds handler middleware
func WithReadyFunc ¶
WithReadyFunc sets a callback for when the server is listening
func WithShutdownFunc ¶
WithShutdownFunc sets a callback for when the server is shutting down
func WithShutdownTimeout ¶
WithShutdownTimeout sets the amount of time to allow the server to shutdown
type ReadyFunc ¶
ReadyFunc provides a function for when the server has started or stopped
func ComposeReadyFuncs ¶
ComposeReadyFunc provides a ReadyFunc that combines a sequence
type Server ¶
type Server struct {
*http.Server
TLSCertFile string
TLSKeyFile string
// ShutdownTimeout specifies how long to wait for the server to shutdown
// when a signal is received
ShutdownTimeout time.Duration
// contains filtered or unexported fields
}
Server provides an HTTP server (indeed http.Server is embedded) that can be initialized and hosted within a CLI app. The server is used within the Uses pipeline where it registers itself as a context service. The action RunServer is used to actually run the server.
The simplest action to use is RunServer(), which runs the server:
&cli.App{
Name: "goserv",
Uses: &httpserver.New(httpserver.WithHandler(...)),
Action: httpserver.RunServer(),
}
This simple app has numerous flags to configure connection handling and to dynamically construct the server's router. Many dynamic server routing actions depend upon the server having a handler which is also a mux that contains a method Handle(string, http.Handler) to register additional handlers. (This is the same API provided by the built-in Go mux, http.ServeMux). There are other APIs provided by convention, documented in their respective contexts.
The cmd/weave package provides weave, which is a command line utility that hosts a server for files and some built-in handlers, which is similar to what the DefaultServer() does.
If you only want to add the Server to the context (typically in advanced scenarios where you are deeply customizing the behavior), you only use the action httpserver.ContextValue() with the server you want to add instead of add the server to the pipeline directly.
func DefaultServer ¶
func DefaultServer() *Server
func FromContext ¶
FromContext obtains the server from the context.
func (*Server) AddMiddleware ¶
func (s *Server) AddMiddleware(m MiddlewareFunc)
AddMiddleware appends additional middleware to the server
func (*Server) HideDirectoryListing ¶
func (*Server) ListenAndServe ¶
func (*Server) OpenInBrowser ¶
OpenInBrowser opens in the browser. The request path can also be specified