celeritas

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

README

Celeritas

Go Reference

License

This project is licensed under the terms of the Apache License 2.0.

wakatime

Documentation

Index

Constants

View Source
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

func (c *Celeritas) BuildDSN() string

BuildDSN builds the datasource name for our database, and returns it as a string.

func (*Celeritas) CreateDirIfNotExist added in v1.3.0

func (c *Celeritas) CreateDirIfNotExist(path string) error

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

func (c *Celeritas) CreateFileIfNotExists(path string) error

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) Init added in v1.3.0

func (c *Celeritas) Init(p initPaths) error

Init takes a initPaths and returns an error

func (*Celeritas) ListenAndServe added in v1.3.0

func (c *Celeritas) ListenAndServe() error

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 (c *Celeritas) MigrateDownAll(dsn string) error

func (*Celeritas) MigrateForce added in v1.4.0

func (c *Celeritas) MigrateForce(dsn string) error

func (*Celeritas) MigrateUp added in v1.4.0

func (c *Celeritas) MigrateUp(dsn string) error

func (*Celeritas) New added in v1.3.0

func (c *Celeritas) New(rootPath string) error

New returns a new Celeritas application

func (*Celeritas) OpenDB added in v1.4.0

func (c *Celeritas) OpenDB(dbType, dsn string) (*sql.DB, error)

func (*Celeritas) RandomString added in v1.4.0

func (c *Celeritas) RandomString(length int) string

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 (c *Celeritas) SessionLoad(next http.Handler) http.Handler

func (*Celeritas) StartLoggers added in v1.3.0

func (c *Celeritas) StartLoggers() (*log.Logger, *log.Logger)

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) Steps added in v1.4.0

func (c *Celeritas) Steps(n int, dsn string) error

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)

type Database added in v1.4.0

type Database struct {
	DataType string
	Pool     *sql.DB
}

Database represents the database connection

Directories

Path Synopsis
cmd
cli command
Package inital contains initial code for the celeritas project
Package inital contains initial code for the celeritas project

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL