import "github.com/go-deck/routeflow" // [Documentation](https://pkg.go.dev/github.com/go-deck/routeflow)
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.
- 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
- Go 1.16+
- Database (PostgreSQL/MySQL/SQLite3)
go get -u github.com/go-deck/routeflow
myapp/
βββ main.go
βββ go.mod
βββ go.sum
βββ lib.yaml
βββ handlers/
βββ user_handler.go
Middleware | Description |
---|---|
logging |
Request/response logging |
cors |
CORS headers management |
auth |
Basic authentication |
recovery |
Panic recovery |
// 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
}
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
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
// 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
}
go test -v ./...
go build -o app cmd/server/main.go
We welcome contributions! Please see our Contributing Guidelines for details.
This project is licensed under the MIT License - see the LICENSE file for details.
Give a βοΈ if this project helped you!
π Happy coding! π―