Documentation
¶
Index ¶
- Constants
- type Celeritas
- func (c *Celeritas) BuildDSN() string
- func (c *Celeritas) CreateDirIfNotExist(path string) error
- func (c *Celeritas) CreateFileIfNotExists(path string) error
- func (c *Celeritas) Init(p initPaths) error
- func (c *Celeritas) ListenAndServe() error
- func (c *Celeritas) MigrateDownAll(dsn string) error
- func (c *Celeritas) MigrateForce(dsn string) error
- func (c *Celeritas) MigrateUp(dsn string) error
- func (c *Celeritas) New(rootPath string) error
- func (c *Celeritas) OpenDB(dbType, dsn string) (*sql.DB, error)
- func (c *Celeritas) RandomString(length int) string
- func (c *Celeritas) SessionLoad(next http.Handler) http.Handler
- func (c *Celeritas) StartLoggers() (*log.Logger, *log.Logger)
- func (c *Celeritas) Steps(n int, dsn string) error
- func (c *Celeritas) WriteJSON(w http.ResponseWriter, status int, data any, headers ...http.Header) error
- type Database
Constants ¶
const (
Version = "1.0.0"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Celeritas ¶ added in v1.3.0
type Celeritas struct {
AppName string // Application name used in logging and identification
Debug bool // Debug mode flag for detailed logging and error handling
Version string // Application version for deployment tracking
ErrorLog *log.Logger // Structured error logging
InfoLog *log.Logger // Structured information logging
RootPath string // Base directory for application files and folders
Routes *chi.Mux // HTTP router for handling web requests
Render *render.Render // Rendering engine
Session *scs.SessionManager // Session manager
DB Database // Database connection
JetViews *jet.Set // Jet template engine
// contains filtered or unexported fields
}
Celeritas represents the core application framework, providing essential services for web applications including routing, logging, and configuration management. It coordinates all the main components needed to run a web server.
The zero value is not usable. Use New to create a new Celeritas instance.
A Celeritas instance manages:
- Application identification and versioning
- Debug mode configuration
- Structured logging (info and error levels)
- File system organization via RootPath
- HTTP routing with Chi router
- Server configuration (ports, rendering options)
- Jet template engine for rendering views
- Go template engine for rendering views
Celeritas is safe for use by a single goroutine at a time.
func (*Celeritas) BuildDSN ¶ added in v1.4.0
BuildDSN builds the datasource name for our database, and returns it as a string.
func (*Celeritas) CreateDirIfNotExist ¶ added in v1.3.0
CreateDirIfNotExist takes a path string and returns an error. It creates the directory if it does not exist.
func (*Celeritas) CreateFileIfNotExists ¶ added in v1.3.0
CreateFileIfNotExists creates a new empty file at the specified path if one doesn't exist. If the file already exists, it does nothing and returns nil. The created file is automatically closed after creation. It returns an error if the file creation fails.
func (*Celeritas) ListenAndServe ¶ added in v1.3.0
ListenAndServe starts and runs the HTTP server until it encounters an error. It configures the server with:
- Idle timeout of 30 seconds
- Read timeout of 30 seconds
- Write timeout of 600 seconds
The server listens on the port specified in the application config. If the server encounters an error during startup or operation, it logs the error and terminates the application.
func (*Celeritas) MigrateDownAll ¶ added in v1.4.0
func (*Celeritas) MigrateForce ¶ added in v1.4.0
func (*Celeritas) RandomString ¶ added in v1.4.0
RandomString generates a random string of a given length using a cryptographically secure random number generator. It returns a string of random characters from the allowedChars constant.
func (*Celeritas) SessionLoad ¶ added in v1.4.0
func (*Celeritas) StartLoggers ¶ added in v1.3.0
StartLoggers initializes the application's logging system with two loggers: an InfoLog for general information (in green) and an ErrorLog for error messages (in red). Both loggers write to standard output with different prefixes, formats, and colors. It returns two loggers for info and error logging respectively.
func (*Celeritas) WriteJSON ¶ added in v1.4.0
func (c *Celeritas) WriteJSON( w http.ResponseWriter, status int, data any, headers ...http.Header, ) error
WriteJSON provides a consistent way to send JSON responses across the application, handling common response needs like custom headers and content-type setting. It abstracts away the repetitive tasks of JSON marshaling and header management that would otherwise be scattered throughout handler code.
The method uses MarshalIndent instead of Marshal to generate formatted JSON that's easier to read during development and debugging, accepting the minor performance trade-off for improved developer experience.
Headers are made optional through variadic parameters to support both simple responses and cases requiring custom header inclusion without complicating the common case.
Example usage with custom headers:
headers := http.Header{
"X-Custom-Header": []string{"value"},
}
err := c.WriteJSON(w, http.StatusOK, data, headers)
Example simple usage:
err := c.WriteJSON(w, http.StatusOK, data)