Skip to content

0mjs/zinc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zinc

Zinc Version Go Version License

Zinc is a high-performance, minimal API framework for Go that focuses on speed, simplicity, and developer velocity. Designed to compete with the most popular frameworks around today in performance and usability.

Screenshot 2025-03-17 at 8 52 56 pm

Features

  • Fast: Optimized routing and minimal middleware overhead
  • Simple API: Intuitive and expressive API that follows Go idioms
  • Powerful Router: Support for static routes, path parameters, route groups, and middleware
  • Template Engine: Built-in HTML templating with custom functions and template caching
  • WebSocket Support: Real-time communication with room-based broadcasting
  • File Uploads: Easy file upload handling with size limits and type validation
  • Cron Scheduler: Built-in cron jobs for scheduled tasks
  • Memory Efficient: Utilizes sync.Pool and fixed-size data structures to minimize allocations
  • Well-Tested: Comprehensive test suite ensures reliability

Installation

go get github.com/0mjs/zinc

Quick Start

package main

import (
    "github.com/0mjs/zinc"
    "log"
)

func main() {
    app := zinc.New()
    
    // Simple route
    app.Get("/", func(c *zinc.Context) {
        c.Send("Hello, World!")
    })
    
    // Path parameters
    app.Get("/users/:id", func(c *zinc.Context) {
        c.JSON(zinc.Map{
            "message": "User ID: " + c.Param("id"),
        })
    })
    
    // Route grouping
    api := app.Group("/api")
    api.Get("/users", func(c *zinc.Context) {
        c.JSON(zinc.Map{
            "users": []string{"matthew", "mark", "luke", "john"},
        })
    })
    
    // Middleware
    app.Use(LoggerMiddleware())
    
    log.Fatal(app.Serve())
}

func LoggerMiddleware() zinc.Middleware {
    return func(c *zinc.Context) {
        // Log before request handling
        c.Next() // Pass control to the next middleware or handler
        // Log after request handling
    }
}

Documentation

For complete documentation, visit:

Benchmarks

Zinc is designed for high performance, with benchmarks showing it to be competitive with or faster than other popular Go frameworks:

  • Static routes: ~800ns/op
  • Dynamic routes: ~1.2μs/op
  • Middleware chain: ~2.0μs/op

Typed Service Dependency Injection

Zinc provides a powerful type-safe service dependency injection system that allows you to register and retrieve services by their concrete types.

Registering Services

You can register services using either the string-based approach or the new type-based approach:

// String-based service registration (legacy)
app.Service("userService", userService)

// Type-based service registration (recommended)
app.Register(userService)

Retrieving Services

There are several ways to retrieve services:

  1. String-based retrieval (legacy):
userService := c.Service("userService").(*UserService)
  1. Type-based retrieval using generics:
// From App instance
userService, ok := zinc.ServiceOf[*UserService](app)
if !ok {
    // Handle service not found
}

// From Context
userService, ok := zinc.ContextServiceOf[*UserService](c)
if !ok {
    // Handle service not found
}

The type-based approach provides several advantages:

  • Compile-time type safety
  • No need for type assertions
  • No string literals that could contain typos
  • Better IDE support with code completion

Examples

Basic Usage

// Register a service
app.Register(userService)

// Use the service in a handler
app.Get("/users", func(c *zinc.Context) error {
    service, ok := zinc.ContextServiceOf[*UserService](c)
    if !ok {
        return c.Status(zinc.StatusInternalServerError).String("Service not available")
    }
    return service.GetUsers(c)
})

Using Services in Middleware

Services can be accessed directly from middleware functions:

// Middleware that uses typed services
authMiddleware := func(c *zinc.Context) error {
    // Access the auth service directly from context
    authService, ok := zinc.ContextServiceOf[*AuthService](c)
    if !ok {
        return c.Status(zinc.StatusInternalServerError).String("Auth service not available")
    }
    
    // Use the service
    token := c.Request.Header.Get("Authorization")
    if err := authService.ValidateToken(token); err != nil {
        return c.Status(zinc.StatusUnauthorized).String("Invalid token")
    }
    
    // Continue with the next handler
    return c.Next()
}

// Apply the middleware to routes or groups
app.Get("/protected", authMiddleware, func(c *zinc.Context) error {
    return c.String("Protected resource")
})

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight web framework for Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published