LessGo

package
v0.0.0-...-3264219 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package LessGo is a minimalist web framework for building fast, scalable, and lightweight web applications in Go.

LessGo is designed to be simple and easy to use, providing essential features for web development without the overhead of large, complex frameworks. It emphasizes speed, flexibility, and a small footprint, making it ideal for developers who want to build web applications quickly while maintaining full control over their projects.

Features

- **Routing**: LessGo provides a flexible and powerful routing mechanism for handling HTTP requests. - **Middleware**: Support for middleware allows you to add custom functionality to your request pipeline. - **Content Negotiation**: Built-in support for content negotiation, enabling your API to serve different content types like JSON, XML, etc. - **Environment Configuration**: Load environment variables and .env files easily for configuration management. - **Pluggable Architecture**: Extend LessGo with custom plugins and middleware for additional functionality. - **CORS Support**: Configure CORS settings for your API. - **Redis Integration**: Easily integrate Redis for caching, rate limiting, and other use cases. - **Static File Serving**: Serve static files like HTML, CSS, JavaScript, or images. - **Security**: CSRF and XSS protection are built-in to enhance the security of your applications. - **Rate Limiting**: Implement rate limiting to protect your application from abuse.

Usage

Here's an example of how to use the LessGo framework in a basic web server setup:

package main

import (
	"log"
	"time"
	"github.com/yourusername/LessGo/app/src"
	LessGo "github.com/yourusername/LessGo/pkg/lessgo"
)

func main() {
	// Configuration setup
	cfg := LessGo.LoadConfig()
	serverPort := cfg.Get("SERVER_PORT", "8080")
	env := cfg.Get("ENV", "development")
	addr := ":" + serverPort

	// Define CORS options
	corsOptions := LessGo.NewCorsOptions(
		[]string{"*"},
		[]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		[]string{"Content-Type", "Authorization"},
	)

	// Initialize Redis client
	rClient := LessGo.NewRedisClient("localhost:6379")

	// Initialize app with middlewares
	App := LessGo.App(
		LessGo.WithCORS(*corsOptions),
		LessGo.WithJSONParser(LessGo.NewParserOptions(5*1024*1024)), // 5MB limit
		LessGo.WithCookieParser(),
		LessGo.WithCsrf(),
		LessGo.WithXss(),
		LessGo.WithCaching(rClient, 5*time.Minute, true),
		LessGo.WithRedisRateLimiter(rClient, 100, 1*time.Second),
	)

	// Serve static files
	App.ServeStatic("/static/", LessGo.GetFolderPath("uploads"))

	// Register modules and dependencies
	LessGo.RegisterDependencies([]interface{}{src.NewRootService, src.NewRootModule})
	LessGo.RegisterModules(App, []LessGo.IModule{src.NewRootModule(App)})

	// Example route
	App.Get("/ping", func(ctx *LessGo.Context) {
		ctx.Send("pong")
	})

	// Start the server
	log.Printf("Starting server on port %s in %s mode", serverPort, env)
	if err := App.Listen(addr); err != nil {
		log.Fatalf("Server failed: %v", err)
	}
}

