Skip to content

RouteFlow is a high-performance, declarative API routing framework for Go that accelerates backend development using YAML. Define RESTful endpoints, middleware, DB connections, and server settings without boilerplate. Switch between Gin, Fiber, or Echo with zero code changes. Ideal for scalable microservices, REST APIs, and rapid prototyping.

License

Notifications You must be signed in to change notification settings

go-deck/routeflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ RouteFlow: Declarative API Routing for Go

Go Reference Go Report Card License: MIT

View Documentation

import "github.com/go-deck/routeflow"  // [Documentation](https://pkg.go.dev/github.com/go-deck/routeflow)

πŸ” Overview

RouteFlow is a high-performance, declarative API routing library for Go that simplifies RESTful API development. Define your entire API structure, including routes, middlewares, and database configurations, in a clean YAML file for better maintainability and scalability.

🌟 Key Features

  • Declarative Configuration - Define your entire API in YAML
  • Middleware Support - Built-in and custom middleware chaining
  • Request Validation - Schema-based request validation
  • Database Integration - Seamless ORM support with GORM
  • RESTful Routes - Intuitive route definitions
  • High Performance - Optimized for speed and low latency
  • Extensible - Easy to extend with custom functionality

πŸš€ Quick Start

Prerequisites

  • Go 1.16+
  • Database (PostgreSQL/MySQL/SQLite3)

Installation

go get -u github.com/go-deck/routeflow

Basic Project Structure

myapp/
β”œβ”€β”€ main.go
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ lib.yaml
└── handlers/
    └── user_handler.go

πŸ›  Middleware System

Built-in Middlewares

Middleware Description
logging Request/response logging
cors CORS headers management
auth Basic authentication
recovery Panic recovery

Custom Middleware Example

// Define your middleware methods
type AuthMiddleware struct{}


// JWT Authentication Middleware
func (m *AuthMiddleware) JWTValidation(c *ctx.Context) (interface{}, int) {
    token := c.GetHeader("Authorization")
    if token == "" {
        return map[string]string{"error": "Authorization header required"}, 401
    }
    
    // Add your JWT validation logic here
    if !isValidToken(token) {
        return map[string]string{"error": "Invalid or expired token"}, 401
    }
    
    return nil, 0 // Continue to next middleware/handler
}

// Rate Limiting Middleware
func (m *AuthMiddleware) RateLimit(c *ctx.Context) (interface{}, int) {
    ip := c.ClientIP()
    if isRateLimited(ip) {
        return map[string]string{"error": "Too many requests"}, 429
    }
    return nil, 0
}

Registering Middleware

middlewares:
  built_in: [logging, recovery]
  custom: [JWTValidation, RateLimit]

## πŸ’Ύ Database Integration

RouteFlow provides seamless integration with multiple databases through GORM:

### Supported Databases

| Database | Connection String Example |
|----------|--------------------------|
| PostgreSQL | `postgres://user:pass@localhost:5432/dbname` |
| MySQL      | `user:pass@tcp(127.0.0.1:3306)/dbname` |
| SQLite3    | `file:test.db` |

### `lib.yaml` Example:

```yaml
database:
  type: postgres  # postgres, mysql, sqlite3
  host: localhost
  port: 5432
  username: dbuser
  password: dbpass
  database: myapp_db
  sslmode: disable
  max_idle_connections: 10
  max_open_connections: 100
  conn_max_lifetime: 1h
  migrate: true  # Auto-migrate models

πŸš€ Getting Started

Example Project Structure

myapp/
β”œβ”€β”€ cmd/
β”‚   └── server/
β”‚       └── main.go
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── config.go
β”‚   β”œβ”€β”€ handler/
β”‚   β”‚   └── user_handler.go
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── auth.go
β”‚   └── model/
β”‚       └── user.go
β”œβ”€β”€ migrations/
β”‚   └── 001_initial_schema.sql
β”œβ”€β”€ pkg/
β”‚   └── utils/
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
└── lib.yaml

Example Handler

// handlers/user_handler.go
package handlers

import (
	"net/http"
	"github.com/go-deck/routeflow/context"
)

type UserHandler struct{}

// GetUser handles GET /users/:id
func (h *UserHandler) GetUser(c *context.Context) (interface{}, int) {
	id := c.Param("id")
	var user User
	
	if err := c.DB.First(&user, id).Error; err != nil {
		return map[string]string{"error": "User not found"}, http.StatusNotFound
	}
	
	return user, http.StatusOK
}

// CreateUser handles POST /users
func (h *UserHandler) CreateUser(c *context.Context) (interface{}, int) {
	var input struct {
		Name     string `json:"name" binding:"required"`
		Email    string `json:"email" binding:"required,email"`
		Password string `json:"password" binding:"required,min=8"`
	}

	if err := c.ShouldBindJSON(&input); err != nil {
		return map[string]string{"error": err.Error()}, http.StatusBadRequest
	}

	user := User{
		Name:     input.Name,
		Email:    input.Email,
		Password: hashPassword(input.Password),
	}

	if err := c.DB.Create(&user).Error; err != nil {
		return map[string]string{"error": "Failed to create user"}, http.StatusInternalServerError
	}

	return user, http.StatusCreated
}

πŸ”„ Development

Running Tests

go test -v ./...

Building

go build -o app cmd/server/main.go

🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

πŸ“„ License

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

πŸ”— Useful Links

🌟 Show Your Support

Give a ⭐️ if this project helped you!


Made with ❀️ by RouteFlow Team

πŸš€ Happy coding! 🎯

About

RouteFlow is a high-performance, declarative API routing framework for Go that accelerates backend development using YAML. Define RESTful endpoints, middleware, DB connections, and server settings without boilerplate. Switch between Gin, Fiber, or Echo with zero code changes. Ideal for scalable microservices, REST APIs, and rapid prototyping.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •