Documentation
¶
Overview ¶
Package http provides functionalities to configure, run, and manage an HTTP server, including middleware support, API route handling, background workers, and graceful shutdown.
Index ¶
- Constants
- func StartHttpServer(serverconfig *ServerConfig)
- type ApiMinifyConfig
- type CorsOptions
- type HTTPServer
- func (s *HTTPServer) Addr() string
- func (s *HTTPServer) ListenAndServe() error
- func (s *HTTPServer) Router() router.Router
- func (s *HTTPServer) Server() *http.Server
- func (s *HTTPServer) SessionManager() *session.Manager
- func (s *HTTPServer) Shutdown(ctx context.Context) error
- func (s *HTTPServer) Start() error
- type HttpRoute
- type ServerConfig
Constants ¶
const ( DefaultServerPort int64 = 8080 DefaultReadTimeout time.Duration = 5 * time.Second DefaultReadHeaderTimeout time.Duration = 2 * time.Second DefaultWriteTimeout time.Duration = 10 * time.Second DefaultIdleTimeout time.Duration = 120 * time.Second DefaultShutdownTimeout time.Duration = 10 * time.Second DefaultMaxHeaderBytes int = 1 << 20 )
Variables ¶
This section is empty.
Functions ¶
func StartHttpServer ¶
func StartHttpServer(serverconfig *ServerConfig)
StartHttpServer initializes and starts the HTTP server.
It performs the following steps: - Validates the Go runtime environment. - Sets up the router with middlewares and HTTP routes. - Configures API key minification, if enabled. - Launches background workers, if any. - Starts the HTTP server and handles graceful shutdown.
Parameters:
- serverconfig: A pointer to ServerConfig containing server settings and route configurations.
Types ¶
type ApiMinifyConfig ¶
type CorsOptions ¶
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer is a configured Photon HTTP server with explicit lifecycle control.
func NewHTTPServer ¶
func NewHTTPServer(serverconfig *ServerConfig) (*HTTPServer, error)
NewHTTPServer normalizes configuration, builds the router, applies middleware, registers routes, and returns a server handle that the caller can start or stop.
func (*HTTPServer) Addr ¶
func (s *HTTPServer) Addr() string
Addr returns the configured listen address.
func (*HTTPServer) ListenAndServe ¶
func (s *HTTPServer) ListenAndServe() error
ListenAndServe starts serving without launching Photon background workers. This is useful for tests and applications that manage workers independently.
func (*HTTPServer) Router ¶
func (s *HTTPServer) Router() router.Router
Router returns the configured Photon router.
func (*HTTPServer) Server ¶
func (s *HTTPServer) Server() *http.Server
Server returns the underlying net/http server for advanced integrations.
func (*HTTPServer) SessionManager ¶
func (s *HTTPServer) SessionManager() *session.Manager
SessionManager returns the session manager attached to the HTTP middleware.
func (*HTTPServer) Shutdown ¶
func (s *HTTPServer) Shutdown(ctx context.Context) error
Shutdown gracefully stops the underlying net/http server.
func (*HTTPServer) Start ¶
func (s *HTTPServer) Start() error
Start launches background workers configured on the server and then starts serving. It blocks until the HTTP listener returns.
type HttpRoute ¶
type HttpRoute struct {
UrlRoute string
RequestMethod string
HttpHandler http.HandlerFunc
Middlewares []middleware.MiddlewareFn
EnableMinify bool
}
type ServerConfig ¶
type ServerConfig struct {
ServerPort int64
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
MaxHeaderBytes int
TLSConfig *tls.Config
TLSCertFile string
TLSKeyFile string
BaseContext func(net.Listener) context.Context
ConnContext func(context.Context, net.Conn) context.Context
ErrorLog *stdlog.Logger
SessionConfig *session.Config
SessionManager *session.Manager
RouteProvider string
HttpRoutes []*HttpRoute
Middlewares []func(http.Handler) http.Handler
CorsOptions *CorsOptions
ApiMinifyConfig *ApiMinifyConfig
WorkerSleepTimeout time.Duration
BGWorkers []*workers.WorkerConfig
AccessLogger logger.Logger
ErrorLogger logger.Logger
ServerLogger logger.Logger
ShutdownHook func()
}
func (*ServerConfig) Normalize ¶
func (s *ServerConfig) Normalize() error
func (*ServerConfig) Validate ¶
func (s *ServerConfig) Validate() error