For more detailed documentation and examples, please visit the [official LessGo documentation](https://github.com/hokamsingh).

Package Structure

- **LessGo**: The core of the framework, providing the main application structure and utilities. - **LessGo/context**: Handles the request context, providing methods to respond with different content types. - **LessGo/middleware**: Contains built-in middleware functions for request handling. - **LessGo/config**: Manages configuration loading from environment variables and .env files.

Index

Constants

View Source
const Parallel = 0
View Source
const Sequential = 1
View Source
const Version = "v1.0.2"

Version

Variables

This section is empty.

Functions

func ConvertToBytes

func ConvertToBytes(size int64, unit SizeUnit) int64

Convert size to bytes

Example

const (

Bytes     SizeUnit = "bytes"
Kilobytes SizeUnit = "kilobytes"
Megabytes SizeUnit = "megabytes"
Gigabytes SizeUnit = "gigabytes"

)

func DiscoverModules

func DiscoverModules() ([]func() IModule, error)

func GenerateRandomToken

func GenerateRandomToken(len int) (string, error)

func GetFolderPath

func GetFolderPath(folderName string) string

Resolves the path of specified folder

func LoadConfig

func LoadConfig() config.Config

LoadConfig loads the ENV configurations

func NewHttpConfig

func NewHttpConfig(options ...func(*config.HttpConfig)) *config.HttpConfig

NewHttpConfig creates a new HttpConfig instance with optional configuration options. This function accepts a variadic number of option functions that allow customization of the HttpConfig fields. If no options are provided, it will create a configuration with default values for all fields.

Default values: - ReadTimeout: 10 seconds - WriteTimeout: 10 seconds - IdleTimeout: 60 seconds - MaxHeaderSize: 1 MB (1048576 bytes) - TLSCertFile: "" (empty string, no certificate file) - TLSKeyFile: "" (empty string, no key file) - Security.EnableHSTS: false - Security.ContentSecurityPolicy: "" (empty string, no policy) - Session.Store: "memory" (default session store) - Session.Timeout: 3600 seconds (1 hour)

The options functions should be used to set various fields of the HttpConfig, such as timeouts, TLS certificates, and security settings. Each option function takes a pointer to HttpConfig and modifies it accordingly.

Example usage:

cfg := LessGo.NewHttpConfig(
  LessGo.WithReadTimeout(30),
  LessGo.WithWriteTimeout(30),
  LessGo.WithTLSCertFile("/path/to/cert.pem"),
)

Parameters: - options: A variadic number of option functions that modify the HttpConfig instance.

Returns: - *HttpConfig: A pointer to the newly created HttpConfig instance with applied options.

See also: - WithReadTimeout - WithWriteTimeout - WithTLSCertFile

func NewRedisClient

func NewRedisClient(redisAddr string) *redis.Client

func RegisterDependencies

func RegisterDependencies(dependencies []interface{})

func RegisterModules

func RegisterModules(r *router.Router, modules []module.IModule) error

func WithCORS

func WithCORS(options middleware.CORSOptions) router.Option

WithCORS enables CORS middleware with specific options. This option configures the CORS settings for the router.

Example usage:

r := router.NewRouter(router.WithCORS(middleware.CORSOptions{...}))

func WithCaching

func WithCaching(redisClient *redis.Client, ttl time.Duration, cacheControl bool) router.Option

WithCaching is an option function that enables caching for the router using Redis.

This function returns an Option that can be passed to the Router to enable response caching with Redis. Cached responses will be stored in Redis with a specified Time-To-Live (TTL), meaning they will automatically expire after the specified duration.

Parameters:

  • redisAddr (string): The address of the Redis server, e.g., "localhost:6379".
  • ttl (time.Duration): The Time-To-Live for cached responses. Responses will be removed from the cache after this duration.

Returns:

  • Option: An option that applies caching middleware to the router.

Example usage:

router := NewRouter(
    WithCaching("localhost:6379", 5*time.Minute),
)

This will enable caching for the router, storing responses in Redis for 5 minutes.

Note: Ensure that the Redis server is running and accessible at the specified address.

func WithContentSecurityPolicy

func WithContentSecurityPolicy(policy string) func(*HttpConfig)

Wrapper for WithContentSecurityPolicy

func WithCookieParser

func WithCookieParser() router.Option

WithCookieParser enables cookie parsing middleware. This option ensures that cookies are parsed and available in the request context.

Example usage:

r := router.NewRouter(router.WithCookieParser())

func WithCsrf

func WithCsrf() router.Option

WithCsrf is an option function that enables CSRF protection for the router.

This function returns an Option that can be passed to the Router to enable Cross-Site Request Forgery (CSRF) protection using a middleware. The middleware generates and validates CSRF tokens to protect against malicious cross-origin requests, ensuring that requests are coming from legitimate users.

Returns:

  • Option: An option that applies CSRF protection middleware to the router.

Example usage:

router := NewRouter(
    WithCsrf(),
)

This will enable CSRF protection for all routes in the router.

func WithFileUpload

func WithFileUpload(uploadDir string, maxFileSize int64, allowedExts []string) router.Option

WithFileUpload enables file upload middleware with the specified upload directory. This option configures the router to handle file uploads and save them to the given directory.

Example usage:

r := router.NewRouter(router.WithFileUpload("/uploads"))

func WithHSTS

func WithHSTS(enabled bool) func(*HttpConfig)

Wrapper for WithHSTS

func WithIdleTimeout

func WithIdleTimeout(timeout int) func(*HttpConfig)

Wrapper for WithIdleTimeout

func WithInMemoryRateLimiter

func WithInMemoryRateLimiter(NumShards int, Limit int, Interval time.Duration, CleanupInterval time.Duration) router.Option

WithRateLimiter enables rate limiting middleware with the specified limit and interval. This option configures the rate limiter for the router.

Example usage:

r := router.NewRouter(router.WithRateLimiter(100, time.Minute))

func WithJSONParser

func WithJSONParser(options ParserOptions) router.Option

WithJSONParser enables JSON parsing middleware for request bodies. This option ensures that incoming JSON payloads are parsed and available in the request context.

Example usage:

r := router.NewRouter(router.WithJSONParser())

func WithMaxHeaderSize

func WithMaxHeaderSize(size int) func(*HttpConfig)

Wrapper for WithMaxHeaderSize

func WithReadTimeout

func WithReadTimeout(timeout int) func(*config.HttpConfig)

func WithRedisRateLimiter

func WithRedisRateLimiter(client *redis.Client, limit int, interval time.Duration) router.Option

WithRateLimiter enables rate limiting middleware with the specified limit and interval. This option configures the rate limiter for the router.

Example usage:

r := router.NewRouter(router.WithRateLimiter(100, time.Minute))

func WithSessionStore

func WithSessionStore(store string) func(*HttpConfig)

Wrapper for WithSessionStore

func WithSessionTimeout

func WithSessionTimeout(timeout int) func(*HttpConfig)

Wrapper for WithSessionTimeout

func WithTLSCertFile

func WithTLSCertFile(certFile string) func(*HttpConfig)

Wrapper for WithTLSCertFile

func WithTLSKeyFile

func WithTLSKeyFile(keyFile string) func(*HttpConfig)

Wrapper for WithTLSKeyFile

func WithTemplateRendering

func WithTemplateRendering(templateDir string) router.Option

WithTemplateRendering sets up the router to use the TemplateMiddleware for rendering HTML templates. It automatically loads all `.html` files from the specified directory and makes them available for rendering within the application's handlers.

The middleware parses all `.html` files from the provided directory during initialization and injects the parsed templates into the request context, allowing handlers to access and render the templates as needed.

Usage:

router := NewRouter(
    WithTemplateRendering("templates"), // Directory containing all .html files
)

router.HandleFunc("/", yourHandler)

In the handler, you can retrieve and execute a template:

func yourHandler(w http.ResponseWriter, r *http.Request) {
    tmpl := middleware.GetTemplate(r.Context())
    tmpl.ExecuteTemplate(w, "index.html", nil) // Renders the index.html template
}

Parameters:

  • templateDir: The directory containing the `.html` files to be used as templates.

Returns:

  • Option: A function that configures the router to use the template rendering middleware.

func WithWriteTimeout

func WithWriteTimeout(timeout int) func(*HttpConfig)

Wrapper for WithWriteTimeout

func WithXss

func WithXss() router.Option

WithXss is an option function that enables XSS protection for the router.

This function returns an Option that can be passed to the Router to enable Cross-Site Scripting (XSS) protection using a middleware. The middleware helps to sanitize and filter out malicious scripts from user input, thereby preventing XSS attacks.

Returns:

  • Option: An option that applies XSS protection middleware to the router.

Example usage:

router := NewRouter(
    WithXss(),
)

This will enable XSS protection for all routes in the router, ensuring that user input is sanitized and secure.

Types

type BaseController

type BaseController = controller.BaseController

BaseController provides a default implementation of the Controller interface. It can be embedded in other controllers to inherit its default behavior, or overridden with custom implementations.

type BaseMiddleware

type BaseMiddleware = middleware.BaseMiddleware

BaseMiddleware provides a basic implementation of the Middleware interface. It allows chaining of HTTP handlers by passing the request to the next handler in the chain.

Example:

mw := &middleware.BaseMiddleware{}
http.Handle("/", mw.Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, World!"))
})))

