Documentation
¶
Index ¶
- type ConditionalMiddleware
- type Context
- type Group
- func (g *Group) DELETE(path string, handler HandlerFunc)
- func (g *Group) GET(path string, handler HandlerFunc)
- func (g *Group) Handle(method, path string, handler HandlerFunc)
- func (g *Group) PATCH(path string, handler HandlerFunc)
- func (g *Group) POST(path string, handler HandlerFunc)
- func (g *Group) PUT(path string, handler HandlerFunc)
- func (g *Group) Use(mw middleware.Middleware)
- type HandlerFunc
- type Middleware
- type Response
- type Server
- func (s *Server) DELETE(path string, handler server.HandlerFunc)
- func (s *Server) GET(path string, handler server.HandlerFunc)
- func (s *Server) Group(prefix string) *Group
- func (s *Server) Handle(method, path string, handler server.HandlerFunc)
- func (s *Server) PATCH(path string, handler server.HandlerFunc)
- func (s *Server) POST(path string, handler server.HandlerFunc)
- func (s *Server) PUT(path string, handler server.HandlerFunc)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Start(addr string)
- func (s *Server) StartAutoTLS(domain string)
- func (s *Server) StartAutoTLSWithStarter(domain string, starter TLSStarter)
- func (s *Server) StartTLS(addr, certFile, keyFile string)
- func (s *Server) Use(mw middleware.Middleware)
- func (s *Server) UseIf(pattern string, mw middleware.Middleware)
- type TLSStarter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConditionalMiddleware ¶
type ConditionalMiddleware = middleware.ConditionalMiddleware
ConditionalMiddleware is an alias to middleware.ConditionalMiddleware, which pairs a pattern (e.g., "/api/*") with a Middleware function.
type Context ¶
Context is an alias to server.Context, which wraps the request and response writer and provides convenience methods (params, body parsing, etc.).
type Group ¶
type Group struct {
// Prefix is the base path for this group (e.g., "/api/v1").
Prefix string
// Server is a reference back to the parent server, allowing
// groups to register routes directly into the main router.
Server *Server
// Middlewares is a list of middleware that will be applied to
// every route registered within this group, in addition to any
// global or conditional middleware from the Server.
Middlewares []middleware.Middleware
}
Group represents a collection of routes that share a common path prefix and middleware stack. Useful for organizing related endpoints like `/api/v1/*`.
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, handler HandlerFunc)
func (*Group) GET ¶
func (g *Group) GET(path string, handler HandlerFunc)
Convenience methods for common HTTP methods for group routes.
func (*Group) Handle ¶
func (g *Group) Handle(method, path string, handler HandlerFunc)
Handle registers a route for the group with a specific HTTP method and path. It automatically prepends the group's prefix to the path and applies the group's middleware stack in reverse order for correct execution.
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, handler HandlerFunc)
func (*Group) POST ¶
func (g *Group) POST(path string, handler HandlerFunc)
func (*Group) PUT ¶
func (g *Group) PUT(path string, handler HandlerFunc)
func (*Group) Use ¶
func (g *Group) Use(mw middleware.Middleware)
Use registers a middleware for this specific group. These middlewares are applied only to routes within the group, in addition to any global middleware from the parent server.
type HandlerFunc ¶
type HandlerFunc = server.HandlerFunc
HandlerFunc is an alias to server.HandlerFunc, the function signature that route handlers must implement. It takes a *Context and returns a *Response.
type Middleware ¶
type Middleware = middleware.Middleware
Middleware is an alias to middleware.Middleware, representing a function that wraps and modifies a HandlerFunc, similar to how middleware works in frameworks like Express or Fiber.
type Response ¶
Response is an alias to server.Response, the unified return type from every handler function. Encoded as JSON and written to the client.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the main entry point for the OneStrike framework. It holds the router, global middlewares, and any conditional middlewares that should be applied based on route patterns.
func New ¶
func New() *Server
New creates a new OneStrike Server instance with an empty router and middleware stack.
func (*Server) GET ¶
func (s *Server) GET(path string, handler server.HandlerFunc)
Convenience methods for each HTTP method.
func (*Server) Group ¶
Group represents a collection of routes sharing a common prefix and middleware stack. Useful for organizing related endpoints. Example: v1 := app.Group("/api/v1")
func (*Server) Handle ¶
func (s *Server) Handle(method, path string, handler server.HandlerFunc)
Handle registers a route with a specific HTTP method and path. Global middleware is automatically applied in reverse order (so execution order is correct).
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler, so OneStrike Server can be passed directly to http.ListenAndServe. It finds the route, applies conditional middleware, executes the handler, and writes the Response as JSON.
func (*Server) Start ¶
Start runs the HTTP server on the specified address. It logs the startup and will terminate the program if ListenAndServe returns an error.
func (*Server) StartAutoTLS ¶
StartAutoTLS starts the server with automatic TLS certificate management using Let's Encrypt. This method automatically obtains and renews TLS certificates for the specified domain using the ACME protocol. The server will bind to port 443.
Parameters:
- domain: The domain name for which to obtain certificates (e.g., "example.com")
The method sets up:
- Automatic certificate cache in a local "certs" directory
- Automatic acceptance of Let's Encrypt Terms of Service
- Host whitelist policy for the specified domain
- TLS configuration with automatic certificate retrieval
Requirements:
- The server must be accessible from the internet on port 443
- The domain must point to the server's IP address
- Port 80 should also be available for ACME challenges (handled automatically by autocert)
This method will call log.Fatal if the server fails to start, terminating the program. Use this for production deployments where server startup failure should halt the application.
Example:
server := &Server{}
server.StartAutoTLS("example.com") // Will serve HTTPS on port 443
func (*Server) StartAutoTLSWithStarter ¶
func (s *Server) StartAutoTLSWithStarter(domain string, starter TLSStarter)
StartAutoTLSWithStarter starts the server with automatic TLS certificate management using a custom TLS starter. This method provides the same automatic certificate functionality as StartAutoTLS but allows dependency injection of the server startup mechanism, making it testable.
Parameters:
- domain: The domain name for which to obtain certificates (e.g., "example.com")
- starter: An implementation of TLSStarter interface that handles server startup
This method is primarily intended for testing purposes where you need to mock the server startup behavior. For production use, prefer StartAutoTLS which uses the server's own startup mechanism.
The method configures:
- autocert.Manager with local certificate caching
- Automatic TOS acceptance for Let's Encrypt
- Host policy restricting certificates to the specified domain
- HTTP server bound to port 443 with TLS configuration
Example:
server := &Server{}
mockStarter := &MockTLSStarter{...}
server.StartAutoTLSWithStarter("example.com", mockStarter)
func (*Server) StartTLS ¶
StartTLS starts the server with TLS using the provided certificate and key files. The server will bind to the specified address and serve HTTPS traffic.
Parameters:
- addr: The address to bind to (e.g., ":443", "localhost:8443")
- certFile: Path to the TLS certificate file
- keyFile: Path to the TLS private key file
This method will call log.Fatal if the server fails to start, terminating the program. Use this for production deployments where server startup failure should halt the application.
Example:
server := &Server{}
server.StartTLS(":443", "/path/to/cert.pem", "/path/to/key.pem")
func (*Server) Use ¶
func (s *Server) Use(mw middleware.Middleware)
Use registers a global middleware that will run on every request.
func (*Server) UseIf ¶
func (s *Server) UseIf(pattern string, mw middleware.Middleware)
UseIf registers a conditional middleware that only runs if the request path matches the given pattern. Patterns can include a wildcard '*' at the end. Example: UseIf("/api/v1/*", AuthMiddleware())
type TLSStarter ¶
type TLSStarter interface {
// contains filtered or unexported methods
}
First, define an interface for the server starter