Documentation
¶
Overview ¶
Package gocore is a highly secure, optimized, and configurable HTTP backend library for Go applications.
Overview ¶
gocore is designed to be installed as a dependency in a project and used as a full-featured HTTP backend foundation. It handles:
- Routing — trie-based URL matching with path parameters and wildcards.
- Middleware — composable, ordered middleware chains.
- Security — JWT authentication, security headers, CORS, rate limiting.
- Observability — structured access logging, panic recovery.
- Lifecycle — graceful shutdown on SIGINT / SIGTERM.
All subsystems are individually configurable via the config package and can be replaced or extended independently.
Quick start ¶
package main
import (
"os"
"github.com/orislabsdev/gocore"
"github.com/orislabsdev/gocore/config"
"github.com/orislabsdev/gocore/handler"
"github.com/orislabsdev/gocore/middleware"
)
func main() {
cfg := config.Default()
cfg.Server.Port = 8080
cfg.JWT.Secret = os.Getenv("JWT_SECRET")
app := gocore.NewWithConfig(cfg)
// Global middleware (applied to every request)
app.Use(
middleware.RequestID(),
middleware.DefaultRecovery(app.Logger()),
middleware.DefaultLogger(app.Logger()),
middleware.Security(cfg.Security),
middleware.CORS(cfg.CORS),
middleware.RateLimit(cfg.RateLimit, app.Done()),
)
// Public route — no JWT required
app.GET("/health", healthHandler).Public()
// Private route group — JWT required
api := app.Group("/api/v1")
api.Use(middleware.Auth(middleware.AuthConfig{Manager: app.JWTManager()}))
api.GET("/profile", getProfile)
app.Run()
}
Index ¶
- type Core
- func (c *Core) Config() *config.Config
- func (c *Core) DELETE(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) Done() <-chan struct{}
- func (c *Core) GET(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) Group(prefix string) *router.Group
- func (c *Core) HEAD(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) Handler() http.Handler
- func (c *Core) JWTManager() *auth.Manager
- func (c *Core) Logger() *logger.Logger
- func (c *Core) OPTIONS(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) PATCH(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) POST(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) PUT(path string, h handler.HandlerFunc) *router.Route
- func (c *Core) Router() *router.Router
- func (c *Core) Run() error
- func (c *Core) Use(mw ...handler.MiddlewareFunc)
- func (c *Core) UseDefaults()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Core ¶
type Core struct {
// contains filtered or unexported fields
}
Core is the main application object. It wires together configuration, routing, middleware, authentication, and the HTTP server.
Create one Core per application (it is not designed to be shared across multiple HTTP listeners).
func New ¶
func New() *Core
New creates a Core with production-safe defaults. Call NewWithConfig to override specific settings.
func NewWithConfig ¶
NewWithConfig creates a Core with the provided configuration. Panics if the JWT secret is configured but invalid.
func (*Core) Config ¶
Config returns the root configuration. Useful when building custom middleware that reads from the same configuration source.
func (*Core) Done ¶
func (c *Core) Done() <-chan struct{}
Done returns a channel that is closed when the server begins shutting down. Pass this to middleware that run background goroutines (e.g., RateLimit).
func (*Core) GET ¶
GET registers a GET handler. Returns a *Route for fluent chaining (.Public(), .Name(), .Use()).
func (*Core) Group ¶
Group creates a route group with a shared path prefix. Use groups to scope middleware (e.g., JWT auth) to a sub-tree of routes.
api := app.Group("/api/v1")
api.Use(middleware.Auth(...))
api.GET("/users", listUsers)
func (*Core) Handler ¶
Handler returns the Core's router as an http.Handler. Use this when embedding gocore into an existing net/http setup instead of calling Run.
http.ListenAndServe(":8080", app.Handler())
func (*Core) JWTManager ¶
JWTManager returns the auth.Manager for issuing and validating tokens. Returns nil if no JWT secret was configured.
func (*Core) Logger ¶
Logger returns the application logger. Use it in your own handlers and middleware for consistent structured output.
func (*Core) Run ¶
Run starts the HTTP server and blocks until the server receives a shutdown signal (SIGINT/SIGTERM) or encounters an unrecoverable error.
Run handles the full lifecycle:
- Starts the HTTP (or HTTPS) listener.
- Waits for an OS signal.
- Closes the done channel (stopping background goroutines like RateLimit).
- Drains active connections (up to ShutdownTimeout).
- Returns nil on clean exit, or the error that caused the failure.
func (*Core) Use ¶
func (c *Core) Use(mw ...handler.MiddlewareFunc)
Use appends one or more global middleware to the application. Global middleware runs on every request before route-specific middleware. Must be called before Run.
func (*Core) UseDefaults ¶
func (c *Core) UseDefaults()
UseDefaults installs the recommended set of global middleware in the standard order:
- RequestID — ensure every request has a unique ID.
- Recovery — catch panics and return 500.
- Logger — emit a structured access-log entry.
- Security — set HTTP security headers.
- CORS — enforce the CORS policy.
- RateLimit — enforce per-client rate limits.
Authentication (JWT) is intentionally omitted from the default set because it should be applied to specific route groups, not globally. Register it on a group via group.Use(middleware.Auth(...)).
Call UseDefaults before registering routes and before calling Run.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package auth provides JSON Web Token (JWT) generation and validation utilities used by the gocore authentication middleware.
|
Package auth provides JSON Web Token (JWT) generation and validation utilities used by the gocore authentication middleware. |
|
Package builtin provides ready-made handlers for common operational endpoints that every backend service should expose.
|
Package builtin provides ready-made handlers for common operational endpoints that every backend service should expose. |
|
Package config provides all configuration structures for the gocore library.
|
Package config provides all configuration structures for the gocore library. |
|
Command example demonstrates how to use the gocore library as a backend foundation.
|
Command example demonstrates how to use the gocore library as a backend foundation. |
|
Package handler provides the core types that route handlers interact with.
|
Package handler provides the core types that route handlers interact with. |
|
Package logger provides a lightweight, structured, leveled logger for the gocore library and for application code.
|
Package logger provides a lightweight, structured, leveled logger for the gocore library and for application code. |
|
Package middleware provides production-ready HTTP middleware for the gocore library.
|
Package middleware provides production-ready HTTP middleware for the gocore library. |
|
Package router provides a high-performance, trie-based HTTP router for the gocore library.
|
Package router provides a high-performance, trie-based HTTP router for the gocore library. |
|
Package server provides the HTTP/HTTPS server for the gocore library.
|
Package server provides the HTTP/HTTPS server for the gocore library. |
|
Package validate provides lightweight, zero-dependency request validation utilities for the gocore library.
|
Package validate provides lightweight, zero-dependency request validation utilities for the gocore library. |