http.ListenAndServe(":8080", nil)

type BaseService

type BaseService = service.BaseService

BaseService provides a default implementation of the Service interface. This struct can be embedded in other service implementations to inherit common functionalities or to be extended with custom methods.

type CORSMiddleware

type CORSMiddleware = middleware.CORSMiddleware

CORSMiddleware is the middleware that handles CORS

type CORSOptions

type CORSOptions = middleware.CORSOptions

CORSOptions defines the configuration for the CORS middleware

func NewCorsOptions

func NewCorsOptions(origins []string, methods []string, headers []string) *CORSOptions

New Cors Options.

Example (default to)

 corsOptions := LessGo.NewCorsOptions(
	[]string{"*"}, // Allow all origins
	[]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, // Allowed methods
	[]string{"Content-Type", "Authorization"},           // Allowed headers

)

type Config

type Config = config.Config

type Container

type Container = di.Container

Container wraps the `dig.Container` and provides methods for registering and invoking dependencies. This struct serves as the main entry point for setting up and managing dependency injection within the application.

func NewContainer

func NewContainer() *Container

NewContainer creates a new dependency injection container

type Context

type Context = context.Context

Context holds the request and response writer and provides utility methods.

type Controller

type Controller = controller.Controller

Controller defines the interface that all controllers in the application must implement. Any controller that implements this interface must define the RegisterRoutes method, which is responsible for setting up the necessary routes for the controller.

type FileUploadMiddleware

type FileUploadMiddleware = middleware.FileUploadMiddleware

type HttpConfig

type HttpConfig = config.HttpConfig

type IModule

type IModule = module.IModule

IModule defines the interface for a module in the application. Modules are responsible for managing controllers and services and can include other submodules. Implementers of this interface must provide methods to get the module's name, controllers, and services.

type Middleware

type Middleware = middleware.Middleware

Middleware defines the interface for HTTP middlewares. Implementers should provide a `Handle` method that takes an `http.Handler` and returns a new `http.Handler`. This allows for wrapping existing handlers with additional functionality.

type Module

type Module = module.Module

Module represents a module in the application. It holds the name, a list of controllers, services, and any submodules. The module can be used to organize and group related functionality.

func NewModule

func NewModule(name string, controllers []interface{}, services []interface{}, submodules []IModule) *Module

NewModule creates a new module

type ParserOptions

type ParserOptions = middleware.ParserOptions

func NewParserOptions

func NewParserOptions(size int64) *ParserOptions

Parser options. set default size

type RateLimiterMiddleware

type RateLimiterMiddleware = middleware.RateLimiter

type RateLimiterType

type RateLimiterType = middleware.RateLimiterType
const (
	InMemory RateLimiterType = iota
	RedisBacked
)

type Router

type Router = router.Router

Router represents an HTTP router with middleware support and error handling.

func App

func App(options ...router.Option) *Router

App creates a new app with optional configuration. You can pass options like WithCORS or WithJSONParser to configure the app.

func GetApp

func GetApp() *Router

func NewRouter

func NewRouter(options ...router.Option) *Router

NewRouter creates a new Router with optional configuration

type Service

type Service = service.Service

Service defines the interface for all services in the application. Implementations of this interface can provide specific functionalities required by different parts of the application.

type SizeUnit

type SizeUnit string
const (
	Bytes     SizeUnit = "bytes"
	Kilobytes SizeUnit = "kilobytes"
	Megabytes SizeUnit = "megabytes"
	Gigabytes SizeUnit = "gigabytes"
)

type TaskBuilder

type TaskBuilder = concurrency.TaskBuilder

TASKS

func NewTaskBuilder

func NewTaskBuilder(mode int) *TaskBuilder

type WebSocketServer

type WebSocketServer = websocket.WebSocketServer

func NewWebSocketServer

func NewWebSocketServer() *WebSocketServer

Jump to

Keyboard shortcuts

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